Git commands you should know

Today, I’ll show you the Git commands I’m using on a daily basis with my team mates.

As for the Docker commands article, I won’t repeat the most basic ones, you can just browse the Git documentation or various tutorials that fit you on the web.

Summary

  1. Git Status & logs
  2. File manipulation
  3. Commit cancellation
  4. Commit modification
  5. Pull & Rebase
  6. Branch deletion
  7. Branch renaming
  8. Cherry Pick
  9. Setting remote url
  10. Git diff

Git Status & logs

Besides the git status & git log command you should already be practicing, you may need to add some style for a better reading experience :

Git styled log In order to do so, you could just set up an alias (assuming you are on linux) :

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

And without an alias, simply type :

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

You can try the following to see the changes within the terminal but using an IDE or directly onto the diffs page proposed by Gitlab or Github (after a push) would be much more comfortable :

git show --color --pretty=format:%b $COMMIT

Alternatives to git log & git diff

There are multiple programs to help you with Git, personally I only rely on my IDE and our version control online tool (ie : Gitlab) :

  • A better Git log
  • Sourcetree
  • GitEye
  • SmartGit
  • P4merge

I haven’t used them and I’ll be more than pleased to hear about them from you ! They might be relevant for large merge requests.

Sometimes Gitlab or Github are getting weird on large merge requests (consider splitting instead of merging a huge bunch of work).
 

File Manipulation

 

Overwrite local files (reset hard)

In order to revert all changes based on the remote branch (located on the remote repository – ie: Github, Gitlab…), the reset hard command will do :

git reset --hard origin/<branch-name>

And to remove the latest commit from the current branch :

git reset --hard HEAD^

Finally once on another branch, you can apply a specific commit :

git reset --hard id_of_commit

Cancel files’ changes (using git stash)

This is another way to revert some unwanted changes but also a way to apply those changes onto another branch !

git stash

Then move onto the other branch to place the modifications and issue :

git stash apply

If you need to apply some other stashed commits, list them and apply the one you wish :

git stash list
git stash stash@{0}

Removing a file from a commit (git checkout)

Spot the referring commit for the file :

git checkout commit-id <path_to_file>

To revert changes to some specific files using checkout :

git checkout origin/branch-name -- path/to/file

Now to get back to another commit and make a new branch out of it :

git checkout -b name_of_commit_HEAD

Commit Modification

To modify the message of a commit :

git commit --amend -m "Hello new commit message"

If you forgot to stage a file to the last commit and wish to have it, you can index it and apply the modification to the last commit :

git commit --amend --no-edit

Pull & rebase

Whenever there is some work achieved remotely and you need to apply an update onto your branch, there are 2 methods (as I see and practice it) :

  1. git rebase
  2. git pull

I strongly advice doing a pull instead of a rebase which will apply a merge onto your branch you will need to push force afterwards.

Git rebase

git fetch -a
git rebase origin/master
git push -f

During the process you will need to correct the conflicts.

Git Pull

Using git merge will allow you to apply the changes from a branch onto the current one. But also a commit out of the change !

git merge other_branch

The pull command is actually doing a fetch then a merge at the same time !

git pull origin master

During the merge you will need to fix the conflicts if any.

Branch deletion

As simple as :

git branch -d name_of_branch

Branch renaming

git branch -m new-branch-name

Cherry picking

This feature is useful for instance in the following case :

  • You need to apply a hot fix on a specific version currently in production and of course you have been working a lot since that version.

In that case you can isolate the commit (I assume you squashed the commit during a merge) and apply it onto the specific version without other changes !

git cherry-pick id_of_commit

To see the list of commits with their id, you can use the following command, it might help :

git log --oneline

And to squash commits while merging :

git pull origin master --ff-only

Abort a cherry-pick

As for a merge, you can abort a cherry pick :

git cherry-pick --abort

Change the remote url (https/ssh)

Let’s say you need this in case you are behind a firewall which does not allow you ssh requests, just switch from ssh to https :

git remote set-url origin https://username@yourgitrepositoryurl

Differences between branches

If you are fond of command lines, then this one is for you :

git diff origin/master

You see here the differences between your branch and the remote master one. I admit using an IDE or whatever software will be more friendly 🙂

 

This is the end for now, I will try to update whenever I meet new useful Git commands. Drop a comment to let us know about your favorite ones !

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Want more information?

Related links will be displayed within articles for you to pick up another good spot to get more details about software development, deployment & monitoring.

Stay tuned by following us on Youtube.

%d bloggers like this: