Git
CheatSheet
- 不建议使用
git push --force
,推荐--force-with-lease
参数 - 识别大小写
git config core.ignorecase false
- 修改上一次的 commit 记录
git commit --amend
- 比较文件
git diff <branch_name1> <branch_name2> --stat
git diff <branch_name1> --stat # 比较当前文件与 branch_name1
-
当本地有领先远端的 commit,同时远端也有领先本地的 commit(这种情况经常会发生),并使用 git pull 拉去远端代码时,会产生一条 merge commit,例:merge branch 'feature/login' of ssh://gitlab.aaa.net/project/main-web into feature/login,污染了 commit 记录
-
原因:本地分支与远程分支存在分叉,而 git pull = git fetch + git merge,在 merge 时有冲突就会产生一条 merge commit
-
避免方法
-
使用
git pull --rebase
代替 git pull,这种方案的原理是不产生额外的合并节点,而是将远端更新拉取到本地,而后将本地的提交附加到远端更新之后。- 一劳永逸:
git config --global pull.rebase true
- 一劳永逸:
-
在本地 commit 之前 stash,pull 之后再 pop 出来
-
-
-
fork 仓库拉取主仓库的更改:
git pull upstream master
- 将 commit 撤回至暂存区
# <commit-id> 之后的提交(不包括其本身)会撤回至暂存区
git reset --soft <commit-id>
- 强制覆盖远程仓库的 commit 记录
git push --force
- 忽略已跟踪的文件
git update-index --assume-unchanged <file-path>
git update-index --no-assume-unchanged <file-path> # 取消忽略
配置
设置名字、邮箱
git config --global user.name "Your Name"
git config --global user.email "email@example.com"
查看名字、邮箱
git config user.name
git config user.email
也可以在当前 repo 下设置用户名和邮箱,这样只会在此 repo 下生效
git config --local user.name "Your Name"
git config --local user.email "email@example.com"
文件操作
创建版本库(repository)
- 把这个目录变成 Git 可以管理的仓库
$ git init
添加文件
把文件往 Git 版本库里添加的时候,是分两步执行的:
第一步是用 git add
把文件添加进去,实际上就是把文件修改添加到暂存区;
第二步是用 git commit
提交更改,实际上就是把暂存区的所有内容提交到当前分支。