美文网首页Git
Git update a specific commit

Git update a specific commit

作者: JaedenKil | 来源:发表于2019-05-23 17:53 被阅读2次

    Current commits:

    $ git log --oneline
    50751d6 (HEAD -> master) Add file03
    37dfeb1 Add file02
    01f470d Add file01
    

    Assume I need to update the commit 37dfeb1.

    $ echo "Some changes to file02" >> file02
    
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
            modified:   file02
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    • Step 1: Stash the changes
    $ git stash
    warning: LF will be replaced by CRLF in file02.
    The file will have its original line endings in your working directory.
    Saved working directory and index state WIP on master: 50751d6 Add file03
    
    $ git show stash
    commit ebe8d7a79186a36d34e2e778a0f29e43e59879ed (refs/stash)
    Merge: 50751d6 8bb2954
    Author: xxx
    Date:   xxx
    
        WIP on master: 50751d6 Add file03
    
    diff --cc file02
    index 9e22bcb,9e22bcb..ee96a91
    --- a/file02
    +++ b/file02
    @@@ -1,1 -1,1 +1,2 @@@
      02
    
    • Step 2: Rebase to the commit to be updated:
    $ git rebase -i 37dfeb1
    
    • Step 3: In the vim editor, change pick to edit
      pick 507551d6 xxx -> edit 507551d6 xxx.
    • Step 4: Take a look at the status:
    $ git status
    interactive rebase in progress; onto 37dfeb1
    Last command done (1 command done):
       edit 50751d6 Add file03
    No commands remaining.
    You are currently editing a commit while rebasing branch 'master' on '37dfeb1'.
      (use "git commit --amend" to amend the current commit)
      (use "git rebase --continue" once you are satisfied with your changes)
    
    • Step 5: Pop the stash
    $ cat file02
    02
    
    $ git stash pop stash@{0}
    interactive rebase in progress; onto 37dfeb1
    Last command done (1 command done):
       edit 50751d6 Add file03
    No commands remaining.
    You are currently editing a commit while rebasing branch 'master' on '37dfeb1'.
      (use "git commit --amend" to amend the current commit)
      (use "git rebase --continue" once you are satisfied with your changes)
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
            modified:   file02
    
    no changes added to commit (use "git add" and/or "git commit -a")
    Dropped stash@{0} (ebe8d7a79186a36d34e2e778a0f29e43e59879ed)
    
    $ cat file02
    02
    Some changes to file02
    
    • Step 6: Save the changes
    $ git add file02
    
    $ git commit --amend --no-edit
    [detached HEAD 2806b3e] Add file03
     Date: Thu May 23 17:27:08 2019 +0800
     2 files changed, 2 insertions(+)
     create mode 100644 file03
    
    $ git status
    interactive rebase in progress; onto 37dfeb1
    Last command done (1 command done):
       edit 50751d6 Add file03
    No commands remaining.
    You are currently editing a commit while rebasing branch 'master' on '37dfeb1'.
      (use "git commit --amend" to amend the current commit)
      (use "git rebase --continue" once you are satisfied with your changes)
    
    nothing to commit, working tree clean
    
    • Step 7: Continue the rebase
    $ git rebase --continue
    Successfully rebased and updated refs/heads/master.
    
    $ git status
    On branch master
    nothing to commit, working tree clean
    
    $ git log --oneline
    2806b3e (HEAD -> master) Add file03
    37dfeb1 Add file02
    01f470d Add file01
    
    $ cat file02
    02
    Some changes to file02
    
    Alternatively we can update the changes after git rebase -i 37dfeb1, without using git stash.

    Bear in mind, after the updated commit, all further commits' SHA hash value will be changed.

    相关文章

      网友评论

        本文标题:Git update a specific commit

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