|
1 | 1 | ### Git |
2 | | -* Git的理念 |
3 | | - * distributed version control system (DVCS) |
4 | | - * The Platonic ideal is that each commit should compile and should move steadily towards more and more tests passing. |
| 2 | +* [Pro Git](https://git-scm.com/book/en/v2) |
| 3 | +* [Learn Git Branching](https://learngitbranching.js.org/?locale=en_US) |
5 | 4 | * [git handbook](https://guides.github.com/introduction/git-handbook/),里面有一些资源 |
6 | 5 | * [完整doc文档](https://git-scm.com/docs) |
7 | 6 | * [resources to learn Git](https://try.github.io/) |
8 | 7 | * [如何fork一个私库](https://stackoverflow.com/questions/10065526/github-how-to-make-a-fork-of-public-repository-private) |
9 | 8 |
|
10 | | -#### 重要命令 |
11 | | -* `git clone` creates a local copy of a project that already exists remotely. The clone includes all the project’s files, history, and branches. |
12 | | - * --depth=1:clone速度慢的时候只clone最后一次commit |
| 9 | +#### VCS(version control system) |
| 10 | +##### Git的理念 |
| 11 | +* distributed version control system (DVCS) |
| 12 | +* The Platonic ideal is that each commit should compile and should move steadily towards more and more tests passing. |
| 13 | +* ugly interface and beautiful design -> bottom-up地理解git |
13 | 14 |
|
14 | | -* `git add` stages a change. Git tracks changes to a developer’s codebase, but it’s necessary to stage and take a snapshot of the changes to include them in the project’s history. This command performs staging, the first part of that two-step process. Any changes that are staged will become a part of the next snapshot and a part of the project’s history. Staging and committing separately gives developers complete control over the history of their project without changing how they code and work. |
| 15 | +This [XKCD comic](https://xkcd.com/1597/) captures Git’s reputation: |
| 16 | + |
| 17 | +<img src="https://raw.githubusercontent.com/huangrt01/Markdown-Transformer-and-Uploader/master/Notes/git/git.png" alt="git" style="zoom:100%;" /> |
| 18 | + |
| 19 | +##### Git's data model |
| 20 | +* Snapshots: 文件是blob,文件夹是tree,snapshot是top-level tree |
| 21 | + |
| 22 | +* Modeling history: relating snapshots |
| 23 | + * a history is a directed acyclic graph (DAG) of snapshots |
| 24 | + * 一个snapshot可能有多个parent,比如merge |
| 25 | + * snapshot被称作commit |
| 26 | +* Data model, as pseudocode |
| 27 | +* Objects and content-addressing |
| 28 | + * `git cat-file -p` 显示对象信息 |
| 29 | +* References:照顾可读性,和对象不同,它是mutable的 |
| 30 | + * `master`表示主分支的最近commit |
| 31 | + * `HEAD`表示"where we currently are" |
| 32 | +* repositories = objects + references |
| 33 | +```python |
| 34 | +// a file is a bunch of bytes |
| 35 | +type blob = array<byte> |
| 36 | + |
| 37 | +// a directory contains named files and directories |
| 38 | +type tree = map<string, tree | blob> |
| 39 | + |
| 40 | +// a commit has parents, metadata, and the top-level tree |
| 41 | +type commit = struct { |
| 42 | + parent: array<commit> |
| 43 | + author: string |
| 44 | + message: string |
| 45 | + snapshot: tree |
| 46 | +} |
| 47 | + |
| 48 | +type object = blob | tree | commit |
| 49 | +objects = map<string, object> |
| 50 | +def store(object): |
| 51 | + id = sha1(object) |
| 52 | + objects[id] = object |
| 53 | + |
| 54 | +def load(id): |
| 55 | + return objects[id] |
| 56 | +``` |
| 57 | +##### Staging area |
| 58 | +* 和`git add`联系 |
| 59 | +* 意义在于摆脱snapshot和当前状态的绝对联系,使commit操作更灵活 |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +#### Git command-line interface |
| 64 | + |
| 65 | +##### Basics |
| 66 | + |
| 67 | +- `git help <command>`: get help for a git command |
| 68 | +- `git init`: creates a new git repo, with data stored in the `.git` directory |
| 69 | +- `git status`: tells you what's going on |
| 70 | +- `git add <filename>`: adds files to staging area |
| 71 | +- `git commit`: creates a new commit |
| 72 | + - Write [good commit messages](https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html)! |
| 73 | + - Even more reasons to write [good commit messages](https://chris.beams.io/posts/git-commit/)! |
| 74 | +- `git log`: shows a flattened log of history |
| 75 | +- `git log --all --graph --decorate`: visualizes history as a DAG |
| 76 | +- `git diff <filename>`: show differences since the last commit |
| 77 | +- `git diff <revision> <filename>`: shows differences in a file between snapshots |
| 78 | +- `git checkout <revision>`: updates HEAD and current branch |
15 | 79 |
|
| 80 | +##### Branching and merging |
| 81 | +- `git branch`: shows branches |
| 82 | +- `git branch <name>`: creates a branch |
| 83 | +- `git checkout -b <name>`: creates a branch and switches to it |
| 84 | + - same as `git branch <name>; git checkout <name>` |
| 85 | +- `git merge <revision>`: merges into current branch |
| 86 | +- `git mergetool`: use a fancy tool to help resolve merge conflicts |
| 87 | +- `git rebase`: rebase set of patches onto a new base |
| 88 | + |
| 89 | +##### Remotes |
| 90 | +- `git remote`: list remotes |
| 91 | +- `git remote add <name> <url>`: add a remote |
| 92 | +- `git push <remote> <local branch>:<remote branch>`: send objects to remote, and update remote reference |
| 93 | +- `git branch --set-upstream-to=<remote>/<remote branch>`: set up correspondence between local and remote branch |
| 94 | +- `git fetch`: retrieve objects/references from a remote |
| 95 | +- `git pull`: same as `git fetch; git merge` |
| 96 | +- `git clone`: download repository from remote |
| 97 | + |
| 98 | +##### Undo |
| 99 | +- `git commit --amend`: edit a commit's contents/message |
| 100 | +- `git reset HEAD <file>`: unstage a file |
| 101 | +- `git checkout -- <file>`: discard changes |
| 102 | + |
| 103 | +##### Advanced Git |
| 104 | +- `git config`: Git is [highly customizable](https://git-scm.com/docs/git-config) |
| 105 | +- `git clone --depth=1`: shallow clone, without entire version history |
| 106 | +- `git add -p`: interactive staging |
| 107 | +- `git rebase -i`: interactive rebasing |
| 108 | +- `git blame`: show who last edited which line |
| 109 | +- `git stash`: temporarily remove modifications to working directory |
| 110 | +- `git bisect`: binary search history (e.g. for regressions) |
| 111 | +- `.gitignore`: [specify](https://git-scm.com/docs/gitignore) intentionally untracked files to ignore |
| 112 | + |
| 113 | +#### Miscellaneous |
| 114 | +- **GUIs**: there are many [GUI clients](https://git-scm.com/downloads/guis) |
| 115 | +out there for Git. We personally don't use them and use the command-line |
| 116 | +interface instead. |
| 117 | +- **Shell integration**: it's super handy to have a Git status as part of your |
| 118 | +shell prompt ([zsh](https://github.com/olivierverdier/zsh-git-prompt), |
| 119 | +[bash](https://github.com/magicmonty/bash-git-prompt)). Often included in |
| 120 | +frameworks like [Oh My Zsh](https://github.com/ohmyzsh/ohmyzsh). |
| 121 | +- **Editor integration**: similarly to the above, handy integrations with many |
| 122 | +features. [fugitive.vim](https://github.com/tpope/vim-fugitive) is the standard |
| 123 | +one for Vim. |
| 124 | +- **Workflows**: we taught you the data model, plus some basic commands; we |
| 125 | +didn't tell you what practices to follow when working on big projects (and |
| 126 | +there are [many](https://nvie.com/posts/a-successful-git-branching-model/) |
| 127 | +[different](https://www.endoflineblog.com/gitflow-considered-harmful) |
| 128 | +[approaches](https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow)). |
| 129 | +- **GitHub**: Git is not GitHub. GitHub has a specific way of contributing code |
| 130 | +to other projects, called [pull |
| 131 | +requests](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests). |
| 132 | +- **Other Git providers**: GitHub is not special: there are many Git repository |
| 133 | +hosts, like [GitLab](https://about.gitlab.com/) and |
| 134 | +[BitBucket](https://bitbucket.org/). |
| 135 | + |
| 136 | +#### Resources |
| 137 | +- [Pro Git](https://git-scm.com/book/en/v2) is **highly recommended reading**. |
| 138 | +Going through Chapters 1--5 should teach you most of what you need to use Git |
| 139 | +proficiently, now that you understand the data model. The later chapters have |
| 140 | +some interesting, advanced material. |
| 141 | +- [Oh Shit, Git!?!](https://ohshitgit.com/) is a short guide on how to recover |
| 142 | +from some common Git mistakes. |
| 143 | +- [Git for Computer |
| 144 | +Scientists](https://eagain.net/articles/git-for-computer-scientists/) is a |
| 145 | +short explanation of Git's data model, with less pseudocode and more fancy |
| 146 | +diagrams than these lecture notes. |
| 147 | +- [Git from the Bottom Up](https://jwiegley.github.io/git-from-the-bottom-up/) |
| 148 | +is a detailed explanation of Git's implementation details beyond just the data |
| 149 | +model, for the curious. |
| 150 | +- [How to explain git in simple |
| 151 | +words](https://smusamashah.github.io/blog/2017/10/14/explain-git-in-simple-words) |
| 152 | +- [Learn Git Branching](https://learngitbranching.js.org/) is a browser-based |
| 153 | +game that teaches you Git. |
| 154 | + |
| 155 | + |
| 156 | +* `git add` stages a change. Git tracks changes to a developer’s codebase, but it’s necessary to stage and take a snapshot of the changes to include them in the project’s history. This command performs staging, the first part of that two-step process. Any changes that are staged will become a part of the next snapshot and a part of the project’s history. Staging and committing separately gives developers complete control over the history of their project without changing how they code and work. |
| 157 | +* `git log --all --graph --decorate` |
| 158 | +* `git cat-file -p`: 显示对象信息 |
| 159 | + * 40位Hash值,前2位是文件夹,后38位是文件名 |
| 160 | + * 存在`.git/objects/`中 |
| 161 | + * [理解git常用命令原理](http://www.cppblog.com/kevinlynx/archive/2014/09/09/208257.html) |
| 162 | +* `git clone` creates a local copy of a project that already exists remotely. The clone includes all the project’s files, history, and branches. |
| 163 | + * --depth=1:clone速度慢的时候只clone最后一次commit |
| 164 | +* `git checkout master`: 丢弃未commit的变化 |
16 | 165 | * `git commit` saves the snapshot to the project history and completes the change-tracking process. In short, a commit functions like taking a photo. Anything that’s been staged with `git add` will become a part of the snapshot with `git commit`. |
17 | 166 | * `git commit -am "m"`可以先add再commit,但前提是commit的文件都是tracked状态 |
18 | 167 |
|
|
24 | 173 |
|
25 | 174 | * `git pull` updates the local line of development with updates from its remote counterpart. Developers use this command if a teammate has made commits to a branch on a remote, and they would like to reflect those changes in their local environment. |
26 | 175 |
|
| 176 | +* `git fetch`可以获取最新版本 |
| 177 | +```shell |
| 178 | +git fetch origin master:tmp |
| 179 | +git diff tmp |
| 180 | +git merge tmp |
| 181 | +git branch -d tmp |
| 182 | +``` |
| 183 | + |
27 | 184 | * `git push` updates the remote repository with any commits made locally to a branch. |
28 | 185 | * `git push origin lab1:lab1` |
29 | 186 | * `git push --set-upstream origin my-branch`,本地关联远程分支,用来省略上面一行的分支标注 |
30 | 187 |
|
| 188 | +* `git reset` |
| 189 | + * `git reset --hard` 回到上次commit的版本,配合`git pull` |
| 190 | + |
31 | 191 | * `git submodule add <url> /path` |
32 | 192 | * clone之后初始化:`git submodule update --init --recursive` |
33 | 193 | * 更新:`git submodule update --init --remote` |
|
0 commit comments