stash

5 least used Git hacks

Git is undoubtedly the best thing happened to the programming lifecycle. To the people who don’t know git, it is a widely used version control system for software development. While git being git , saves most of the troubles software engineers having related to versions, collaborating, going back and forth in developing features and adding them to the software, there are lot of commands in git which are not very commonly used but saves your time in a great deal.

(This article assumes that you have a basic understanding of git workflow)

Git stashing

Whenever you have any uncommitted changes in your git repository and you want to change the branch, you may have come across the following error.

Capture

When you have uncommitted changes which you still want to keep, then you just have to type “git stash”. It will record both your staged and unstaged changes internally. When you are ready to comeback to your incomplete work you just have to type the command “git stash apply”. This command will only apply your most recent stashed changes.

You can see your stash history by “git stash list”, It will give you the list of stashes which are stored. If you want to apply an older stash, you have to specify the name with the command like “git stash apply stash@{1}”.

Capture stash list

Git doesn’t provide a command like “git stash unapply” to unapply a stash. If you want to unapply the changes that came from the stash , you have to do it by simply retrieving the patch associated with a stash and apply it in reverse using “git stash show –p | git apply -R”. This will revert the changes in the most recently applied stash. If you want to specify a stash to unapply you can use the above command as this “git stash show –p stash@{1} | git apply -R”.

If you are more into it , you can also create a git unapply command yourself so that you don’t have to remember the whole thing or google it everytime you need it by using aliases which is discussed in the last section in this blogpost.

Following are few more commands associated with stash

git stash apply –index” – When the stash is applied using the normal “git stash apply” command then your previous staged changes will be unstaged. If you want your previous position with all your staged changes staged, then you have to use the apply command with “–index”. This will give you the original position you were in before stashing.

git stash drop stash@{0}” – When you apply a stash using “git stash apply” command it will continue to stay on your stash list. To remove your stash from the list you can use the above command.

git stash pop” – If you want to apply the stash and immediately remove from the list, then you can use the above command.

One of the other use cases of using stash can be like this. You have two feature branches in your git repository named feature1, feature2. You start developing something in feature1 branch and suddenly you figure out this is not something you need in feature1 branch, but needed in feature2 branch. All you have to do is stash the changes, switch the branch to feature2 from feature1 and apply the stash. Applying the stash might cause you merge conflicts. But if you are using git you should have lot of experience in resolving merge conflicts. So I am not going to focus on them here.

Git reset

This is not very uncommon command as you may have expected. But I am sure the only command you use all of the time is “git reset –hard”. But there is more to the reset command.

If you want to unstage a file or set of files from the staging index, you can use the “git reset” or “git reset <file_name>

If you have a commit to undo you can use “git reset –hard HEAD~1” or “git reset –hard“. 1 can be replaced by number of commits you want to undo. If you want to move some commits to a separate branch, all you have to do is make a branch from the branch currently you are in and then undo the commits. In this scenario you will have the commits safely on the branch you created new.

Suppose while you are working on a certain branch and suddenly you got to fix a bug in another branch. But your current work is not completed. So instead of stashing them you just commit the changes of the working directory and checkout the other branch and fix the bug. But when you return to your previous branch, the last commit of it would contain incomplete work. So you can use “git reset –soft” to revert the commit and keep the changes in the index. So you can start completing work from there.

If you want to remove a file from the index before a commit you can use “git reset <filename>”.

Git ignore

There are standards defining what types of files you should commit and not. If you are a collaborator of a large project, you may have come across what kind of files to commit and not. Most probably these are the files related to your IDE. You can use a .gitignore file in your repository to mention all the files which should not be tracked intentionally. A .gitignore file should be committed in your repository in order to share your ignore rules with other collaborators. Github provides a list of recommended .gitignore files for popular operating systems, environments and languages. (https://github.com/github/gitignore)

But if you have a file which is already tracked by your repository adding that to the .gitignore file will not stop git from tracking it. So you have to manually remove it.

Each line in .gitignore file specifies a pattern . # denotes a comment. ‘!’ can be used to negate patterns. Sample .gitignore file would look like this

#Complied Object Files

*.slo

*.lo

*.o

*.obj

# Precompiled Headers

*.gch

*.pch

Git cherrypick

This is the most useful command which will get out of you from most of the problems. Git cherrypick allows you to merge a single commit from one branch to another. This is very useful in situations which you do not merge the whole branch because of inconsistencies in versions and etc. cherrypicking is very simple. You have to first get your hash of the commit. You can use git log or any other preferred method. If you are using github the yellow highlightes numbers represent the hash associated with each commit in the following picture.

hash.PNG

Then all you have to do is use the command “git cherry-pick <hash>” in the branch you want to apply the commit.

Git aliases

Aliases is the most amazing concept of git which will let you own it. Here is how it happens. If you are too lazy to type the git commands you can add short aliases for them. For example let’s take the command “git checkout” . You can create an aliase “c” for it by using the following command “git config –global alias.co checkout”. Then next time when you need checkout you just have to say “git c” instead of “git checkout”. As we discussed earlier in the stashing section you can create your own “git stash unapply” command using “git config –global alias.stash-unapply ‘!git stash show -p | git apply –R

 

Resources: https://git-scm.com/docs

Advertisements