GIT: useful commands

Ilies Ait Hamouda
5 min readFeb 25, 2020

Hello Folks,

So recently, I decided to quit my job and start another one elsewhere. And as any new developer, I had to setup my new workspace and adapt to the new workflow.

Quickly, I realized that I forget some useful Git commands that helps a lot in the daily tasks. So I decided to write them down and share them through a post. This way, I will never forget them again and maybe it will help someone else and benefit to everyone 😃.

How to Squash

So you finished the development and you want to push your changes into the remote repo, open a PR and merge it to master/develop or whatever flow you’re using…. But you made a lot of commits and some of them have a very funny/weird messages. You would like to make all those commits into ONE. This way, your target branch will be clean and it will be easy to find all the changes you did related to this feature/bug. Here’s one way to do it, I’m not aware of any other way, maybe there’s a better one but this one works for sure 👍 and I’m very happy with it:

1- First, you need to rebase your local branch from the remote one the number of commits you want. the command should look like this :

git rebase -i origin/branch-name~numberOfCommits branch-name

Here’s an exemple:

git rebase -i origin/XXX-9876-do-something~4 XXX-9876-do-something

2- Once you enter that command, you’ll have a prompt window that will let you choose which commit you want to keep and which one you want to squash. You have to remove the word pick from the commit and replace it with squash. At the end you should have only one commit having “pick” as word. Exemple:

Before:

After:

When you finish, type

:wq

Then you’ll have another prompt window asking to choose the commit message. In general, I remove the old commit messages and i keep only one. Do as you please!

3- Finally, the last step is to push these changes into remote branch. For that, you just need to force push the changes:

git push origin +branch-name

That’s it, Now you will see through git log that you have only one commit that contains all your changes. Enjoy! 😄

Helpful reference : https://stackoverflow.com/questions/5667884/how-to-squash-commits-in-git-after-they-have-been-pushed

How to update a forked repository

Let’s say you have been working on something for quite a few days and a friend of yours has pushed changes into master (upstream repository) and you need to get them before pushing your changes (you don’t want to face the nightmare conflict). It is simple then:

1- Make sure to stash your changes, or commit them into your local branch

2- If it was not done before, make sure you have set your upstream repository in git. In order to do that, you just need to :

git remote add upstream https://github.com/OWNER/REPOSITORY.git

Once done, you can check if it was well saved by :

git remote -v

You should find 4 lines, two for each : origin and upstream (fetch and push)

2- You need then to fetch the upstream repository following the first step:

git fetch upstream

3- When you completed the first 2 steps (or you already did it once before) you can simply merge from the upstream branch into your current branch. usually, you want to merge upstream master into your local master. So it should look like this:

# switch to the master branch of your fork
git checkout master

# merge changes from the upstream repository into your fork
git merge upstream/master

Done! Now your branch is up to date and you can continue your development.

Create a branch locally and push it in remote repository

Easy:

1- Change your current branch to master

git checkout master

2- Create your new local branch and set it as current branch

git checkout -b my-feature-branch

3- When you have commited all your changes, simply push your new branch to remote repository:

git push --set-upstream origin my-feature-branch

Remove a changed file from the unstaged files

So the use case here is simple: we have changed a file ( or a bunch of them lol) and Oh wait, we don’t need theses changes anymore because someone already fixed them on master. so let’s just pull from master then… hmm can’t because we have unstaged files 😃.

with the command line, we can simply reset the state of our file.

git reset --hard

now, your branch is clean and HEAD is set on the last commit.

Delete a remote branch

To delete a branch on your remote repository, simply type the following command:

git push -d origin EFD-10082-your-branch-name

Move a branch from repo A to repo B

So let’s say you have a branch XXX-11 in your forked repository and you want to move it to the main repository. A simple way to do that is to :

git push upstream XXX-11

upstream represents the name of your destination repository.

be sure to be on the repo where your branch lives and that your dest. repo is well set. You should find it if you type :

git remote -v

along with your origin repo.

Clean up you local repo (remove all merged branches)

ok now folks, we want to clean our local repo from all merged branches, simple, here’s a quick one line command line to do that:

git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

Now let’s explain in 3 lines what this command do :

1- git branch — merged will list all branches that have been merged with remote/origin

2- grep will take all the list and exclude master and dev branches (that you really don’t want to delete)

3- will delete all branches outputted from the grep.

Now enjoy the new clean local repo :D

ps: you can create an alias for that in order to forget the long command line :D

Remove last commit from your local branch

You inadvertently committed a file or a change that was not supposed to be committed OR you wrote some joke in the commit message and it doesn’t look professional ? well then very simple, just type:

git reset — soft HEAD~1

this command will simply delete completely the last commit from your branch (history included), it is not a revert (which will push another commit that undo what the previous commit did)

I will Edit this page once in a while to add more tips and useful stuffs. So stay tuned 😉

That’s it!

--

--