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:
-
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. -
git stash
- This adds your uncommitted changes to your stash. -
git checkout -b [another-branch]
orgit checkout [another-branch]
- Create a new branch or switch to an existing branch. -
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:
-
git add
and thengit commit
orgit stash
to preserve any uncommitted changes you may still have. -
git checkout -b [another-branch]
orgit checkout [another-branch]
- Create a new branch or switch to an existing branch. -
git merge [original-branch]
- Merge the changes in the other branch where you DON'T want them to be. - (optional)
git stash pop
- Get the last set of changes that you stashed away into your new working branch if you usedgit stash
in step 1. -
git checkout [original-branch]
- Switch to the original branch where you want to get rid of your changes. -
git reset --hard origin/[original-branch]
- Reset the branch to what is in the remote repository, blowing away all your changes π₯ -
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 π
-
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 hitq
to get out of this. -
git rebase -i [target-commit]
- Use the ID you copied in the previous step. Alternatively, you can dogit rebase -i HEAD~2
, replacing2
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 replacepick
withdrop
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. -
git rebase --continue
- If needed, use this command until the rebase is completed. -
git log
- This is optional, but you can use this to review the completed rebase before you push to origin. -
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)