Top 10 Daily Git Situations

The most common situations when using Git

Git is a free and open source distributed version control system and it is widely used throughout the world. Although it can be quite complex, it is very simple to use. As a Software Engineer, I compiled the most common situations. This article reveals the top ten.

1 – Cloning

Of course, the most common and simplest situation is at the beginning, when you have to clone the repository.

2 – Status

One of the most frequently-used commands is when you need to see all the changes you have made.

3 – Push changes

Usually you will use this flow to push some changes you have already done. First, pull to see if there are any updates.

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

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

Finally, just push your commit(s).

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. One of the most used is the features flow which creates 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>

 

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

5 – Stash

Some engineers might start coding and when finished, realize that they are standing on the wrong branch. No problem! There are a few commands to help get out of this situation.

Stash your changes. What does this mean? Think of it as storing the changes in a thing just to retrieve them later.

So first, stash changes.

Then, create or swap to the correct branch.

And finally, retrieve the last changes you stashed.

  • To see all stashes: git stash list

 

  • To apply all stashes: git stash apply

 

6 – “Oh no! I didn’t want to add that file’s changes”

Don’t worry, this is a very common mistake. Fortunately, there is a command to undo that:

7 – “What did I do!?”

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

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).

Then, pull the updates from develop.

Then, merge time!

Note: One good way to do this is to use no-ff because it will maintain a nice view in the branches’ graph view and will generate a merge commit in all cases.

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.

To exit that view, just type q

10 – Multiple accounts

You might have multiple accounts you want to use for different repos. For instance, you may 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.

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.

Conclusion

As you can see, there are only a few commands that you have to remember for basic 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 expansive and will be happy to help you.

 

Sources:

Agustin Luques

Agustin Luques

Software Engineer
Agustin is a software engineer who studied at the University of Buenos Aires. Agustin is experienced in .Net but is always open to learn new technologies and languages.
Agustin Luques

Latest posts by Agustin Luques

Share This Article

Post A Comment