系列教程
Git 的四个工作区域
![ad]()
Git 初始化本仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| $ mkdir test_repo
$ cd test_repo
$ git init
$ tree -al ../test_repo test_repo/ └── .git ├── branches ├── config ├── description ├── HEAD ├── hooks │ ├── applypatch-msg.sample │ ├── commit-msg.sample │ ├── post-update.sample │ ├── pre-applypatch.sample │ ├── pre-commit.sample │ ├── prepare-commit-msg.sample │ ├── pre-push.sample │ ├── pre-rebase.sample │ └── update.sample ├── info │ └── exclude ├── objects │ ├── info │ └── pack └── refs ├── heads └── tags
|
Git 配置签名(本地库)
这里的签名仅仅是为了方便标识提交代码的作者身份,并不用于 Git 远程库(Github)的身份认证(例如 Github 登录)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| $ cd git_test
$ git config --help
$ git config user.name peter $ git config user.email peter@gmail.com
$ cat .git/config [user] name = peter email = peter@gmail.com
$ git config --get user.name $ git config --get user.email
$ git config --unset user.name $ git config --unset user.email
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| $ git config --global user.name peter_glb $ git config --global user.email peter@gmail.com
$ cat ~/.gitconfig [user] name = peter_glb email = peter@gmail.com
$ git config --global --get user.name $ git config --global --get user.email
$ git config --global --unset user.name $ git config --global --unset user.email
|
Git 操作新建的文件(本地库)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| $ git status
$ git add .
$ git add api.json
$ git add --all
$ git rm --cached api.json
$ git commit api.json -m 'create message'
$ git commit -am "create message"
|
Git 查看工作区与暂存区的差异
1 2 3 4 5 6 7 8
| $ git status
$ git diff
$ git diff --staged
|
Git 操作修改过的文件(本地库)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| $ git restore api.json
$ git checkout -- api.json
$ git restore --staged api.json
$ git add api.json
$ git reset HEAD api.json
$ git add --all
$ git commit api.json -m 'update message'
$ git commit -am "update message"
|
Git 忽略追踪已提交过的文件与文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| $ touch .gitignore
$ vim .gitignore
$ git rm --cached shop.cpp $ git rm -f --cached shop.cpp $ git rm -r -f --cached store/
$ git add --all && git commit -am 'Cancel tracking file'
|
Git 设置网络代理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| $ git config http.proxy http://192.168.1.122:1080
$ git config --global http.proxy 192.168.1.122:1080
$ git config http.proxy socks5://192.168.1.122:1081
$ git config --global http.proxy socks5://192.168.1.122:1081
$ git config --unset http.proxy
$ git config --global --unset http.proxy
|
Git 设置用户名、邮箱
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| $ git config --list
$ git config --list --global
$ git config user.name clay $ git config user.email example@qq.com
$ git config --unset user.name $ git config --unset user.email
$ git config --global user.name clay $ git config --global user.email example@qq.com
$ git config --global --unset user.name $ git config --global --unset user.email
|
Git 更改最后一次提交的信息
1 2 3 4 5 6 7 8
| $ git commit --amend
$ git commit --amend --author=clay
$ GIT_COMMITTER_DATE="2021-02-04T12:32:03" git commit --amend --date="2021-02-04T12:32:03"
|
Git 撤销提交并清除痕迹
1 2 3 4 5 6 7 8 9 10 11 12
| $ git reset --hard HEAD~4
$ git reflog $ git reset --hard 6ddd44e
$ git push -f
$ git push origin master -f
|
Git 创建和删除标签
1 2 3 4 5 6 7 8 9
| $ git tag <tagname>
$ git tag -a <tagname> -m "message"
$ git reflog $ git tag -a <tagname> <commit_hash> -m "message"
|
1 2 3 4 5
| $ git tag -d <tagname>
$ git push origin :refs/tags/<tagname>
|
1 2 3 4 5
| $ git tag
$ git tag --sort=version:refname
|
1 2 3 4 5
| $ git push origin <tagname>
$ git push origin --tags
|
Git 临时保存工作区修改
提示
git stash 命令可以将当前工作区和暂存区的改动保存到一个临时栈中(方便后续自由切换分支),并将工作区恢复到最近一次提交(commit)的状态(干净状态)。git stash pop 命令可以取出最近一次 stash 的内容并应用到当前分支,然后从 stash 栈中移除这条 stash 记录。
1 2 3 4 5 6 7 8
| $ git stash
$ git stash pop
$ git stash apply
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| $ git stash list stash@{0}: WIP on master: 9f1a7a2 添加新功能 stash@{1}: WIP on dev: 2b3c1ef 修复 bug stash@{2}: WIP on master: 45ac2e1 重构模块
$ git stash show stash@{1}
$ git stash show -p stash@{1}
$ git stash pop stash@{1}
$ git stash apply stash@{1}
$ git stash drop stash@{1}
$ git stash clear
|
Git 复制指定提交到当前分支
提示
git cherry-pick 命令可以将其他分支的某个(或多个)特定提交,复制并 "重放" 到当前分支上。- 假设在
feature 分支中做了一个新的功能提交,现在想把这个提交拷贝到 master 分支,而不是把整个 feature 分支合并过去,这时候可以使用 git cherry-pick 命令实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| $ git reflog 4630fa3 HEAD@{0}: commit: update message 346f528 HEAD@{1}: commit: update message b86daaa HEAD@{2}: commit: update message fdbbf3c HEAD@{3}: commit: update message 775a0c5 HEAD@{4}: commit: update message
$ git checkout master
$ git cherry-pick b86daaa
$ git cherry-pick b86daaa 346f528 4630fa3
$ git cherry-pick <oldest_hash>^..<newest_hash>
|
1 2 3 4 5 6 7 8 9 10 11 12
|
$ git add <file>
$ git cherry-pick --continue
$ git cherry-pick --abort
|