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:
It is strongly recommended to that you create a full backup of your data before performing any of the steps in this page
You may be required to add additional commits on top of the latest commit after making some changes
It is also strongly recommended that you perform this on a local repository or on a test server before performing it on production
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: Stash Admin <stash@admin.com>
Date: Sun May 31 12:30:44 2015 +0800
Fourth Commit
commit 1f096bfe7503b2805af64b69c9fe12c059ec9000
Author: Stash Admin <stash@admin.com>
Date: Sun May 31 12:30:30 2015 +0800
Third Commit
commit 1ffce288fa200abbac4d0789dc181b4e2f2f8cbd
Author: Stash Admin <stash@admin.com>
Date: Sun May 31 12:30:20 2015 +0800
Second Commit
commit 0ba19e07c5b4e5b2372a51382b910844c81bbefa
Author: Stash Admin <stash@admin.com>
Date: Sun May 31 12:30:11 2015 +0800
First Commit
- Determine which commit that requires change.
In this example, the third commit with the commit hash of1f096bfe7503b2805af64b69c9fe12c059ec9000
needs to be modified. After that, access the repository via the terminal. 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 of1ffce288fa200abbac4d0789dc181b4e2f2f8cbd
. The command would be :git rebase -i 1ffce288fa200abbac4d0789dc181b4e2f2f8cbd
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
This will allow Git to stop at the Third Commit to make amendments.
- Depending on the type of changes, you can perform the following if you need to change the:
- The author of the commit
Perform:git commit --amend --author="Author Name <email@address.com>"
- The date of the commit
- For current date and time
Perform:git commit --amend --date="$(date -R)"
- For a specific date and time
Perform: {{git commit --amend --date="Thu, 07 Apr 2005 22:13:13 +0200"}}
Note that the Date format must follow the Git standard RFC 2822 or ISO 8601 which is described here
- For current date and time
- The commit message
Perform:git commit --amend -m "New Commit Message"
- The author of the commit
- 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.
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: Stash Admin <stash@admin.com> Date: Sun May 31 12:30:20 2015 +0800 Second Commit commit 0ba19e07c5b4e5b2372a51382b910844c81bbefa Author: Stash Admin <stash@admin.com> Date: Sun May 31 12:30:11 2015 +0800 First Commit
Note that the commit hash for the Third Commit has changed as well
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: Stash Admin <stash@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: Stash Admin <stash@admin.com> Date: Sun May 31 12:30:20 2015 +0800 Second Commit commit 0ba19e07c5b4e5b2372a51382b910844c81bbefa Author: Stash Admin <stash@admin.com> Date: Sun May 31 12:30:11 2015 +0800 First Commit
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.
- Once you're happy with the change, it is time to push the new content to Stash. 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 Stash's counterpart. In order the continue, you would either:
- 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 Stash.
git log
would show like this as per the exampleXyrenus ~/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: Stash Admin <stash@admin.com> Date: Sun May 31 13:11:23 2015 +0800 Merge branch 'master' of https://<repository>/Andrew/repo commit 99d6bc74e3ef03cb5100d6306bf55f9220308976 Author: Stash Admin <stash@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: Stash Admin <stash@admin.com> Date: Sun May 31 12:30:44 2015 +0800 Fourth Commit commit 1f096bfe7503b2805af64b69c9fe12c059ec9000 Author: Stash Admin <stash@admin.com> Date: Sun May 31 12:30:30 2015 +0800 Third Commit commit 1ffce288fa200abbac4d0789dc181b4e2f2f8cbd Author: Stash Admin <stash@admin.com> Date: Sun May 31 12:30:20 2015 +0800 Second Commit commit 0ba19e07c5b4e5b2372a51382b910844c81bbefa Author: Stash Admin <stash@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
Perform a Push with --force flag
git push origin master --force
Note that this will completely overwrite all commits to Stash
Do not everforce
push on a public repository
Do not do this as that can break someone'spull
This would be the least recommended way to push to Stash
Please take a backup copy of your Stash Home Directory should you need to revert back to your previous state
- Have to integrate with the changes with your remote.
- Your new changes should have reflected onto Stash