git basics

git basics

Submit to local repository

git add & git commit

git among addand commitwith the svn reasons differ: Distributed vs Centralized

Since the distribution of git determines that each of our computers is a complete repository (repository), the addsum commitis relative to our local repository.

There are many things in the repository, the most important of which are:

  1. Temporary storage area called stage (or index) ;
  2. There is also the first branch that Git automatically created for us , the trunk master, and a pointer to the master HEAD.


HEADIt means the latest version of the current branch, but in fact it directly points to the current branch.

If there is only one main branch, that is master, the main branch, it HEADpoints to master, and masterthen points to the latest version

$ git add a.txt//Add a file to the temporary storage area of ​​the local repository
$ git add b.txt
$ git add.//Add all non-version files to the local library temporary storage area
$ git commit -m "add 2 files"//Submit all files in the temporary storage area to the branch (such as master)

Add files to the Git repository in two steps:

  • The first step, use the command git add <file>- the staging area attention, can be used repeatedly to add multiple files;
  • In the second step, use the command to git commitcomplete. - Branch noted that after commit, staging area will be cleared

ps. At this time, it is only added to the local library, not the remote version library github

Remote library github

When there is a local library first and then a remote library, how to associate the remote library

  1. Create a new repository on github
  2. git remote add origin git@server-name:path/repo-name.git

After the association, use the command git push -u origin master to push all the contents of the master branch for the first time;

After that, after each local submission, as long as necessary, you can use the command git push origin master to push the latest changes;

Clone the remote library

$ git clone
$ git branch

When using the clone command to clone a remote library, by default, only the local master branch can be seen.

What if you want to get the dev branch and develop on it?

//The dev branch of the remote origin must be created to the local
$ git checkout -b dev origin/dev

View remote library information

$ git remote

$ git remote -v//detailed information
octocat (fetch)
octocat (push)
origin (fetch)
origin (push)
//The above shows the address of origin that can be crawled and pushed. If you don't have push permission, you won't see the push address.

Push branch

$ git push origin master//Specify the local branch, and then it will be pushed to the corresponding branch
$ git push origin dev

Grab branch

Because the remote branch version is newer than your local one, you need to git pulltry to merge first , that is, update code

If git pullthere is a conflict in the merge, you need to resolve the conflict and submit it locally;

If git pull prompts no tracking information , it means that the link relationship between the local branch and the remote branch has not been created

$ git branch --set-upstream <branch-name> <origin/branch-name>

Version rollback

  1. HEADThe version pointed to is the current version, so Git allows us to shuttle between the version history, using the command
$ git reset --hard commit_id
$ git reset --hard HEAD^//roll back the last version
$ git reset --hard HEAD^^//Roll back to the previous two versions
$ git reset --hard HEAD~100//Roll back to the previous 100 versions
  1. Before shuttle, use git log to view the commit history, mainly looking for the version number, in order to determine which version to roll back to.
    $ git log
    commit 5a396f23af1729fa0c0049c58d42fe0d71541665
    Author: kei <>
    Date: Fri Jul 10 15:20:21 2015 +0800


    commit e325faab05ad640d1e1dc31873dfd362b9c00a92
    Author: zhangfengqi <>
    Date: Fri Jul 10 12:50:58 2015 +0800

        JavaScript inheritance summary

   //If you think the output information is too much and you are dazzled, you can try
    $ git log --pretty=oneline
   //Exit or terminate log press q
  1. To go back to the future, use the git reflogview command history to determine which version to go back to in the future
    $ git reflog
    9a2f804 HEAD@{0}: pull: Merge made by the'recursive' strategy.
    3bb1509 HEAD@{1}: commit: Summary of function currying
    e325faa HEAD@{2}: commit: javascript inheritance summary
    bd0213d HEAD@{3}: clone: ​​from

Modifications between the staging area and the work area

Scenario 1: When you mess up the contents of a file in the workspace, you want to discard the changes in the workspace directly

With commandgit checkout -- <file>

The command git checkout -- readme.txtmeans to undo all the modifications of the readme.txt file in the work area. There are two situations here:

  1. The readme.txt has not been placed in the temporary storage area since it was modified. Now, if the modification is undone, it will return to the same state as the version library;
  2. One is that readme.txt has been added to the temporary storage area and then modified. Now, if the modification is undone, the local file will be restored to the version of the temporary storage area.

ps. The content of the file in the workspace is modified, that is, the local file.

Scenario 2: When you not only change the content of a file in the work area, but also add it to the temporary storage area, you want to discard the record of this file in the temporary storage area

In two steps

  • The first step is to use the command git reset HEAD <file>to return to scenario 1, that is, to cancel the add operation to this file!
  • The second step is to operate according to scenario 1.

Scenario 3: When an inappropriate modification has been submitted to the repository, and you want to cancel this submission, the version is rolled back, but the premise is that it is not pushed to the remote repository.

git checkout -- <file>The meaning of attention

git checkout -- <file>In fact, the version in the version library (including the temporary storage area) is used to replace the version of the workspace. Regardless of whether the workspace is modified or deleted, it can be "restored with one click."

Create and merge branches

The principle of git branch creation

In SVN, the speed of creating and switching branches is very slow, while in git it is a matter of 1 second.

  • When we create a new branch, such as dev, Git creates a new pointer called dev, which points to the same commit of master, and then changes HEAD to point to dev, which means that the current branch is on dev
  • Therefore, the reason for the fast speed is: Except for git adding a dev pointer and changing the HEAD point, the files in the workspace have not changed!
  • At this time, commit, dev and HEAD will move forward, while the master stays in place.

Create merge branch command

$ git branch//View existing branches

$ git branch new_branch//Create branch (without switching)
$ git checkout new_branch//switch branch
$ git checkout -b new_branch//Create + switch to new_branch branch

$ git branch -d new_branch//delete branch
$ git branch -D <name>//If you want to discard a branch that has not been merged, you can delete it forcibly
$ git merge new_branch//merge on the main master
Updating d17efd8..fec145a
 readme.txt | 1 +
 1 file changed, 1 insertion(+)

Fast-forward Tell us that this merge is in "fast forward mode", that is, the master is directly pointed to the current commit of dev, so the merge speed is very fast.

What happens when switching branches

If there is no add and commit content in the current branch, it is impossible to switch to another branch

What if I don’t want to commit for the time being, but I need to switch to another branch?

$ git stash//"store" the current work site, and continue to work after returning to this branch in the future
$ git stash list
stash@{0}: WIP on dev: 6224937 add merge

When I returned to this branch, I found that the temporary storage area was empty! Need to restore the previous stash content

$ git stash apply//After recovery, the stash content is not deleted, you need to use git stash drop to delete
$ git stash pop//delete the stash content while restoring

Another use scenario of stash: When the current branch code does not have commit, it is impossible to pull the code down, and then you need to stash first

Conflict resolution

When you modify the same location of the same file in different branches, and merge after each commit:

In this case, Git cannot perform "quick merge" and can only try to merge their respective modifications, but this merge may have conflicts.

zhangfengqi@ED63038F43AD14D/D/zfengqi/github/Spoon-Knife (master)
$ git merge branch222
Auto-merging styles.css
CONFLICT (content): Merge conflict in styles.css
Automatic merge failed; fix conflicts and then commit the result.

At this time, the branch status will become

zhangfengqi@ED63038F43AD14D/D/zfengqi/github/Spoon-Knife (master|MERGING)

Manual conflict resolution: open the conflicting file and keep the final part

Git with <<<<<<<, =======, >>>>>>>marking the contents of the different branches

After modification, add & commit again

$ git log --graph//You can see the branch merge graph.

Common commands

//pwd is used to display the current directory
$ pwd

$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes.

var gitalk = new Gitalk({ clientID: '82f3fb6574634b43d203', clientSecret: '5790d9fdbfe9340d29810b694b3b2070a9a3b492', repo:'', owner:'zfengqi', admin: ['zfengqi'], id: winMode: true .loc.pathname, }); gitalk.render('gitalk-container');

Reference: git basics-cloud + community-Tencent Cloud