git 学习笔记

本文最后更新于:2020年3月12日

一. 创建版本库

  1. 创建一个空目录
1
2
mkdir learngit
cd learngit
  1. 把这个目录变成 Git 可以管理的仓库
1
git init
  1. 把文件添加到版本库

创建一个readme.md的文件

1
2
3
4
# 把文件修改添加到暂存区
git add readme.md
# 把暂存区的所有内容提交到当前分支
git commit -m "wrote a readme file"
  1. 仓库当前的状态
1
git status
  1. 查看 difference
1
git diff
  1. 历史记录
1
2
git log
git log --pretty=oneline
  1. 回退
1
2
3
4
5
6
7
8
9
10
# 上一个版本
git reset --hard HEAD^
# 上上个版本
git reset --hard HEAD^^
#往前100个版本
git reset --hard HEAD^100

# 未来的版本
# e475a 是之前的版本号,没必要写全
git reset --hard e475a
  1. 记录每一次命令
1
git reflog
  1. 查看工作区和版本库里最新版本的区别
1
git diff HEAD -- readme.md
  1. 撤销修改
1
2
3
4
5
# 未使用add
git restore readme.md

# 使用了add
git restore --staged readme.md
  1. 删除文件

添加一个新文件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
# 1. 从版本库中删除
git rm test.txt
git commit -m "remove test.txt"
# 2.把误删的文件恢复到最新版本
git restore test.txt

二、远程仓库

  1. 添加远程库
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. 从远程库克隆
1
git clone git@github.com:michaelliao/gitskills.git
  1. pull
1
git pull

三、分支合并

  1. 创建dev分支
1
2
3
4
5
6
7
8
# 推荐使用switch
git switch -c dev

# -b 创建并切换
git checkout -b dev
# 或者
git branch dev
git checkout dev
  1. 查看当前分支
1
git branch
  1. 切换分支
1
2
3
git switch master

git checkout master
  1. 合并分支
1
git merge dev
  1. 删除分支
1
git branch -d dev
  1. 合并时发生冲突
1
2
3
4
5
6
# 手动解决

# 分支的合并情况
git log --graph --pretty=oneline --abbrev-commit
# 分支合并图
git log --graph
  1. 分支管理策略
1
2
# 普通模式合并,合并后的历史有分支,能看出来曾经做过合并
git merge --no-ff -m "merge with no-ff" dev
  1. 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
# 储藏当前的工作现场
# dev分支
git stash

---
# issue-101分支
git switch -c issue-101
git add readme.txt
git commit -m "fix bug 101"
# master
git switch master
git merge --no-ff -m "merged bug fix 101" issue-101
---
#dev分支
git switch dev
# 查看存储的工作现场
git stash list

# 恢复同时删除stash内容
git stash pop
# 恢复但是不删除stash内容
git stash apply
git stash drop

# 多次stash,恢复指定stash
git stash apply stash@{0}

# 复制修复的bug到dev中
# 4c805e2 : commit id
git cherry-pick 4c805e2
  1. Feature分支
1
2
3
4
5
# 添加新的功能
git switch -c feature-vulcan

# 丢弃一个没有被合并过的分
git branch -D feature-vulcan
  1. 多人协作(待补充)
1
2
3
4
5
6
# 查看远程库的信息
git remote
git remote -v
# 推送本地分支
git push origin master
git push origin dev
  1. rebase(待补充)

四、标签管理

标签也是版本库的一个快照。

  1. 创建标签
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

# 找到历史提交的 commit id
git log --pretty=oneline --abbrev-commit
git tag v0.9 f52c633

# 查看标签信息
git show v0.9

# 创建带有说明的标签
# -a 标签名
# -m 说明文字
git tag -a v0.1 -m "version 0.1 released" 1094adb
  1. 操作标签
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

  1. 忽略特殊文件
  • 在 Git 工作区的根目录下创建一个特殊的.gitignore文件,然后把要忽略的文件名填进去
  • GitHub 已经为我们准备了各种配置文件
  • .gitignore也提交到 Git
1
2
3
4
# 用-f 强制添加到 Git
git add -f App.class
# 检查
git check-ignore -v App.class
  1. 配置别名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# st 表示 status
# --global参数是全局参数
# 也就是这些命令在这台电脑的所有 Git 仓库下都有用
git config --global alias.st status

git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.br branch

# 把暂存区的修改撤销掉(unstage),重新放回工作区
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/config文件中
  • 别名就在[alias]后面,要删除别名,直接把对应的行删掉即可

  • 当前用户的 Git 配置文件放在用户主目录下的一个隐藏文件.gitconfig

参考资料

  1. git 官方网站
  2. Git Cheat Sheet
  3. 廖雪峰Git教程
  4. learnGitBranching:一个 Git 命令可视化学习项目
  5. gitmoji-cli:Git 交互式客户端,方便在提交信息中增加 emoji 表情

本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!