美文网首页Git使用Git
2018-01-11 GIT 修改旧Commit并Push(并非

2018-01-11 GIT 修改旧Commit并Push(并非

作者: 由宇婷 | 来源:发表于2018-01-12 01:35 被阅读36次

注意,请不要master 或者重要branch使用这招

假设我们要给commit2 添加 feature A

1.查看commit2的commit id
git log --oneline

my-Mac:you$ git log --oneline
01420kk commit4
03fc6kk commit3
2c413kk commit2 # this one
06d4kkk commit1
2.保存featureA
3.stage feature A
4.使用特殊Commit
git commit --fixup commitID

my-Mac:you$ git commit --fixup 2c413kk

此时feature A 已经被commit ,如果你再次使用

git log --oneline

你会发现在新增一条commit

my-Mac:you$ git log --oneline
7800kkk fixup commit2  #this is your feature A
01420kk commit4
03fc6kk commit3
2c413kk commit2
06d4kkk commit1
5 将featureA 与 commit2 合并

首先你要确定commit2之前的commitID

my-Mac:you$ git log --oneline
7800kkk !fixup commit2  
01420kk commit4
03fc6kk commit3
2c413kk commit2
06d4kkk commit1 #this one 06d4kkk

将此ID 带入下面的语句中

git rebase -i --autosquash  前一个commitID

例如

git rebase -i --autosquash 06d4kkk

之后会进入vim,确定合并位置
通常会是这样的

pick 2c413kk commit2
^fixup 7800kkk commit2 #this is feature A
pick 06d4kkk commit1

# Rebase 93cf9cb..8626a82 onto 93cf9cb (13 commands)
#
# 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

一般fixup会加在要修改的commit下面,
如果没问题的就可以保存退出。(:wq)
如果看错了就不保存退出,再做一遍rebase(q!)
一般这样就完成了!

如果你输入

git branch 

显示 (no branch rebasing)之类的
输入

git status

查看下一步该做什么,通常使用

git rebase --continue 

即可

6.push到远程branch

注意,这里必须要覆盖,如果是普通的push会产生merge和混乱。

git push -f origin yourbranch

如果你一个不小心,看错了commit id ,可以回滚到之前的操作。

git reflog
git reset --hard Obfafd

用reflog来检查id。
用reset来 回滚。

相关文章

  • 2018-01-11 GIT 修改旧Commit并Push(并非

    注意,请不要master 或者重要branch使用这招 假设我们要给commit2 添加 feature A 1....

  • git push -f 强推

    git commit -m "feat:sonar问题修改" git push -f "origin" mq202...

  • git恢复文件

    恢复已修改未add的文件git checkout 恢复(撤销)已经提交(commit并push)到远端仓库的com...

  • Git 命令 修改已经 commit 但是还没有push的 co

    以下大家应该经常用: 修改已经 commit 但是还没有push的 commit message git comm...

  • git 入门系列(一)

    git学习-commit、push常见问题及解决措施(一) git commit 当本地修改完分支,准备将暂存区的...

  • git 命令

    删除分支命令git分支教程git-flow说明 Git撤销git commit 但是未git push的修改 找到...

  • 0 ---

    .git目录文件夹结构git log命令 本地修改、add、commit、push后撤销git rebase -i...

  • git 修改commit信息

    修改commit信息主要有这几种情况1.刚刚commit,还没有push,使用git commit --amend...

  • git 修改冲突

    :wq//保存并退出修改完后git add .git commit ""

  • git修改已经提交的commit

    git修改已经push过的commit信息 在使用git提交代码时会出现提交信息写错的情况,并且已经push到远程...

网友评论

    本文标题:2018-01-11 GIT 修改旧Commit并Push(并非

    本文链接:https://www.haomeiwen.com/subject/srjxoxtx.html