The basic concept

What is version control? Why should I care? Version control is a system that records changes to the contents of one or more files for future reference of revisions to a particular version.

  1. In the development process, we often need to modify or delete a file, but we want to save the history of the file, if through backup, it will be very complicated to manage.
  2. In multiplayer development, if multiple people are required to collaborate on a single page, modification and merging can be tricky. Prone to conflict.

Classification of version control systems

See article: Introduction to version control

Local version control system

A local version control system is a system that records changes in versions on a single machine

Advantages:

  • Ensure that content is not lost

Disadvantages:

  • If you have multiple developers, everyone is working on different systems and computers, and you can’t work together.

Centralized version control system

The SVN is a centralized version control system. A centralized version control system has a single centrally managed server (central server) to store the revised versions of all files. All users can access this server through clients to retrieve the latest files or submit updates.

Advantages:

  • Solved the problem of multi-player collaboration

Disadvantages:

  • Overly dependent on the central server, prone to single point of failure
  • Too dependent on network speed, if there is no network, cannot commit code

Distributed version control system

Git is a distributed version control system. The client of a distributed version control system does not just take a snapshot of the latest version of the file, but mirrors the entire code repository. This way, a failure of any of the co-operating servers can be later recovered using any of the mirrored local repositories. Because each extraction operation is actually a complete backup of the code repository

Advantages:

  • Single point of failure is solved
  • Solved the problem of multi-player collaboration

Git Installation

Git is a free, open source distributed version control system designed to handle any project, small or large, with agility and efficiency.

Environmental installation

Download address

Note:

  1. Do not install in a Chinese directory
  2. Do not use desktop management software

Installation is simple, just go to the next step. Right-click in any directory, you can see the menu, indicating that the installation is successful.

There are two ways to use Git

  1. Git GUI means graphical interface
  2. Git bash command line (recommended)

Gitee account registration

Making website

Matters needing attention:

  • The registered email address must be valid, otherwise there is no way to receive mail
  • Remember your account and password
  • Ensure that the email address and account are configured; otherwise, subsequent Git management cannot be performed

Configure the Git mailbox and account

If you are using Git for the first time, you need to configure the submitter information. It is recommended that you use the same email address as the github account

Git config user.name Specifies your target user name
Git config user.email Specifies your target email address

Configure the global username and mailbox only once using --global. You are advised to set the Github user name and password
git config --global user.name 'Own username'
git config  --global user.email 'My own correct email'

# Check the configuration
git config --list
Copy the code

The git command

Git initialization

git init

  • Use git to initialize a git repository. You need to use Git to manage a projectgit initInitialize
Create a hidden folder in the current directory. Git. You can't change anything in
git init
Copy the code

Working area, temporary storage area, warehouse area

Git repositories are divided into three zones

Workspace: The directory where we write code is called workspace.

Staging: An area of temporary storage. In Git, code cannot be submitted directly from a workspace to the repository. Instead, it needs to be added from the workspace to staging before it can be submitted from staging to the repository. The purpose of the staging area is to avoid misoperations and record Git operations.

Local repository: Dump content stored in staging area permanently to Git repository to generate version numbers. After the version number is generated, you can go back to a specific version at any time.

Git Basic commands

git status

  • Function: View the file status
  • Command:git status
    • Red indicates that files in the workspace need to be submitted
    • Green indicates that files in the staging area need to be submitted
  • Identity in the editor
    • U: indicates that the current file has not been added
    • A: Git add has been executed, but the repository has not yet been committed
    • M: The file has been modified, but there is no add yet
    • C: Conflict: file conflict

git add

  • Function: File byThe workspaceAdded to theThe staging areaIn Git, files cannot be added directly from workspace to warehouse. You must first add files from workspace to staging area and then from staging area to warehouse area.
  • Command:Git add File name/directory name
Add index.html to the staging area
git add index.html

Add all files in the CSS directory to the staging area
git add css

Add all js files in the current directory to the staging area
git add *.js

Add all files in the current directory
git add .
git add -A
git add --all
Copy the code

Note: Empty folders are ignored. If you want to submit the folder, you will create a. Gitkeep file in that directory

git commit

Effect: Add file from temporary storage area to warehouse area, generate version number

Commit files from staging area to warehouse
git commit -m "Submit instructions"

# If you do not write a description of the submission, the vi editor will enter.
git commit   # use vi to enter content

If it is a temporary file, it can be submitted quickly, if it is not traced, then the command will not take effect.
git commit -a -m 'Submission Instructions'

You can use this command to modify the last submission note if the submission note was typed incorrectly
git commit --amend -m "Submit instructions"
Copy the code

git log

  • Effect: View the commit log
  • git logView the submitted logs
  • Git reflog: View all logs

Git contrast

git diff

Git diff can see the difference in each commit

Check the difference between workspace and staging
git diff

Check the difference between the staging area and the warehouse area
git diff --cached

# check the difference between workspace and warehouse area, HEAD represents the most recent commit
git diff HEAD

See the differences between the two versions
git diff c265262 de4845b
Copy the code

Git reset

git reset

  • Role: Version rollback, which restores the code to a version that has been committed.

  • Git reset –hard version resets the code to a specified version (only the first 7 digits of the version are required)

  • Git reset –hard head~1 reverses the version back to the last commit

    • ~1: indicates the last submission
    • ~2: last submission
    • ~0: current submission
  • After you run the git reset command, the version is rolled back. You can view only the information before the current version using the Git log. Git reflog allows you to view all version information

Git ignores files

There are some files in the repository that you don’t want to be managed by Git, such as configuration passwords for data, and how to write code. Git can be configured to ignore files so that they don’t have to be committed.

  • Create one at the root of the repository.gitignoreThe file name is fixed.
  • Add file paths that don’t need to be managed by Git to.gitignoreIn the
  • Be sure to add the ignored file before adding the corresponding file. If the file has already been submitted, adding the ignored file will not take effect
# ignore idea.txt file
idea.txt

# ignore the index.js file under CSS
css/index.js

Ignore all JS files under CSS
css/*.js

# Ignore all files under CSS
css/*.*
# Ignore the CSS folder
css
Copy the code

Git branch operations

A branch is a parallel universe in a science fiction movie, where you are trying to learn Git while you are trying to learn SVN in another parallel universe.

If the two universes didn’t interfere with each other, it wouldn’t matter to you right now. At some point, though, the two parallel universes merged, and as a result, you learned both Git and SVN!

Branch features:

  • Branches do not interfere with each other
  • Branches can be merged

Why branches?

  • If you want to develop a new feature, it takes 2 weeks, you can only write 50% of the code in the first week, and if you submit it immediately, the code is not finished, and the incomplete code will affect the work of others. If you wait until the code is written before submitting it, it’s easy to lose the code.
  • With branches, you can create a branch of your own, invisible to others, and not affect others. You work on your branch, commit to your branch, and once the features are developed, merge into the original branch. This is safe and does not affect other people’s work.
  • In the process of work, we often encounter the situation of multi-task parallel development, and the use of branches can well avoid the influence between tasks.
  • Other versions of tools, such as SVN and CVS, also have the concept of branches, but branches in these tools are very slow and almost meaningless.

Git branch command

In Git, a branch is essentially just a pointer that moves backwards after each code commit, ensuring that it always points to the last version committed. Git uses HEAD to point to the current branch

Create a branch

  • Git Branch Branch nameCreate a branch, and the code in the branch is exactly the same as the current branch when it is created.
  • Git has a name called git on its first commitmasterThe main branch of.
  • git branch dev, creating a branch called dev

See the branch

  • git branchYou can look at all the branches,
  • There will be one before the current branch*
  • In Git, there is a special pointerHEADWill always point to the current branch

Switch branch

  • Git Checkout branch nameThe HEAD pointer points to another branch
  • Anything done on the current branch does not affect other branches unless a branch merge occurs.
  • When the code is submitted, the version number is produced and the current branch points to the latest version number.

Create and switch branches

  • Git checkout -b branch nameCreate and switch branches
  • Switching branches does two things
    • Create a new branch
    • Points the head pointer to the current branch

Delete the branch

  • Git branch -d Specifies the branch nameYou can delete branches
  • Note: You cannot delete the current branch from the current branch. You need to switch to another branch to delete the current branch.

Merging branches

  • Git merge branch nameMerges the contents of other branches into the current branch. If you want to merge the Login branch into the Master branch, you need to switch to the Master branch first
  • inmasterExecute in branchgit merge devdevThe code in the branch is merged intomasterbranch
  • Branch merge

Git merge conflict

  • For the same file, if multiple branches need to be merged, conflicts are easy to occur.
  • When merging branches, if there is a conflict, you have to handle it manually and commit it again, usually by putting your own code after the conflicting code.

Git remote repository

Making and git

Git is not directly related to Github.

  • Git is a version control tool.
  • Github is a code hosting platform, open source community, and remote repository for Git.
//1. GitHub is a hosting platform for open source and private software projects. It is named gitHub because it only supports Git as the only version library format for hosting.
//2. Github is free. Everyone can see the code, but only you can change it yourself. Paid ones can be hidden.
//3. When creating a Git project, do not include Chinese.
Copy the code

Making website

Open Source China – Git

git push

  • Action: Commits code from a local repository to a remote repository
  • Git push repository masterBefore submitting code to the remote repository, note that the master branch must be written and cannot be omitted
  • Example:git push [email protected]:autumnFish/test.git masterIf the first time use, need to fill in making | gitee user name and password

git clone

  • Action: Clone remote repository code to local
  • Git clone
  • git clone git://github.com/autumnFish/test.gitA new one will be created locallytestFolder, which contains one in test.gitDirectory, used to save all version records, and test file also has the latest code, you can directly for subsequent development and use.
  • Git clones use the project name of the remote repository by default, or you can specify it yourself. To do this, run the following command:Git clone

git pull

  • Function: Downloads remote code locally

  • Usually you need to pull once before pushing.

  • Before submitting code to a remote repository, note that the master branch must be written and cannot be omitted

Get updates to the remote repository and merge them with the local branch
git pull
Copy the code

git remote

Each push operation needs to bring the address of the remote warehouse, which is very troublesome. We can set an alias for the address of the warehouse

Create an alias for the remote repositoryGit remote add alias warehouse Warehouse address git remote add autumnFish [email protected]: autumnFish/test. The git# autumnFish
git remote remove autumnFish

Check whether the association is successful
git remote -v

Git pull origin master

# push to remote library:
git push -u autumnFish master
git push
git pull

Git Clone repository has an origin alias by default
Copy the code