技术咨询、项目合作、广告投放、简历咨询、技术文档下载 点击这里 联系博主

此文列举出一些工作中经常使用的 git 操作

# git reset & revert

  • git reset --hard commitId : 代码彻底回滚到指定的commit,中途编写的代码将会清除;
  • git reset --soft commitId : 代码回滚到指定的commit,中途编写的代码还会存在,只是显示等待提交;
  • git revert commitId: 重新做某一个commit的内容,在我们原始的提交之中,会创建了一个新的 commit 提交,而此时我们对于想重做的某个 commit 提交的内容都不存在了

# git rebase / git merge

  • git rebase master: 将提交记录合并为一条直线

# 如何更新 git message

日常工作中我们经常会遇到,自己已经提交的代码 message 编写有误,需要重新更改,本文就列出一些常用的更改 message 的方式.

  1. 更改上一次的提交信息
git commit --amend

然后会进入一个文本编辑器界面,修改 commit 的描述内容,然后:wq,即可完成操作。

  1. rebase 修改任意提交历史位置的 commit

使用 rebase 的方式修改提交记录,首先 使用 git log 查看所有的提交记录

# 查看所有的提交记录
git log

# rebase到指定的commitId,此处假设要修改最后提交的三次记录

git rebase -i HEAD~3

将会得到如下的信息,这里的提交日志是和git log倒叙排列的,我们要修改的日志信息位于第一位.

  1 pick 2275781 feat: should find method from parent
  2 pick 223fc80 feat: unit test case
  3 pick 9ac1179 feat: update test case
  4
  5 # Rebase 79db0bd..9ac1179 onto 79db0bd (3 commands)
  6 #
  7 # Commands:
  8 # p, pick = use commit
  9 # r, reword = use commit, but edit the commit message
 10 # e, edit = use commit, but stop for amending
 11 # s, squash = use commit, but meld into previous commit
 12 # f, fixup = like "squash", but discard this commit's log message
 13 # x, exec = run command (the rest of the line) using shell
 14 # d, drop = remove commit
 15 #
 16 # These lines can be re-ordered; they are executed from top to bottom.
 17 #
 18 # If you remove a line here THAT COMMIT WILL BE LOST.
 19 #
 20 # However, if you remove everything, the rebase will be aborted.
 21 #
 22 # Note that empty commits are commented out

我们现在要修改修改要feat: update test casefeat: unit test case这条日志,那么修改的日志为,将对应的 pick 修改为 edit, 然后 :wq退出.

  1 pick 2275781 feat: should find method from parent
  2 edit 223fc80 feat: unit test case
  3 edit 9ac1179 feat: update test case

将会看到如下信息,意思就是如果要改日志,执行git commit --amend,如果修改完成后,执行git rebase --continue

client_java git:git rebase -i HEAD~3
Stopped at 2275781...  feat: should find method from parent
You can amend the commit now, with

  git commit --amend

Once you are satisfied with your changes, run

  git rebase --continue
➜  client_java git:(2275781)
  • 然后输入git commit --amend进入修改feat: update test case的 message,feat: update test case修改完成之后:wq退出,
  • 输入git rebase --continue继续修改下一个;
  • 输入git commit --amend进入修改feat: unit test case的 message,修改完成之后:wq退出,
  • 输入git rebase --continue继续修改下一个;
  1. cherry-pick 修改任意提交历史位置的 commit

在网上很多的方式是使用git rebase -i commitId变基到指定的版本,但是在比较复杂的情况处理可能并非如你所愿,最好的方式是基于 Master 重新 checkout 出一个新的干净分支,然后使用 cherry-pick 的方式将要提交的记录挑拣到干净分支


git checkout master
git fetch public # 或者使用 git fetch origin
git checkout -b feat_xx_xx #换成自己的名字
# 执行挑拣操作,建议使用一些辅助工具进行挑拣,当然也可以使用命令行的方式
git cherry-pick commitId # 挑拣指定的提交记录
# 如果顺利的话,就可以正常提交了。如果遇到冲突,使用 git diff 解决冲突即可,工作中,不推荐手工解决冲突,最好还是使用一些 diff 工具来处理,毕竟手工处理容易出错。

# git cherry-pick <start-commit-id>…<end-commit-id> 可以连续pick多个

# cherry-pick之后如果当前commit需要修改message就使用

git commit --amend

# 上述命令会进入编辑页面,修改message,然后esc+:wq


# 如此循环往复,直到所有的都已经修改,最后再push

# 如何合并提交多次的记录

在使用 Git 作为版本控制的时候,我们可能会由于各种各样的原因提交了许多临时的 commit,而这些 commit 拼接起来才是完整的任务。那么我们为了避免太多的 commit 而造成版本控制的混乱,通常我们推荐将这些 commit 合并成一个。

# 1. 查看提交历史,git log

首先你要知道自己想合并的是哪几个提交,可以使用 git log 命令来查看提交历史,假如最近 4 条历史如下:

commit 3ca6ec340edc66df13423f36f52919dfa3......

commit 1b4056686d1b494a5c86757f9eaed844......

commit 53f244ac8730d33b353bee3b24210b07......

commit 3a4226b4a0b6fa68783b07f1cee7b688.......

历史记录是按照时间排序的,时间近的排在前面。

# 2. git rebase

想要合并 1-3 条,有两个方法

  1. 从 HEAD 版本开始往过去数 3 个版本
git rebase -i HEAD~3

2.指名要合并的版本之前的版本号

git rebase -i 3a4226b

请注意 3a4226b 这个版本是不参与合并的,可以把它当做一个坐标

# 3. 选取要合并的提交

  1. 执行了 rebase 命令之后,会弹出一个窗口,头几行如下:
pick 3ca6ec3 'commit记录'

pick 1b40566 'commit记录'

pick 53f244a 'commit记录'
  1. pick 改为 squash 或者 s,之后保存并关闭文本编辑窗口即可。改完之后文本内容如下:
pick 3ca6ec3 'commit 记录'

squash 1b40566 'commit 记录'

squash 53f244a 'commit 记录'
  1. 然后保存退出,Git 会压缩提交历史,如果有冲突,需要修改,修改的时候要注意,保留最新的历史,不然我们的修改就丢弃了。修改以后要记得敲下面的命令:
git add .

git rebase --continue

如果你想放弃这次压缩的话,执行以下命令:

git rebase --abort
  1. 如果没有冲突,或者冲突已经解决,则会出现如下的编辑窗口:

# This is a combination of 4 commits.

#The first commit’s message is:  
注释......

# The 2nd commit’s message is:

注释......

# The 3rd commit’s message is:

注释......

# Please enter the commit message for your changes. Lines starting # with ‘#’ will be ignored, and an empty message aborts the commit.
  1. 输入 wq 保存并推出, 再次输入 git log 查看 commit 历史信息,你会发现这两个 commit 已经合并了。

# branch管理

  • git branch -D [branchName] 删除本地分支
  • git push origin -D [branchName] 删除远端分支
  • git checkout -b feat/sass-v1 origin/feat/sass-v1 克隆远端分支feat/sass-v1到本地
  • git checkout -b feat/saas-0817 从当前分支新建一个分支feat/saas-0817
  • git merge [branchName] 将branchName合并到当前分支
  • git merge [branchName] --squash 将branchName合并到当前分支,并将branchName上的所有提交合并成一次提交

# rebase branch

  • git pull --rebase origin [branchName] = git fetch + git rebase
// 假设当前分支dev, commit 为 a b c d e
// 假设master分支, commit 为 a b f g h
git pull --rebase origin master
// 当前分支dev commit 变为 a b c d e f g h

  • git rebase master
// 假设当前分支dev, commit 为 a b c d e
// 假设master分支, commit 为 a b f g h
git rebase origin/master
// 当前分支dev commit 变为 a b f g h c d e

# commit

  • git commit -m "msg" --no-verify 强制提交不检查
  • git push -f 强制提交代码并以本地版本代码为主覆盖远程 git push -f是不安全的,git push --force-with-lease更安全,注意--force-with-lease失败后再执行一次也会强制提交覆盖

# reset回退

  • git log 查看提交日志
  • git reset 将所有暂存区回退到工作区
  • git checkout . 丢弃工作区所有的更改
  • git reset --hard [commit hash] 将从commithash(不包括此hash)之后的丢弃
  • git reset --hard 将暂存区、工作区所有内容丢弃
  • git reset --soft [commit hash] 将从commithash(不包括此hash)之后的提交回退到暂存区
  • git reset --soft HEAD~4 回退最近4次提交到暂存区

# cherry-pick 复制提交

场景:当你在merge或者rebase的时候发现冲突太多了,想哭的时候,可以用原分支check目标分支处理,然后再cherry-pick当前分支的每个提交,这样冲突就会少很多。或者另一分支上有些代码还没有merge到master,但是你当前分支又非要用的时候,就可以cherry-pick过来一份。

  • git cherry-pick [commit hash] 将其他分支上已提交的commit在当前分支再提交一次,产生新的commithash

# --patch 挑选指定分支的文件合并到当前分支

git checkout --patch 源分支 文件相对路径(/xxxx.txt)

# revert

  • git revert [commit hash] 非merge的commit
  • git revert -m [1|2] [commit hash] merge类型的commit
【未经作者允许禁止转载】 Last Updated: 2/4/2024, 6:06:40 AM