Everything I should know about Git

reference:

廖雪峰 - Git教程

Coursera - Server-side Development with NodeJS, Express and MongoDB

0. Git Diagram

工作区(Working Directory) --add--> 暂存区(Staging area) --commit--> Master

HEAD refers to master

1. Global Configuration for Git

  • Check to make sure that Git is installed and available on the command line, by typing the following at the command prompt:
1
git --version
  • To configure your user name to be used by Git, type the following at the prompt:
1
git config --global user.name "Your Name"
  • To configure your email to be used by Git, type the following at the prompt:
1
git config --global user.email <your email address>
  • Check your default Git global configuration:
1
git config --list

2. Start

  • Initializes the current folder as a git repository, just go to the folder and type the following at the prompt:
1
git init

Now the folder will be mark as a master

  • Current status of the foler:
    • current branch
    • untracked files: files that are not been added to Git repository. Use add to add.
    • Changes not staged for commit: still use add to update
    • changes to be committed: files listed below are ready to be committed
1
git status
  • See change details of a file:
1
git diff <file>
  • Add files to staging area:
1
2
git add <file(s)/folder(s)>
git add . # the "." means all files
  • After adding some files, you can commit the current state of the folder into Git repository:
1
2
git commit
# use -m <message> to add message
  • Show the brief log of all the commits with messages
1
git log --oneline

3. Checkout/Reset/Restore

  • Discard changes of the last commit, and checkout the file from an older commit. last commit -> any commit (staged)
1
2
git checkout <commit> <file>
# commit means commit id, using first several chars is ok

The file changes will be automatically set into the staging area, all you need to do is submitting.

  • Reset a file from staged to untracked: staged -> untracked
1
2
3
git reset <commit [default=HEAD]> <file>
git reset <commit-id-found-from-log> # use git log to find previous commit
git reset HEAD^ # previous version

This command essentially has deeper meaning: You can return to past versions by config the <commit>.

HEAD: current version; HEAD^: previous version; HEAD^^: previous previous version...;HEAD-100: 100 versions before

  • Reset the staging area to untracked. (Just status, not actually discarding your changes on the files)
1
git reset
  • Discard changes of an untracked file: untracked -> last commit
1
2
3
git restore <file>
# or
git checkout -- <file>
  • Return to a future version? Still use commit id as reference in git reset. But how to know the commit id of a future version? Use reflog to see command history.
1
git reflog

说人话

场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令git checkout -- <file>或者git restore <file>

场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令git reset HEAD <file>,就回到了场景1,第二步按场景1操作。

场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,用git reset HEAD^,不过前提是没有推送到远程库。

场景4:那已经推送到远程库了咋办呢?当然还是先在本地撤销到合适的历史版本(同场景3),然后再强制推送到远程库:git push --force

4. Stash的简单使用

如果本地有更改,暂时不想commit;可是远程库又有了新的更新,需要同步过来。那就

  • 先用stash把本地更改暂存起来:git stash
  • 然后正常pull:git pull
  • 最后,把刚存起来的本地更改pop出来:git stash pop

如果有太多stash,想清空:git stash clear

Github

0. Connect with your Github account using SSH

1. Connect with a remote repo

  • Suppose you have a blank repo on Github, and an existing repo on your computer
  • Connect local repo with a remote one:
1
git remote add origin git@github.com:username/repoName.git

origin is the default name of a remote repo.

  • Push local repo to the remote one:
1
git push -u origin master

command -u is for: Branch 'master' set up to track remote branch 'master' from 'origin'.

After the first push, you don't need the -u.

1
git push origin master
  • Show info of remote repo:
1
git remote -v
  • Disconnect remote repo by name:
1
git remote rm <name>

2. Clone an existing remote repo

1
git clone git@github.com:username/repoName.git

Instead of ssh, https is also available.

Branch

  • Create a new branch
1
git branch <name>
  • Switch to a branch
1
git switch <name>
  • Show branchs:
1
git branch

Current bransh has a * ahead.

  • Merge branch to current one:
1
git merge <name>
  • Delete a branch
1
git branch -d <name>

Solve conflicts

When you merge a branch to current branch, sometimes there are conflicts.

The conflicts will be directly showed in the files as:

1
2
3
4
5
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature1
  • Just fix it manually, then add, then commit.
  • Show merge logs:
1
2
git log --graph --pretty=oneline --abbrev-commit
git log --graph

stash to save current work

1
2
3
git stash # save the work
git stash list # list saved works
git stash pop # restore the saved works and delete the stash

cherry-pick to copy a specific commit

1
git cherry-pick <commit id>

Cooperation

  • 从本地推送分支,使用git push origin branch-name,如果推送失败,先用git pull抓取远程的新提交;
  • 在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称最好一致;
  • 建立本地分支和远程分支的关联,使用git branch --set-upstream branch-name origin/branch-name
  • 从远程抓取分支,使用git pull,如果有冲突,要先处理冲突。

Tag

Tag is a name that we give to a commit.

  • Set a new tag to the newest commit:
1
2
3
git tag <name>
# add message:
git tag -a v0.1 -m "version 0.1 released"

To a past commit?

1
git tag <name> <commit id>
  • List all tag:
1
git tag

Show info of a tag:

1
git show <tagname>
  • Delete a tag:
1
git tag -d <name>