- 1. Git常用命令
- 2. git rebase
- 2.1. 合并多余提交记录
- 2.2. 修改提交记录
- 3. git设置忽略特殊文件
- 3.1. 忽略文件的原则
- 3.2. 设置的方法
- 3.3. gitignore 不生效解决方法
- 4. Git分支重命名
- 5. 代码冲突
- 6. 修改历史提交的用户信息
- 7. 撤销已经push的提交
1. Git常用命令
| 分类 | 子类 | git command | zsh alias |
|---|---|---|---|
| 分支 | 查看当前分支 | git branch |
gb |
| 创建新分支,仍停留在当前分支 | git branch |
||
| 创建并切换到新分支 | git checkout -b |
gcb | |
| 切换分支 | git checkout |
||
| 合并分支 | git checkout |
||
| 提交 | 查看状态 | git status | gst |
| 查看修改部分 | git diff —color | gd | |
| 添加文件到暂存区 | git add —all | ||
| 提交本地仓库 | git commit -m “ |
||
| 推送到指定分支 | git push -u origin |
||
| 查看提交日志 | git log | - |
2. git rebase
如果信息修改无法生效,设置永久环境变量:export EDITOR=vim
帮助信息:
# Rebase 67da308..6ef692b onto 67da308 (1 command)## Commands:# p, pick = use commit# r, reword = use commit, but edit the commit message# e, edit = use commit, but stop for amending# s, squash = use commit, but meld into previous commit# f, fixup = like "squash", but discard this commit's log message# x, exec = run command (the rest of the line) using shell# d, drop = remove commit## These lines can be re-ordered; they are executed from top to bottom.## If you remove a line here THAT COMMIT WILL BE LOST.## However, if you remove everything, the rebase will be aborted.## Note that empty commits are commented out
2.1. 合并多余提交记录
#以交互的方式进行rebasegit rebase -i master#合并多余提交记录:s, squash = use commit, but meld into previous commitpick 6ef692b FIX: Fix parsing docker image version errors 3df667y FIX: the second pushs 3fds95t FIX: the third push保存退出# 进入修改交互界面删除需要删除的提交记录,保存退出#查看提交记录是否已被修改git log#最后强制提交到分支git commit --force -u origin fix/add-unit-test-for-global-role-revoking
2.2. 修改提交记录
#以交互的方式进行rebasegit rebase -i master#修改提交记录:e, edit = use commit, but stop for amendinge 6ef692b FIX: Fix parsing docker image version errore 5ty697u FIX: Fix parsing docker image version error#保存退出git commit --amend#修改提交记录内容,保存退出git rebase --continuegit commit --amend#修改下一条提交记录,保存退出git rebase --continuegit status # 查看状态提示#最后强制提交到分支git commit --force -u origin fix/add-unit-test-for-global-role-revoking#查看提交记录是否已被修改git log
3. git设置忽略特殊文件
3.1. 忽略文件的原则
- 忽略操作系统自动生成的文件,比如缩略图等;
- 忽略编译生成的中间文件、可执行文件等,也就是如果一个文件是通过另一个文件自动生成的,那自动生成的文件就没必要放进版本库,比如Java编译产生的
.class文件; - 忽略你自己的带有敏感信息的配置文件,比如存放口令的配置文件。
3.2. 设置的方法
在项目的workdir 下编辑 .gitignore 文件,文件的路径填写为workdir的相对路径。
.idea/ #IDE的配置文件_build/server/server #二进制文件
3.3. gitignore 不生效解决方法
原因是.gitignore只能忽略那些原来没有被track的文件,如果某些文件已经被纳入了版本管理中,则修改.gitignore是无效的。那么解决方法就是先把本地缓存删除(改变成未track状态),然后再提交:
git rm -r --cached .git add .git commit -m 'update .gitignore'
4. Git分支重命名
假设分支名称为oldName
想要修改为 newName
1. 本地分支重命名(还没有推送到远程)
git branch -m oldName newName
2. 远程分支重命名 (已经推送远程-假设本地分支和远程对应分支名称相同)
a. 重命名远程分支对应的本地分支
git branch -m oldName newName
b. 删除远程分支
git push --delete origin oldName
c. 上传新命名的本地分支
git push origin newName
d.把修改后的本地分支与远程分支关联
git branch --set-upstream-to origin/newName
5. 代码冲突
git checkout mastergit pullgit checkout <branch>git rebase -i masterfix conflictgit rebase --continuegit push --force -u origin <branch>
6. 修改历史提交的用户信息
1、克隆并进入你的仓库
git clone --bare https://github.com/user/repo.gitcd repo.git
2、创建以下脚本,例如命名为rename.sh
#!/bin/shgit filter-branch --env-filter 'OLD_EMAIL="your-old-email@example.com" #修改参数为你的旧提交邮箱CORRECT_NAME="Your Correct Name" #修改参数为你新的用户名CORRECT_EMAIL="your-correct-email@example.com" #修改参数为你新的邮箱名if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]thenexport GIT_COMMITTER_NAME="$CORRECT_NAME"export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"fiif [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]thenexport GIT_AUTHOR_NAME="$CORRECT_NAME"export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"fi' --tag-name-filter cat -- --branches --tags
3、执行脚本
chmod +x rename.shsh rename.sh
4、查看新 Git 历史有没有错误。
#可以看到提交记录的用户信息已经修改为新的用户信息git log
5、确认提交内容,重新提交(可以先把rename.sh移除掉)
git push --force --tags origin 'refs/heads/*'
7. 撤销已经push的提交
# 本地仓库回退到某一版本git reset -hard <commit-id># 强制 PUSH,此时远程分支已经恢复成指定的 commit 了git push origin master --force
