Git tips & common pitfalls

Git is one of the most helpful version control systems out there. It contains many features and capabilities that can make your life a lot easier.

In this article, we will go through the most useful tips and tricks that you can use in your projects.

I accidently commited my code on the wrong branch! what should I do?

True Stories Omg GIF

The easy way (when you don’t have a branch yet):

git branch my-branch # Move this code to a new branch
git reset --hard HEAD~ # Undo this commit
git checkout my-branch # Now move to the new created branch with this code

If you have a specific branch you want to move this commit into, you can do the following:

git reset --soft HEAD~ # Undo this commit but leave the files
git stash # Store this files on the side
git checkout my-branch # Now move to your branch
git stash pop
git commit -m "commit with the missing files"

But I have already pushed my code! What should I do? Well, in this case, this will only remove your local repository commit, but not on the remote repository.

To do so, you will have to have “force” permissions to the branch you are working on. This can be dangerous as if others are working on this branch you can remove their commits as well.

If you are the only one working on this branch, then you can push it and remove the last commit with the following command:

# If your branch is called 'feature/my-branch' and you want to push the removed commit to that branch, you will have to force push it
git push origin feature/my-branch --force # This is dangerous! read the paragraph above 👆before doing so

How do I remove all of the branches except the one that I’m currently working on?

Phone Reaction GIF by Robert E Blackmon

You can simply remove all of the branches on your local repository with the following command:

git branch | xargs git branch -D # This will give you a list of all branches which will remove each branch using the 'git branch -D' command

This will give you an error, that’s alright the error is regarding the branch you are currently working on that cannot be deleted.

How do I checkout\get a specific file from another branch to my local branch?

Shake It Dancing GIF by DreamWorks Animation

This one is easy:

git checkout feature/messy-branch ./src/file-i-need.js # Checks out a file from the branch feature/messy-branch called 'file-i-need.js'

I accidently commited the wrong files! how do I remove them?

Tom Segura Comedy GIF by Netflix Is a Joke

Let’s say we committed two files on our last commit:

  1. good_file.txt
  2. bad_file.txt

And you want to remove the bad_file.txt from your last. commit. We will first have to remove the last commit (but leave the files as is) and then remove the files we don’t want from the staging area.

Here are the commands:

git reset --soft HEAD~ # Undo the last commit and move the files back to staging
git restore --staged bad_file.txt # Remove the bad file from the staging area
git commit -m "my new commit" # Create a new commit

How do I change my last commit message?

Well this one is easy:

git commit --amend # This will prompt up a window where you can edit your last message in the form of vi

When merging, I want only to have one single commit, how do I do that?

You can easily do that when merging by using squash:

git merge --squash feature/my-branch # This will merge all of the files but leaves you to create a new commit

Leave a Reply

Your email address will not be published. Required fields are marked *