Notes for Git¶
约 897 个字 预计阅读时间 3 分钟
一个自底向上的学习过程,最后附上了常用指令,暂无操作演示,可以参考原课程网站
Terminology¶
定义
blob
- 一个文件被称作blob.
tree
- 一个文件夹被称作tree.
commit
- 整个仓库的快照被称作commit.
history
- commit 的有向无环图构成history.
object
- 一个object是 blob、tree 或者 commit,每个对象都有自己对应的 hash.
reference
- 一个reference是一个对象的别名,除了别名,我们还可以通过 hash 来引用一个对象。
main/master
- main/master 作为一个特殊的 reference 总是指向开发中的最新稳定版本。
HEAD
- HEAD 作为一个特殊引用指向我们目前在历史中的位置。
Git repository
- 一个 Git repository* 是 object 和 reference 的集合。
Staging area
- Staging area 是一种机制来让我们指定哪些对象要被 commit, 而非整个仓库。
Git Data Model¶
首先定义数据对象
在 Git 存储结构中,每个对象对应一个 hash
Commands¶
Basic
git help <command>
: get help for a git commandgit init
: creates a new git repo, with data stored in the .git directorygit status
: tells you what’s going ongit add <filename>
: adds files to staging areagit commit
: creates a new commitgit log
: shows a flattened log of historygit log --all --graph --decorate
: visualizes history as a DAGgit diff <filename>
: show the differences between current file and HEAD version filegit diff <revision> <filename>
: shows differences between current file and specified snapshotgit diff <reversin> <reversion> <filename>
shows the differences between two specified version of filegit checkout <revision>
: updates HEAD and current branchgit checkout <filename>
: erase all the changes we made after the HEAD version for this filegit cat-file -p <hash>
: The instruction to catch a tree or blob
Branching and Merging
git branch
: shows branchesgit branch <name>
: creates a branchgit checkout -b <name>
: creates a branch and switches to it- same as
git branch <name>
;git checkout <name>
git merge <revision>
: merges into current branch;wait for latter adjustments when conflictions occur(But the file has been changed already)git merge --abort
: erase the changes git try to make when an merge conflict occursgit merge --continue
: continue to merge after you fix the merge conflictiongit mergetool
: use a fancy tool to help resolve merge conflictsgit rebase
: rebase set of patches onto a new base
More on rebase
Commonly used to "move" an entire branch to another base, creating copies of the commits in the new location.
-
Rebase the current branch on top of another specified branch:
git rebase new_base_branch
-
Start an interactive rebase, which allows the commits to be reordered, omitted, combined or modified:
git rebase -i target_base_branch_or_commit_hash
-
Continue a rebase that was interrupted by a merge failure, after editing conflicting files:
git rebase --continue
-
Continue a rebase that was paused due to merge conflicts, by skipping the conflicted commit:
git rebase --skip
-
Abort a rebase in progress (e.g. if it is interrupted by a merge conflict):
git rebase --abort
-
Move part of the current branch onto a new base, providing the old base to start from:
git rebase --onto new_base old_base
-
Reapply the last 5 commits in-place, stopping to allow them to be reordered, omitted, combined or modified:
git rebase -i HEAD~5
-
Auto-resolve any conflicts by favoring the working branch version (
theirs
keyword has reversed meaning in this case):git rebase -X theirs branch_name
Remotes
git remote
: list remotesgit remote add <name> <url>
: add a remote ;name is origin by convention if we have only one remote; url can be a web address or a local directorygit push <remote> <local branch>:<remote branch>
: send objects to remote, and update remote referencegit branch --set-upstream-to=<remote>/<remote branch>
: set up correspondence between local and remote branchgit fetch
: retrieve objects/references from a remote; be aware of the changes pushed here from the remotegit pull
: same as git fetch; git mergegit clone
: download repository from remote
Undo
git commit --amend
: edit a commit’s contents/messagegit reset HEAD <file>
: unstage a filegit checkout <file>
: discard changes
Advanced Git
git config
: Git is highly customizablegit clone --depth=1
: shallow clone, without entire version historygit add -p
: interactive staginggit rebase -i
: interactive rebasinggit blame
: show who last edited which linegit stash
: temporarily remove modifications to working directorygit bisect
: binary search history (e.g. for regressions).gitignore
: specify intentionally untracked files to ignore