Soft&Skills

Git In Case Of

2013-05-01. Category & Tags: Soft&Skills Soft&Skills

Motivation #

2019:

[ref]

Config #

git config -l # ls all configs

Frequently Used #

git help <command>

git show            # show content of a commit/file-obj. ref:is.gd/KYM5mo
git show $id
git show object
git show $REV:$FILE
git show somebranch:from/the/root/myfile.txt
git show HEAD^^^:test/test.py

git co  -- <file>   # discard modifications in workspace // co = Check Out
git co  .           # discard modifications for all (previously existing?) files in workspace
git co ea083b .     # discard modifications in workspace and co all files from commit ea083b to overwrite files, but without changing branch/commit

git add <file>      # 将工作文件修改提交到本地暂存区
git add .           # 将所有修改过的工作文件提交暂存区
git st/status       # workspace status
git commit -m'msg'  # commit with a msg
git tag v1.0 ea083b # tagging ea083b with a tag "v1.0"
git tag -d ea083b   # delete tag of commit ea083b

git rm <file>       # 从版本库中删除文件
git rm <file> --cached  # 从版本库中删除文件,但不删除文件

git ci <file>
git ci .
git ci -a           # 将git add, git rm和git ci等操作都合并在一起做
git ci -am "some comments"
git ci --amend      # 修改最后一次提交记录

git revert <$id>    # 恢复某次提交的状态,恢复动作本身也创建了一次提交对象
git revert HEAD     # 恢复最后一次提交的状态

git blame [commit] -Ln1,n2 -- prev/file # see sub-section below

Diff #

git diff <file>     # 比较当前文件和暂存区文件差异
git diff
git diff <$id1> <$id2>   # 比较两次提交之间的差异
git diff <branch1>..<branch2> # 在两个分支之间比较
git diff --staged   # 比较暂存区和版本库差异
git diff --cached   # 比较暂存区和版本库差异
git diff --stat     # 仅仅比较统计信息

Reset & Resore (inc. Stage) #

git reset           # = `undo add ` 从暂存区恢复所有文件到工作文件
git reset <file>    # = `undo add <file>` 从暂存区恢复到工作文件
git reset -- .      # 从暂存区恢复到工作文件
git reset --hard    # 恢复最近一次提交过的状态,放弃上次提交后的所有本地修改,用的也很多!!
git reset --hard origin/master
git reset --hard <a_commit>
git reset --soft HEAD~1  # 撤消最后一次commit,所有的修改都保留, 用的最多 !!!

git restore --staged myFile.name  # Unstage one file (undo git add). Copies the last version of myFile.name from repo to index
git restore --staged .            # Unstage all files (undo git add)

Log #

git log
git log <file>      # 查看该文件每次提交记录
git log -p <file>   # 查看每次详细修改内容的diff
git log -p -2       # 查看最近两次详细修改内容的diff
git log --stat      # 查看提交统计信息`</pre>

Local Branch #

take a look #

git br -r           # 查看远程分支
git br -v           # 查看各个分支最后提交信息
git br --merged     # 查看已经被合并到当前分支的分支
git br --no-merged  # 查看尚未被合并到当前分支的分支
git br <new_branch> # 创建新的分支(不常用!)
git co -b <new_branch> # 从当前commit 创建新的分支,并且切换过去 (常用!)

change a branch #

git co <branch>     # 切换到某个分支
git co <branch> .   # co某个分支所有文件,但是不切换分支
git co -b <new_branch> <branch>  # 基于branch创建新的new_branch
git co $id          # 把某次历史提交记录checkout出来,但无分支信息,切换到其他分支会自动删除
git co $id -b <new_branch>  # 把某次历史提交记录checkout出来,创建成一个分支

delete a branch #

git br -d <branch>  # 删除某个分支
git br -D <branch>  # 强制删除某个分支 (未被合并的分支被删除的时候需要强制)

merge & rebase #

git merge <branch>               # 将branch分支合并到当前分支
git merge origin/master --no-ff  # 不要Fast-Foward合并,这样可以生成merge提交

git pull --rebase
git rebase master <branch>       # 将master rebase到branch,相当于:
git co <branch> && git rebase master && git co master && git merge <branch>

blame (track changes) #

Use -L to control which lines to check.

...