git 学习笔记
一. 创建版本库
- 创建一个空目录
1 2
| mkdir learngit cd learngit
|
- 把这个目录变成 Git 可以管理的仓库
- 把文件添加到版本库
创建一个readme.md
的文件
1 2 3 4
| git add readme.md
git commit -m "wrote a readme file"
|
- 仓库当前的状态
- 查看 difference
- 历史记录
1 2
| git log git log --pretty=oneline
|
- 回退
1 2 3 4 5 6 7 8 9 10
| git reset --hard HEAD^
git reset --hard HEAD^^
git reset --hard HEAD^100
git reset --hard e475a
|
- 记录每一次命令
- 查看工作区和版本库里最新版本的区别
1
| git diff HEAD -- readme.md
|
- 撤销修改
1 2 3 4 5
| git restore readme.md
git restore --staged readme.md
|
- 删除文件
添加一个新文件test.txt
1 2 3 4 5 6 7 8 9 10 11
| vim test.txt git add test.txt git commit -m "add test.txt" rm test.txt
git status
git rm test.txt git commit -m "remove test.txt"
git restore test.txt
|
二、远程仓库
- 添加远程库
1 2 3 4 5 6 7
| git remote add origin git@github.com:gaoyangu/learngit.git
git push -u origin master
git push origin master
git push --set-upstream origin master
|
- 从远程库克隆
1
| git clone git@github.com:michaelliao/gitskills.git
|
- pull
三、分支合并
- 创建
dev
分支
1 2 3 4 5 6 7 8
| git switch -c dev
git checkout -b dev
git branch dev git checkout dev
|
- 查看当前分支
- 切换分支
1 2 3
| git switch master
git checkout master
|
- 合并分支
- 删除分支
- 合并时发生冲突
1 2 3 4 5 6
|
git log --graph --pretty=oneline --abbrev-commit
git log --graph
|
- 分支管理策略
1 2
| git merge --no-ff -m "merge with no-ff" dev
|
- bug分支
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
|
git stash
---
git switch -c issue-101 git add readme.txt git commit -m "fix bug 101"
git switch master git merge --no-ff -m "merged bug fix 101" issue-101 ---
git switch dev
git stash list
git stash pop
git stash apply git stash drop
git stash apply stash@{0}
git cherry-pick 4c805e2
|
- Feature分支
1 2 3 4 5
| git switch -c feature-vulcan
git branch -D feature-vulcan
|
- 多人协作(待补充)
1 2 3 4 5 6
| git remote git remote -v
git push origin master git push origin dev
|
- rebase(待补充)
四、标签管理
标签也是版本库的一个快照。
- 创建标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| git branch git switch master
git tag v1.0
git tag
git log --pretty=oneline --abbrev-commit git tag v0.9 f52c633
git show v0.9
git tag -a v0.1 -m "version 0.1 released" 1094adb
|
- 操作标签
1 2 3 4 5 6 7 8 9 10
| git tag -d v0.1
git push origin v1.0
git push origin --tags
git tag -d v0.9 git push orign :/refs/tags/v0.9
|
五、自定义git
- 忽略特殊文件
- 在 Git 工作区的根目录下创建一个特殊的
.gitignore
文件,然后把要忽略的文件名填进去
- GitHub 已经为我们准备了各种配置文件
- 把
.gitignore
也提交到 Git
1 2 3 4
| git add -f App.class
git check-ignore -v App.class
|
- 配置别名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
git config --global alias.st status
git config --global alias.co checkout git config --global alias.ci commit git config --global alias.br branch
git config --global alias.unstage 'reset HEAD'
git config --global alias.last 'log -1'
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
|
参考资料
- git 官方网站
- Git Cheat Sheet
- 廖雪峰Git教程
- learnGitBranching:一个 Git 命令可视化学习项目
- gitmoji-cli:Git 交互式客户端,方便在提交信息中增加 emoji 表情