git book

  • 撤销上一次所有add

git reset详解

  1. git reset --soft|mixed|hard
  2. --soft
  3. Does not touch the index file or the working tree at all (but resets the head to <commit>, just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it. 切换到另一次commit,该commit之后的修改都被放在暂存区
  4. --mixed
  5. Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.切换到另一次commit,该commit之后的修改都被放在工作目录
  6. --hard
  7. Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded. 切换到另一次commit,该commit之后的修改都被舍弃
  • 删除分支
  1. //删除本地分支
  2. git branch -d dev_20181018
  3. //删除远程分支
  4. git push origin --delete dev
  • 取消跟踪已跟踪的文件
  1. git rm --cached readme.txt
  • git 添加远程仓库
  1. git remote add <远程仓库名> <URL>
  • git 删除远程仓库
  1. git remote remove origin
  2. //重命名
  3. git remote rename origin test
  • git clone 特定仓库
  1. git clone -b <分支名> <远程仓库地址>
  • 显示远程分支
  1. git ls-remote <remote>
  2. git remote show <remote>
  • 推送分支
  1. //来将本地的 serverfix 分支推送到远程仓库上的 awesomebranch 分支。
  2. git push origin serverfix:awesomebranch
  3. git push <remote> <branch>:
  • 打标签

ag的位置是固定的,在给指定提交打好标签以后,它就固定于此位置。 分支的位置会不断变动的,随着分支的向前推移或者向后回滚,都在不断变化。

  1. //-m 选项指定了一条将会存储在标签中的信息。
  2. //如果没有为附注标签指定一条信息,
  3. //Git 会启动编辑器要求你输入信息。
  4. git tag -a v1.4 -m "my version 1.4"
  5. 后期打标签
  6. 你也可以对过去的提交打标签。
  7. git tag -a v1.2 9fceb02
  8. //标签推送至远端
  9. //将本地v1.0的tag推送到远端服务器
  10. git push origin v1.0
  11. //push所有tag,命令格式为:
  12. git push [origin] --tags
  • git比较分支提交的不同
  1. //commit 后面的箭头,根据我们在 –left-right dev…master 的顺序,
  2. //左箭头 < 表示是 dev 的,右箭头 > 表示是 master的
  3. git log --left-right dev...master

git cherry-pick有冲突解决办法

一般情况下,直接使用cherry-pick产生的冲突只是因为文件重复编辑造成的,可以直接手动解决冲突即可,但是有时候A分支的patch_01依赖了其它的patch,但是你仅仅想cherry_pick patch_01的更改到分支B,如果直接使用cherry-pick, git 会默认个其依赖的patch也cherry-pick到分支B.可以使用如下方法解决:

  1. ## cherry-pick到其他分支
  2. 假设打算同步dev-brd-20q3分支的代码至prod-nome2-skywell-2020-06分支,约定好的同步topic为/sync_patch
  3. 1. 定位到patches所在的模块
  4. 2. 输入指令:git format-patch remotes/origin/prod-nome2-skywell-2020-06...remotes/origin/dev-brd-20q3
  5. 3. 此时会产生分离后所有的patches(不是不同的patches),需要找到上次同步的点和之后需要同步的patches,如:
  6. 0687-Bug-36832-Fix-bug-of-update-section-to-next.patch
  7. 4. 切换到需要同步的分支prod-nome2-mz-2020-06
  8. git checkout -b prod-nome2-skywell-2020-06 origin/prod-nome2-skywell-2020-06
  9. 5. 输入指令:git am 0687-Bug-36832-Fix-bug-of-update-section-to-next.patch
  10. a. 如果发生冲突
  11. git status
  12. git apply --reject 0687-Bug-36832-Fix-bug-of-update-section-to-next.patch
  13. b .找到*.rej,解决这些文件里提示的冲突;
  14. git add rejected_files
  15. git am --resolved
  16. c. 如果已经cherry-pick
  17. git am --skip
  18. 6. 同步到分支,输入指令:
  19. git push origin HEAD:refs/for/prod-nome2-skywell-2020-06/V2X_V2V-prod-nome2-skywell-2020-06
  20. # 注意:
  21. 1. 若从同步点到需要的pahch的中间还有其他的patches,如果直接插入需要的patch,中间可能会出错,此时,需要把中间依赖的patches依次插入进去。

git 更改某个提交内容或

将当前改动追加到某次commit 上

  1. 尤其适用于gerrit 上的CL submit 之前,有多个patch时的多个功能点并行开发。
  2. rebase 前,可以创建一个分支,用于rebase:
  3. git checkout -b rebase_tmp
  4. 直接更改某次提交(改动某个指定的commit)
  5. HEAD移到需要更改的commit上:
  6. git rebase -i 0bdf89^
  7. 找到需要更改的commit, 将行首的pick改成edit, esc, 输入:wq退出
  8. (弹出的交互式界面中显示的commit 信息,与git log 显示的顺序相反,即父节点在上显示)
  9. 更改文件
  10. 使用git add 改动的文件添加改动文件到暂存
  11. 使用git commit --amend追加改动到第一步中指定的commit
  12. 使用git rebase --continue移动HEAD到最新的commit
  13. 解决冲突:
  14. 编辑冲突文件, 解决冲突
  15. git add .
  16. git commit --amend
  17. 解决冲突之后再执行git rebase --continue
  1. 将工作空间中的改动追加到某次提交上
  2. **note:**第0步和第2步(加粗)与上面步骤不同,其余步骤相同
  3. 保存工作空间中的改动git stash
  4. HEAD移到需要更改的commit上:
  5. git rebase f744c32^ --interactive
  6. 找到需要更改的commit, 将行首的pick改成edit, esc, 输入:wq退出
  7. 执行命令git stash pop
  8. 使用git add 改动的文件添加改动文件到暂存
  9. 使用git commit --amend追加改动到第一步中指定的commit
  10. 使用git rebase --continue移动HEAD到最新的commit
  11. 解决冲突:
  12. 编辑冲突文件, 解决冲突
  13. git add .
  14. git commit --amend
  15. 解决冲突之后再执行git rebase --continue