git常用命令

105 阅读3分钟

修改历史提交

  • 修改最近一次提交

git commit --amend 会将当前修改追加到最近一次提交中

git reset --soft [commitId]

  • 修改历史提交

目标:修改最近的第3个提交,将“fix:1123 修改文案”中的1123改为1314 查看log信息

git log --pretty=oneline

03d5035007108eb70b7ab9b89f7ba31895013b3e (HEAD -> git) 初始化;
cd555862f212f9962aa13b40770e93d24a928f5a 增加-玩家统计-““积分业绩字段
4adcd7717455f3babf48bb0cdad7f067ff8441a5 fix:1123 修改文案

使用命令:git rebase -i HEAD~3,会出现以下内容;我们是需要修改

pick 4adcd771 fix:1123 修改文案
pick cd555862 增加-玩家统计-““积分业绩字段
pick 03d50350 初始化;
# Rebase 29439bf6..03d50350 onto 29439bf6 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# 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

“fix:1123 修改文案”是我们需要修改的目标,将此条文案前面的pick修改为edit,退出保存;

再输入: git commit --amend 进入界面:

fix:1123 修改文案
# ....省略

# 修改后的
fix:1314 修改文案

退出保存,使用命令:git rebase --continue 完成修改;

变基的功能

  1. 整合分叉
  2. 修改历史提交(git rebase -i HEAD~3
  3. 排除x分支log变基(git rebase --onto master server clien,在clien分支上相对于server新增的log,在master上重放一遍) 4.不切分支变基:now branch server: git rebase master server

重置揭秘

  • 三棵树

HEAD:上一次提交的快照,下一次提交的父节点

Index:预期的下一次提交的快照(即暂存区的快照)

Working Directory:沙盒(工作目录)

  • git reset [--xxx] [commitId]
  1. --soft:移动HEAD指向commitId(撤销掉的commit修改都在暂存区)
  2. --mixed无--参数:移动HEAD指向commitId,并使Index看起来像HEAD(撤销掉的commit修改都在工作目录)
  3. --hard:移动HEAD指向commitId,并使Index看起来像HEAD,还使Working Directory看起来像HEAD(车削掉的修改被删除)
  • git reset commitId fileName git reset HEAD^^ fileName:HEAD不移动,使Index像commitId fileName,使Working Directory像HEAD

  • 压缩提交 将3个提交压缩为1个提交

# 压缩前2个提交

## 第1种方式
git reset --soft HEAD^^^
git commit -m 'xxxxx'

## 第2种
git reset --soft HEAD^^
git commit --amend