Enhancing your Programming efficiency with Advanced Git Commands 

Advanced Git programming for proper SCM 

Stephen Rodriguez

--

If you have not done so yet, I would highly recommend downloading Git today and applying to your already existing projects. What I usually tell new adopters is that Git is kind of like the Apple Time Machine but for code. So if you haven’t done so yet, get on Git today.

This tutorial is targeted towards people who already use Git everyday for their programming projects. This tutorial will expand upon some of the more crucial advanced Git commands and how they work. Any of these commands can easily be adopted and will vastly improve code workflow and efficiency amongst other collaborators. So without further ado, lets jump right in…

git commit —amend

Lets say you love making multiple commits before a single push command is considered. Once you do make a push, all those commits will cause tons of problems for your collaborators since the branch HEAD will be much farther ahead from them. As such, they will need to do a pull, rebase, commit, etc etc. And thats not too nice. Hence, we see the use for the commit —amend command.

This command will apply the new changes on top of the top-most commit. This way, when a push is made, only one commit is sent.

git rebase

This command is helpful for bringing outdated branches up to speed.

Credit: https://www.atlassian.com/git/tutorial/rewriting-git-history#!rebase

As you can see, the rebase command will update the Feature branch to match with the HEAD of Master. The purpose of doing this is to help with keeping branches up to date when the master branch progresses too far. At some point, the Feature branch will be adopted to the Master branch. When that time comes around, then the branch will need to be rebased in order to accommodate for the differences in commits.

git stash

Often times, you will be working on a project and things will get messy. Your code is all over the places, functions aren’t name properly, files are misplaced, and so on. As such, you dont want this to be committed to the branch but you want to keep track of your changes at least. This is where git stash comes handy.

Stashing makes a copy of your current workflow in a stack. Since it is on the stack, you can keep pushing changes to the stack. At any point, simple run the following command to see what is on the stack:

$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
stash@{2}: WIP on master: 21d80a5 added number to log

In order to apply a stash onto the existing workflow (basically a commit):

$ git stash apply
# On branch master # Changes not staged for commit: # (use “git add <file>…” to update what will be committed) # # modified: index.html # modified: lib/simplegit.rb #

And lastly, you can even apply the changes from the stash as a new branch using the following command:

$ git stash branch TestChanges

There are of course many more commands that can be used but these are just some of the many commands that I myself use on a day to day basis. Pass the word on and Happy Coding!!!

--

--

Stephen Rodriguez

Senior DevOps Engineer @ Sharkmob & Fan of all things Golang/Typescript ❤