git 常用指令

156 阅读1分钟

1. 解决合并冲突(从A分支切出B分支,B分支开发完后合并到A分支上)

  • git rebase方法:切换到B分支,git rebase A -> 解决冲突 -> git add . -> git rebase --continue -> git push -f
  • 将B分支的修改内容git stash -> git rebase A (将B分支代码更新) -> git stash pop(将B分支修改的内容放出来,此时再合并代码就不会有冲突了)

2. 将多个commit合并为一个

  • B分支有a,b,c三个提交,若想合并a,b两个提交,记下c的commit Id,然后git rebase -i commit Id, 将除了第一个提交信息以外的提交前面标记成 s,修改完后保存即可.

3. 提交之后,改了东西后想重新提交,但不想产生新的提交记录

  • git commit --amend --no-edit
  • git commit --amend -m "new message" 也不会产生新的commit记录

4. A B两个毫无关系的分支,想要将A分支上的一个提交移动到 B 分支上

  • 切换到 A 分支 git log查看这次提交的 commitId. 切换到 B 分支 git cherry-pick commit id 

5. .gitignore不生效:(对于已追踪的文件,是不生效的。)

  • git rm -r --cached . 清除缓存内容
  • git add .
  • git commit -m 'update .gitignore'