I’ve inadvertently become the go-to guy in the office for git ‘accidents’, although as complex as the situation seems it’s usually just a quick command or two to get things back on track. So we’ve outlined a few common problems in the hope it may get someone out of trouble one day!
THE “I MERGED AND PUSHED THE WRONG BRANCH TO REMOTE BY ACCIDENT” MISTAKE.
Ok, so you've got lots of jobs on the go, your under-caffeinated brain picks the incorrect branch in the merge and before you've fully woken up, you’ve pushed the merge to the master branch. Don’t panic, it’s an easy two step fix:
Step 1: Reset your local branch. You want to do this with the ‘hard’ flag to ensure you completely undo your mistake. To do this, you'll need the last commit ID before you made your merge (GIT GUI’s or bitbuckets/github online service really help with this). Then run:
git reset --hard #commit-id#
Step 2: You need to force this change on the remote server. To do so, run:
git push origin master --force
THE “FILES ARE APPEARING IN MY WORKING TREE THAT I HAVEN’T CHANGED” PROBLEM.
Often installers, compilers, task runners, etc. will overwrite files and despite the contents of these files not changing, they show up in the working tree. At this point it may be worth mentioning that you can play around with the settings of your git configuration to avoid this happening completely. But a quick way to only commit the updated files is to run:
git add -uv
This should only commit the updated files of your project and simply no longer show the other files.
THE “I ADDED IT TO MY GITIGNORE BUT IT’S STILL COMING UP” ISSUE.
Ok so git is a bit like your ex, it doesn’t like to give things up. Once it’s tracked a file, it won’t easily just forget about it. For example, you have a file such as design.psd, which you accidentally committed to your repository, every subsequent change to this file will show up in your working tree, even if you have added *.psd to your .gitignore file. So to tell git to forget about this file, or any other newly ignore files, run:
# Remove all cached files to ensure there are no .gitignore files being tracked git rm --cached -r . # Track the files that should be tracked git add -A
THE “I COMMITTED TO THE WRONG BRANCH”.
The solution for this issue is similar to the first fix, although the difference is we need to ensure we don't lose the changes.
Step 1: Reset your local branch to the commit before. Note, that we are using the
mixed flag here instead of the
hard flag. This ensures we don't lose the changed files, they are simply returned to the working tree.
git reset --mixed #commit-id#
Step 2: If you pushed your changes to remote, reset this as we did with fix #1.
git push origin #branch# --force
Step 3: Change to the correct branch and commit changes as you usually would.
git checkout #branch# git add -A git commit -m "some message"
If you’re anything like us, you will have hit the above scenarios a time or two, so we hope next time it arises you're slightly better prepared.