GIT Tips & Tricks

In this post, I will try to explain some of the very useful commands I came across. if you use git daily then this post will make your task easy. 

Let's start with an example, you are working on your local/working directory which means you have some local changes. Now, you want to pull the latest changes or sync your local repo with remote/master repo, you have to clean your working directory. So how do you save your local changes? 

One answer could be to create a patch of changes and store somewhere and reapply that patch manually after syncing/updating your local repo, this is what I used to do earlier but I found it very tedious and annoying and I came across a very good git command "git stash" which does this very efficiently. 

git stash

Various operations which git stash can perform are like:

git stash save - Take all the changes you have in your working directory and the index, and save them away, leaving you with a clean working directory
git stash list - List all the stashes you’ve saved away
git stash apply - Apply the topmost stashed commit onto your working directory
git stash pop -  Also remove the stashed commit in addition to applying it
git stash clear -  Throws away all your stashes
git stash drop - Allows you to remove a single stash

So instead of, creating a patch you could use "git stash save" and applying of patch "git stash apply". 

git diff --staged

"git diff" shows all your not staged changes but for the changes which are staged through "git add" command, "git diff --staged" helps a lot to see the diff.

git checkout -p FILE

This is really interesting command I have ever come across. When we want to checkout a file we use "git checkout test.c". But what if we want partial checkout. Let me elaborate it by taking an example, I have made 3 local changes in test.c and I want to discard 1st change and keep other 2nd/3rd changes. I can do that with,
git checkout -p test.c
It will prompt me a message "Discard this hunk from worktree" for 1st change, and If I say "y", it discards 1st hunk, which is what I wanted then it prompts same thing for 2nd change and I simply type "q" for quit because I want to keep 2nd/3rd changes. 

Above one is called an interactive checkout(hunk pattern), but I have remembered this as partial checkout since -p option is given.

git log -L <start line number>,<last line number>:FILE

This is the one of the best option I could find with git log. Let's take a scenario where you want to check the history of particular function e.g. int func(int a, int b) defined in test.c file. In other words, you want to know what all commits/changes have been gone into this function definition. 

So what you have to do is, just check both(starting and ending) line numbers. For instance, "func" starts at 25 and ends at 100 line numbers and run below git command which does you job:
git log -L 25,100:test.c
Note: You can give 25,+75 instead of 25,100 also.

git aliases

There is a way to set aliases for git commands as well so that you don't need to remember commonly used long commands. You could either add via terminal or edit ~/.gitconfig file. For example, in order to see last log I simply run
git last
after adding this as an alias
git config --global alias.last 'log -1 HEAD'
You can even make an alias "g" for "git" in your ~/.bashrc file so that you could use "g" instead of typing "git" all the times.

.gitignore

This is something I really love to use while debugging. What happens when we are debugging, we create lot of temporary files(e.g. log files etc.) which are always showed up as untracked files when you see the status "git status". So, to let git know that we want to ignore these files you need to make an entry into .gitignore file present in .git directory(you can create if not). e.g. In .gitignore file
*.log
This way all the logs file present are ignored by git. 

git status -sb

This shows status of repository into very short and sweet manner with some color coding which helps in case you have lot changes in your working directory. 

Comments

Popular posts from this blog

MBR partitioning with 'parted' utility

Disk Partitioning: MBR vs GPT

Replace default splash screen in Yocto