My scrapbook about anything which I learned or want to remember, Sometime about tech tips, thoughts and rambling. If you find anything useful don't forget to give thumbs-up :)

Breaking

Monday, February 3, 2020

Modify last (recent) commit in Git

There are lots of way to achieve it and we are going to discuss them one by one with the reasoning why we should use them or avoid in what scenario.
The very first options which we have is 'commit --amend', so let's see how to use this.

Scenario #1:
When we can made a last commit in local (not yet pushed to remote git repo) and wanted to modify that.

# Git add and commit the change
git add Readme
git commit -m "Adding Readme file"

Now, we realized that we missed few things to add in this commit, so let's add those files or changes -

# Adding few more changes to git
echo "This is a Readme file  " >> Readme
git add Readme SomeOtherFile
git commit -m "Adding Readme and SomeOtherFile" --amend

If you are ok with the changes, now we can push this commit to remote repo by executing below push command.

# Git push
git push -u origin master

Scenario #2:
What if you have already pushed the commit to remote and want to amend it, You can do that by executing commit with amend option but you have to force the push in this case.

# Git force push
git push -f origin master

Only the thing which we need to care is that inform the team before making any amends in last commit so that they can pull the change in their working directory.


No comments:

Post a Comment

Disclaimer

The postings on this site are my own and don't necessarily represent IBM's or other companies positions, strategies or opinions. All content provided on this blog is for informational purposes and knowledge sharing only.
The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of his information.