Agustin Luques

Top 10 daily git common situations

a logo

Along my years as a developer, I have seen a lot of developers which, for different reasons, do not know anything about Git and have plenty of problems when they have to use it. Git is a free and open source distributed version control system and it is world-widely used. Although it is quite complex, it is very simple to use. I thought about what are the most common situations and decided to make my top 10.

1 - Cloning

Ofc, the most common and simplest situation is at the beginning when you have to clone the repo.

git clone <url>

2 - Status

One of the most used commands, at least for me, is when you need to see all the changes you have made.

git status

3 - Push changes

Usually, you will use this flow to push some changes you have already done.

First, pull to see whether there are some updates.

git pull

Then, you will add the changes. Usually, will add all the changes by -A or ., but ofc, you can do it file by file or with the help of wildcards.

git add -A

Third step, you will create a commit with a message containing all the changes you just staged.

git commit -m "descriptive message"

Finally, just push your commit(s).

git push

Note: you might run a git status in between each command to make sure everything is ok.

4 - Branch Management

It's quite common for a project to have a flow to coding. The most used, in my opinion, is the features flow. It consists in creating a branch for each feature.

  • To list all branches: git branch
  • To create a branch: git branch <name_of_the_new_branch>
  • To move between branches: git checkout <destinate_branch>
  • To delete a branch: git branch -d <branch_to_be_deleted>

But, one of the most used commands is to create and swap to the recently created branch.

git checkout -b <name_of_the_new_branch>

5 - Stash

If you are not very organised you might start coding and when you've finished, you realise that you are standing on the wrong branch. No problem! There are a few commands to help you get out of this situation.

Stash your changes. What does it mean? Just think you will store the changes in a thing just to retrieve them later.

So first,

git stash

Then, create or swap to the correct branch.

git checkout -b feature/<new_branch>

And finally, retrieve the last changes you stashed.

git stash pop
  • To see all stashes: git stash list
  • To apply all stashes: git stash apply

6 - "Noo, I didn't want to add that file's changes"

No worry, it's just a very common mistake. Fortunately, there is a command to undo that.

git restore --staged <file>

7 - "What did I do!?"

If you made some changes to a file but you want to undo all the changes whatever the reason is, just type:

git restore <file>

8 - Merging time

In features flow you may want to merge your branch just to include the feature to your develop branch.

First, swap to develop (or wherever you want to merge to).

git checkout develop

Then, pull the updates from develop.

git pull

Then, merge time!

git merge --no-ff <source_branch>

Note: I like to use --no-ff because it will maintain a nice view in the branches' graph view and will generate, in all cases, a merge commit.

9 - "Why red?"

It's quite common that, if you like to use the terminal, you want to see what changes you have made to a specific file.

git diff <file>

To exit that view, just type q

10 - Multiple accounts

You might have multiple accounts you want to use for different repos. In my case, I have an account for each client and also an account for internal projects. So you should configure a global and local account for each project.

git config --add <name> <value>
git config --global <name> <value>
git config --local <name> <value>

It's a very simple command to manage the config file of git, so you can read the documentation here.

Spam: if you want to configure a different proxy global and locally on Windows, you can read this post.


As you could see, there are only a few commands that you have to remember just for a daily use of Git. It is a very complex and powerful tool, though. You can always ask or search on StackOverflow because the git community is quite huge and will help you. Also, you can reach me on any of my social medias.


instagram icon twitter icon linkedin icon github icon


Buy me a beer🍻

or

Invitame un café ☕️


almost 4 years ago

Agustin Luques