47 Degrees joins forces with Xebia read more

Git - Useful commands

Git - Useful commands

In this post, we’d like to take some time to explain a few Git commands that, in our opinion, are very helpful and not as well known as they should be. These will be more useful for beginners, but for those of you that have extensive experience with Git, this is a good opportunity to review them.

We hope you all like them as much as we do.

git add -p

This will allow us to commit different changes on the same modified file. It’s important because it allows us to make atomic commits, which will be very helpful if we have to revert a specific change.

$ git add -p samples/my-test-file

Run it

Git Tips

Check it

Git Tips

We like this way of adding changes because it lets us to review all of them before they’re added, which should be a required practice for every developer. Previously, we would use git diff before adding, but by using the -p param instead, we avoid running two different commands instead of running just one.

git stash

This gives us a way to save file modifications and leave the repo in its last commit state. Changes will be added to a change Stack. Using the param save, we will be able to provide a short description that will be very helpful if we have more than one element at the stash Stack.

$ git stash save "description"

Run it

Git Tips

To see all of your stashes’ changes, just use the list param, which will give you an output similar to this one:

$ git stash list
stash@{0}: On feature-branch: My test change1
stash@{1}: On feature-branch: My test change2
stash@{2}: On feature-branch: My test change3

Run it

Git Tips

Now, if we want to recover a specific stashed change, we just have to input the right stash id, knowing which changes were staging on it:

$ git stash pop stash@{1}

Run it

Git Tips

git reset

This git command is very useful when we want to restore the branch state to a specific commit. It will revert all commits and keep your git branch in the same state as it was in the specified commit number. We have two ways to accomplish this:

get reset –soft

This will keep your changes like unstaged changes.

$ git reset --soft <commit hash> [/path/to/file/file-name]

Run it

Git Tips

Check it

Git Tips

get reset –hard

This will remove your changes. Be careful when using this command, though, because it could cause you to lose changes you don’t want to.

$ git reset --hard <commit hash> [/path/to/file/file-name]

Run it

Git Tips

Check it

Git Tips

git rebase –interactive

This command should not be used if the commits you want to modify have already been pushed.

The Interactive option allows us to interact with all of the commits in our repo history. It’s useful when, for example, we need to rewrite a commit message or want to merge two commits into one. There are many other uses, but these are the ones I use the most. For example, many times I realized when checking my git log history that I had a typo in one commit message or I forgot to add the ticket number to it. If the affected commit is not the last one, amend option will not work, but we’ll be able to change it with this rebase --interactive tool.

$ git rebase --interactive <commit_hash>

After that, we will enter in the predefined git text editor (Vim in my case). We will see something like this:

pick 4f2h421 Sample Message 1
pick b29e2k1 Sample Message 2
pick c2356ab Sample Message 3
pick 15eba34 Sample Message 4

# Rebase fac3g94..15eba34 onto fac3g94

Run it

Git Tips

Check it

Git Tips

At the bottom, Git gives us a list with all possible options and a short description of each one. It’s very helpful, too, so make sure to take a look!

Finally, we want to offer you not another git command, but a piece of advice. Do not use git push -f. It will rewrite your remote git history, which, in most cases, is not a good idea. In 99.9% of cases when we saw somebody using it, the pulls that it produced from other teammates failed. So, it’s better to have a bad git commit message, a type, or a revert commit, than doing a push --force.

As always, thanks for reading! If you have any questions or suggestions, don’t hesitate to start the conversation on Facebook, Twitter, or in the comments below.

Ensure the success of your project

47 Degrees can work with you to help manage the risks of technology evolution, develop a team of top-tier engaged developers, improve productivity, lower maintenance cost, increase hardware utilization, and improve product quality; all while using the best technologies.