I initially wrote this article on my Linkedin. But, now I think, it should be a part of this blog.
There are many moments in life when we want to go back and want to amend our mistakes. Life may be or may not give you those chances but fortunately, git gives. ☺ So I am sharing my experiences where I did blunders and came back clean in git.
There are many moments in life when we want to go back and want to amend our mistakes. Life may be or may not give you those chances but fortunately, git gives. ☺ So I am sharing my experiences where I did blunders and came back clean in git.
1. Going back to a previous merge where things were really working: I did some changes in our pom.xml file and upgraded some jar files to the latest one. Suddenly, some of my automation test cases started skipping in execution which impacted the entire execution reports and dashboards. Why that happened is another story but the first thing which I did after that, was to go back to the previous merge, where things were actually working.
git reflog: It gave me the list of all my history in git commits with the reference id. So after getting that, the next thing, which I did, was,
git reset HEAD@{index}
2. Going back to your previous commit and do changes: This I did many times in my initial days. Sometimes I forgot to add one file, sometimes the commit message was not appropriate blah blah. So the rescue command at that time was:
git commit --amend
3. Undo your last commit but keep changes with indexing: My favorite one. Whenever I feel things are going out of my hand because of my silly mistakes, this saved me:
git reset --soft HEAD~1
Following a git commit will create a commit with the exact same changes as the commit "removed" before.
4. Undo your last commit but don’t keep changes with indexing. I use it when I think yes, the previous blunder was a bigger one and I don’t want them even in indexing also☺.I will add them later if I want to in my next commit.
git reset HEAD~1
5. Undo your last commit and discard the changes as well. Never used it but might help someone. ☺
git reset --hard HEAD~1
So you have done bigger messes, and the following changes are of no use for you. Would suggest:
git reset –help
6. Going back to your last commit and push changes to correct branch: So you are new in the team and someone told you to push your changes to some particular branch and you did it meticulously without any of above mistakes. But suddenly, your manager comes and asks you, who told you to push your changes to this branch. Shitty situation. Now what will you do, it's up to you but through git , you can handle it by the below commands:
git reset HEAD~ --soft # undo the last commit but leave the changes available
git stash
git checkout name-of-the-correct-branch # move to the correct branch
git stash pop
git add . or add individual files
git commit -m "your message here" . # Now your changes are on the correct branch
7. The work is done, now delete the branch:
git branch -d temp_branch
Oh, my bad, nothing happens smoothly with me. I simply want to delete this branch but I am getting this message: error: The branch 'temp_branch' is not fully merged. This is just a warning, not an error. And as usual, we never care about warnings. So I used:
git branch -D temp_branch
8. Go back and check which branches are not merged yet:
git log oldbranch ^newbranch --no-merges
9. Everything is messed up and you just want to discard everything and want the latest history from the server and point your local master branch at it:
git fetch origin
git reset --hard origin/master
No comments:
Post a Comment