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.
Monday, February 3, 2020
Modify last (recent) commit in Git
Tags
# amend
# commands
About Atul Singh
I am a Data Consultant at a Canadian financial firm. My keen interests varies from Data Analytics, ML, Kubernetes, NLP to ETL.
I love to blog and travel in my spare time. If you’d like to get in touch, feel free to say hello through any of the social links.
Learning
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment