How do you make changes on a specific commit

お困りですか?

アトラシアン コミュニティをご利用ください。

コミュニティに質問

In some unusual cases, whereby a specific commit have to be modified to correct problems when running git fsck on a repository. On the other hand, it may be due to mistakes that were made after committing a change. Before moving on, please be aware of the following items:

 

(warning) As this is an unsupported procedure, performing the following is at your risk. Therefore, it is strongly recommended to that you create a full backup of your data before performing any of the steps in this page

(warning) You may be required to add additional commits on top of the latest commit after making some changes

(warning) It is also strongly recommended that you perform this on a local repository or on a test server before performing it on production

(warning) Avoid attempting to perform the following if it is not required

 

To perform a change on a specific commit, this KB will use the following output of this git log as an example to demonstrate:

Xyrenus ~/Desktop/repo_sample (master)
$ git log
commit 3b0065e6a00729f3ee753c162b545f101feb7277
Author: Bitbucket Server Admin <bitbucket@admin.com>
Date:   Sun May 31 12:30:44 2015 +0800
    Fourth Commit

commit 1f096bfe7503b2805af64b69c9fe12c059ec9000
Author: Bitbucket Server Admin <bitbucket@admin.com>
Date:   Sun May 31 12:30:30 2015 +0800
    Third Commit


commit 1ffce288fa200abbac4d0789dc181b4e2f2f8cbd
Author: Bitbucket Server Admin <bitbucket@admin.com>
Date:   Sun May 31 12:30:20 2015 +0800
    Second Commit


commit 0ba19e07c5b4e5b2372a51382b910844c81bbefa
Author: Bitbucket Server Admin <bitbucket@admin.com>
Date:   Sun May 31 12:30:11 2015 +0800
    First Commit

 

  1. Determine which commit that requires change. 
    In this example, the third commit with the commit hash of 1f096bfe7503b2805af64b69c9fe12c059ec9000 needs to be modified. After that, access the repository via the terminal.

  2. Specify a rebase by identifying an earlier commit to the one that you've chosen above and perform git rebase -i <Earlier Commit>.
    In this example, the earlier commit would be the second commit with the commit hash of 1ffce288fa200abbac4d0789dc181b4e2f2f8cbd. The command would  be : git rebase -i 1ffce288fa200abbac4d0789dc181b4e2f2f8cbd
     

  3. This will open up a text editor and you would need to find the commit that needs changing and on the same line, replace the word *pick* to *edit* for that specific commit
    In this example, the next editor will output the following:

    pick 1f096bf Third Commit
    pick 3b0065e Fourth Commit

    Since we are interested in changing the commit for 1f096bf

    edit 1f096bf Third Commit
    pick 3b0065e Fourth Commit
  4. This will allow Git to stop at the Third Commit to make amendments.
     

  5. Depending on the type of changes, you can perform the following if you need to change the:

    1. The author of the commit
      Perform: git commit --amend --author="Author Name <email@address.com>" 

    2. The date of the commit
      1. For current date and time
        Perform: git commit  --amend --date="$(date -R)"
         
      2. For a specific date and time
        Perform:  {{git commit  --amend --date="Thu, 07 Apr 2005 22:13:13 +0200"}}
        (warning) Note that the Date format must follow the Git standard RFC 2822 or ISO 8601 which is described here
         
    3.  The commit message
      Perform: git commit --amend -m "New Commit Message"

  6.  After performing any of the above, a text editor will show up again. This is allow you to change the commit message if needed. Otherwise, just save it.
     
  7. Performing git log will show you the changes that you have made on the commit as the latest commit.

    Xyrenus ~/Desktop/repo_sample (master|REBASE-i 1/2)
    $ git log
    commit a285de0f648dd807dda329e2d2079ef20cf59118
    Author: Andrew Er <andrew@test.com>
    Date:   Sun May 31 12:30:30 2015 +0800
        Third Commit - Changed Author
    
    commit 1ffce288fa200abbac4d0789dc181b4e2f2f8cbd
    Author: Bitbucket Server Admin <bitbucket@admin.com>
    Date:   Sun May 31 12:30:20 2015 +0800
        Second Commit
    
    commit 0ba19e07c5b4e5b2372a51382b910844c81bbefa
    Author: Bitbucket Server Admin <bitbucket@admin.com>
    Date:   Sun May 31 12:30:11 2015 +0800
        First Commit

    (info) Note that the commit hash for the Third Commit has changed as well

     

  8. To complete the rebase and to restore back any other existing commits, simply execute git rebase --continue

    Xyrenus ~/Desktop/repo_sample (master|REBASE-i 1/2)
    $ git rebase --continue
    Successfully rebased and updated refs/heads/master.
    Xyrenus ~/Desktop/repo_sample (master)
    $ git log
    
    commit 99d6bc74e3ef03cb5100d6306bf55f9220308976
    Author: Bitbucket Server Admin <bitbucket@admin.com>
    Date:   Sun May 31 12:30:44 2015 +0800
        Fourth Commit
    
    commit a285de0f648dd807dda329e2d2079ef20cf59118
    Author: Andrew Er <andrew@test.com>
    Date:   Sun May 31 12:30:30 2015 +0800
        Third Commit - Changed Author
    
    commit 1ffce288fa200abbac4d0789dc181b4e2f2f8cbd
    Author: Bitbucket Server Admin <bitbucket@admin.com>
    Date:   Sun May 31 12:30:20 2015 +0800
        Second Commit
    
    commit 0ba19e07c5b4e5b2372a51382b910844c81bbefa
    Author: Bitbucket Server Admin <bitbucket@admin.com>
    Date:   Sun May 31 12:30:11 2015 +0800
        First Commit

    (info) Also note the commit hash after modified commit has been changed as well. In this example, the Fourth commit's hash has also been modified.
     

  9. Once you're happy with the change, it is time to push the new content to Bitbucket Server. There are two ways about this because this is a non-fast-forward push as the tip of the current branch on your local repository is behind Bitbucket Server's counterpart. In order the continue, you would either:
    1. Have to integrate with the changes with your remote.
      • This will cause the repository to have duplicate of additional commits
      • For this, you would need to perform a git pull. Only then, you can push the new content to Bitbucket Server.
      • git log would show like this as per the example

        Xyrenus ~/Desktop/repo_sample (master)
        $ git pull
        Merge made by the 'recursive' strategy.
        
        
        Xyrenus ~/Desktop/repo_sample (master)
        $ git log
        
        commit 09db367da8147cf42065caa945a85ec829ff312b
        Merge: 99d6bc7 3b0065e
        Author: Bitbucket Server Admin <bitbucket@admin.com>
        Date:   Sun May 31 13:11:23 2015 +0800
            Merge branch 'master' of https://<repository>/Andrew/repo
        
        commit 99d6bc74e3ef03cb5100d6306bf55f9220308976
        Author: Bitbucket Server Admin <bitbucket@admin.com>
        Date:   Sun May 31 12:30:44 2015 +0800
            Fourth Commit
        
        commit a285de0f648dd807dda329e2d2079ef20cf59118
        Author: Andrew Er <andrew@test.com>
        Date:   Sun May 31 12:30:30 2015 +0800
            Third Commit - Changed Author
        
        commit 3b0065e6a00729f3ee753c162b545f101feb7277
        Author: Bitbucket Server Admin <bitbucket@admin.com>
        Date:   Sun May 31 12:30:44 2015 +0800
            Fourth Commit
        
        commit 1f096bfe7503b2805af64b69c9fe12c059ec9000
        Author: Bitbucket Server Admin <bitbucket@admin.com>
        Date:   Sun May 31 12:30:30 2015 +0800
            Third Commit
        
        commit 1ffce288fa200abbac4d0789dc181b4e2f2f8cbd
        Author: Bitbucket Server Admin <bitbucket@admin.com>
        Date:   Sun May 31 12:30:20 2015 +0800
            Second Commit
        
        commit 0ba19e07c5b4e5b2372a51382b910844c81bbefa
        Author: Bitbucket Server Admin <bitbucket@admin.com>
        Date:   Sun May 31 12:30:11 2015 +0800
            First Commit
         
        Xyrenus ~/Desktop/repo_sample (master)
        $ git push origin master
        
        Counting objects: 6, done.
        Delta compression using up to 4 threads.
        Compressing objects: 100% (4/4), done.
        Writing objects: 100% (4/4), 570 bytes | 0 bytes/s, done.
        Total 4 (delta 2), reused 0 (delta 0)
        To https://<repository>/Andrew/repo.git
           3b0065e..09db367  master -> master
    2. Perform a Push with --force flag

      git push origin master --force

      (warning) Note that this will completely overwrite all commits to Bitbucket Server
      (warning) Do not ever force push on a public repository
      (warning) Do not do this as that can break someone's pull
      (warning) This would be the least recommended way to push to Bitbucket Server
      (warning) Please take a backup copy of your Bitbucket Server Home Directory should you need to revert back to your previous state

     

  10. Your new changes should have reflected onto Bitbucket Server

 

最終更新日: 2015 年 10 月 15 日

この内容はお役に立ちましたか?

はい
いいえ
この記事についてのフィードバックを送信する
Powered by Confluence and Scroll Viewport.