DEV Community

Cover image for Painlessly Fix Common Git Mistakes
Jamena McInteer
Jamena McInteer

Posted on

Painlessly Fix Common Git Mistakes

When I first started using Git extensively, I was pretty much able to get away with knowing the basics:

  • git init: start a Git project
  • git clone [github-repo]: Clone a remote repository locally
  • git pull: Get the latest changes from the remote repository
  • git checkout -b [branch-name]: Create a new branch and switch to it
  • git checkout [branch-name]: Switch to a branch
  • git status: See what you have uncommitted, unstaged/staged, untracked
  • git add [some matcher string]: Stage files to commit
  • git commit: Commit your changes to the branch
  • git push origin [branch-name]: Push your changes to the remote repository
  • git merge [branch-name]: Merge another branch into your branch

However, the more I worked with Git the more I ended up making mistakes that I needed to fix (I'm only human after all 🤷‍♀️). Here are a couple of the tricks I've learned to fix these mistakes without panicking.

#1. Worked on the wrong branch and realized it before committing

This is a fairly easy one to fix and I usually catch this issue when running git status before committing my changes, and realizing that I am on the wrong branch. The fix for this is:

  1. git add . - Stage all your changes. If you don't do this step, your new files may not make it into the stash in the next step.
  2. git stash - This adds your uncommitted changes to your stash.
  3. git checkout -b [another-branch] or git checkout [another-branch] - Create a new branch or switch to an existing branch.
  4. git stash pop - Get your stashed changes out of your stash and into the correct branch.

#2. Worked on the wrong branch and committed a couple of times before realizing it, but didn't push to origin

Ok, sometimes I'm just a bit tired and don't realize I'm working on the wrong branch and I end up committing changes a couple of times without realizing it, but haven't pushed those commits to the remote repository yet. Here is how to fix this issue:

  1. git add and then git commit or git stash to preserve any uncommitted changes you may still have.
  2. git checkout -b [another-branch] or git checkout [another-branch] - Create a new branch or switch to an existing branch.
  3. git merge [original-branch] - Merge the changes in the other branch where you DON'T want them to be.
  4. (optional) git stash pop - Get the last set of changes that you stashed away into your new working branch if you used git stash in step 1.
  5. git checkout [original-branch] - Switch to the original branch where you want to get rid of your changes.
  6. git reset --hard origin/[original-branch] - Reset the branch to what is in the remote repository, blowing away all your changes 💥
  7. git checkout [another-branch] - Checkout the branch that now has your changes and keep coding 👩‍💻

#3. Worked on the wrong branch, committed changes, and pushed them to origin

After changes are pushed to origin that shouldn't be there, it's a bit trickier to fix. Hopefully this doesn't happened on master or a staging branch as you can run into issues with other developers having problems or deployments being kicked off 😑

  1. git log - A log of the different commits you've made will show up in the terminal. Go down (arrow down) to the earliest commit you want to fix and copy the ID for that commit. Now hit q to get out of this.
  2. git rebase -i [target-commit] - Use the ID you copied in the previous step. Alternatively, you can do git rebase -i HEAD~2, replacing 2 with the number of latest commits you want to work with. A file should open in your editor displaying the different commits that have occurred on that branch, starting with your target commit. You can add # at the beginning of a commit's line, or replace pick with drop to delete the commit and the changes made by that commit. You can do this with several commits, as well as modify commits with the other commands displayed below the commits. When you're done, save and close the editor.
  3. git rebase --continue - If needed, use this command until the rebase is completed.
  4. git log - This is optional, but you can use this to review the completed rebase before you push to origin.
  5. git push -f origin HEAD - Since the history has changed, you will need to force push to origin.

I hope this is useful! What Git mistakes have you made?

Top comments (0)