Fork me on GitHub
 
 

GIT commands used quite often

Written by Aravind H.U on Jul 22, 2014

I am using GIT since past 3 + years, to manage my source code version, I am a member of Github, I come across some strange situations which are not normal I research a lot to figure out the solution. Nowadays I found that these situations are repeating quite often so I decided to list them down in a place so that It could help others who are facing the same situations.

Situation 1 : -

I want GIT to display commit logs in one line along with graph and decoration.

git log --oneline --graph --all --decorate

The above command gives the below output



    * 4886a10 thatwed
    * 1cb8551 thatwed
    * 4f10981 thatwed
    * 6fe047f thatwed
    * ca36710 Years
    * 83bcaf6 thatwednesday
    * 4a87291 thatwednesday
    * ce5be8b thatwednesday
    * 552e5c5 thatwednesday
    * ed54b6e Merge pull request #2 from NSElvis/patch-1
    |\
    | * e91b090 Fix typo
    * | 6464637 cars
    * | 5bd63e4 /cars/
    * | 24d8692 Disable Facebook social plugins which are currently broken (a bug in its code will resi
    * | 7a1c9d4 pong info
    * | 91857f7 pong info
    * | 17e8ef2 pong info
    * | 23d9584 pong info
    * | 7b213a5 Menu Pong app zip file thunder thingy magic package cabinet downloadable lolcat
    * | 3ff9765 updated /about w/ "favorite technical papers"
    * | 1bf5ee4 layout fixes for "photo" type of post
    * | 6d8b82a Updated layout & etc
    * | 1bddddf Updated layout & etc
    * | c353c94 listed post goes away
    * | 9b47308 listed post goes away
    * | bd204db -a-
    * | 877c448 Long overdue update to /about
    * | 1afc658 css zoom level generator update
    |/
    * c9babd7 unbreak gh pages?
    * 1ee4da1 new post

Situation 2 : -

Make existing git branch to track remote branch , say you create a branch called banyan in your local using the command


    git branch banyan

later you just push that to remote for collaboration


    git push origing banyan

Lets say some contributor pushes some thing to the branch banyan and later you also need to push your changes. To do so you need to take pull and then push, but you cannot since you dont have any local branch tracking the remote collaborating branch which you just pushed. So to solve this problem.


    git branch -u origin/banyan banyan

Situation 3 : -

You have to commited some large binary files to github and also pushed to the remote, later you realize that your repository is growing enormously, and decide to have a CDN. Once decided we delete them from the repository and commit .

But there is a problem here, whenever a new stakeholder joins for collaboration, it takes lot of time to clone because those large binary files still exists in the repository history. Here is how we could resolve this problem.

Lets say you are aware that you previously had /img folder with binary files, now we need to remove them from repository history permanently.


    git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch img/" HEAD

once completed just run the below command to remove the local repository .


    rm -rf .git/refs/original/ && git reflog expire --all && git gc --aggressive --prune

Just commit and push the repository. you can see the reduction in size.

Situation 4 : -

You need to checkout a partiular file from the remote tracking branch

Just run


git fetch
## and later
git checkout origin/branch_name -- path_to_file/filename

Situation 5 : -

you need to see the list of files in a particular branch and its current commit hash


    git ls-tree master

 

Please keep checking the list I will be updating as and when I face a new situation.

Share this:

Disclaimer

I hope that you won’t object if I occasionally use contractions and the less formal “you” instead of “one” in my posts presented here. That makes writing it easier for me and ensures a less formal atmosphere.

I attempt to post articles here as carefully as possible, but I cannot guarantee absolute freedom from errors in general at this time. Please bear with me. If you would like to help me please contact me

The information contained in this website is for general information purposes only. I try to keep the information up to date and correct, I make no representations or warranties of any kind, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the website or the information, products, services, or related graphics contained on the website for any purpose.

Any reliance you place on such information is therefore strictly at your own risk.

×