« Back to News

Git Tips and Tricks – Part I (File related)

Many times we get stuck in a situation where we get confused about which Git command will be most suitable for the problem. Basically we avoid trying out things we fear might cause further problems.

I have seen many people who are either not aware about the power of Git or are hesitant to try out the commands in the repository in fear of losing the code changes or causing a chaos with the commit.

This arGit-Tips-&-Tricksticle assumes that you know basic Git commands (for example, git add, git commit, git push, etc.) and discusses some of the powerful Git commands, which some of us are not aware and hence never bother to try them out in day-to-day work.

File Related Commands:

  1. Log actual changes in a file

If we want to know all the changes that occurred in a particular file (here “change” means all past commits in which this file had been modified), then we can make use of:

Command: git log -p filename

This command displays details like commit message, author, date and code changes on the file in the past commits. This is a very handy command when we know that the issue is in a particular file and we want to have a look at all code changes that took place in it.

Say we want to dig deeper and see the changes on a specific line in that file, then we use

git log -L startLine,endLine:filename

Example:git log -L 1,10:issueFile.js’     

This will show the changes in a block of 1st to 10th line in the issueFile.js.

Note that we could have thought of using “git blame filename” to find the person responsible for the changes in every line of the file. The only problem with this is that it will display the details of the last commit and NOT the history of commits.

  1. Extract a file from another branch

Many a times we come across situations when we need to pull file(s) from another branch (say branch X) to the current branch (say branch Y). Ideally someone would do this by checking out to branch X, copying the files somewhere (say /tmp/),  then checking out to branch Y and adding the files from /tmp/ to the desired destination. This is tedious. What if there were an easier way to do the same without switching the branches? Wouldn’t you love it ? This is how we do it:

git checkout branchToPickFileFrom — path/to/file/iwant


Example: In the below example, this is how we could update the MyFeature branch with myplugin.js file from the master branch.



If we need to copy directories from one branch to the other, without switching branches then use:


  1. Add only few changes from a file

Suppose during coding we have made changes at two places in a file and we want to add only one of those changes to the repository and keep the other change for other requirements. Then use “git add -p” This is a very useful command as we have the convenience to add some changes to the repository and can continue to work on other changes.



  1. Stash only some files/ changes

We all know what git stash does, it simply puts all our unsaved changes on a “git stack” of sorts. Then at a later time we can do git stash apply and our changes will be re-applied. “git stash list” can be used to see all stashed changes. But regular git stash will stash all of the files at once. Sometimes it is handy to only stash some of the files, and keep the rest in our working tree which can be achieved by command:

git stash -p

It will prompt with a few actions for each hunk (Change) and will add/skip the change based on user actions.                                                                                          

In the example below, you can see I am stashing one change, while keeping one change in a repository for further use.


  1. Look inside stash

We often put work away to do later. Then other stuff comes along and a few weeks later, we might have number of stashes in our repository and we are not sure what each stash contains. If we want to inspect any stash, and find out which files were changed in it, then we can make use of:

git stash show -p

Example: If we are interested in knowing what the 2nd stash contains then the command is:

git stash show -p [email protected]{2}

Continue Reading…Git Tips and Tricks – Part II (Commits related)


Leave a Reply

About the Writer

  • Tushar Bende
    Senior Software Developer at Synerzip