Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts

Useful GIT Commands

This is collection of useful GIT commands I use in my day today work. Just posting here to keep as my own reference.

Revert changes made to your working copy:

git checkout .

Revert changes made to the index (i.e., that you have added):

git reset

Revert a change that you have committed, do this:

git revert ...

View commits not yet pushed to remote:

git log --branches --not --remotes

Not pushed most recent commit with branch name:

git log --branches --not --remotes --simplify-by-decoration --decorate --oneline

Difference between two branches:

git diff --stat --color master..branch 

Here the order of branch name is merge-target..merge-source.

Create remote tracking branch:

git branch --track feature1 origin/my-remote-branch

Here git fetch/pull from feature1 branch will get the updates from my-remote-branch

Create new local branch from remote branch:

git branch --no-track feature2 origin/master

Push local branch to remote repository:

git push origing feature2

Checkout specific file from differrent branch:

git checkout remote/branch path/to/file

 

How to Use Multiple GIT Accounts

Yesterday at HMS we moved some of our projects to GIT from SVN repository. So in order to access that office GIT repository I had to create a new ssh key using my office email address. I already had a ssh key which I used to access my GitHub account. So once I created new key I was unable to access GitHub since in normal configuration ssh can only have one public key.
But I wanted to find a way which I can use both office GIT server login and my GitHub login. After doing some searching on google I found a solution to that. For that we'll have to create ssh config file. So I created following config file at my .ssh folder
vi ~/.ssh/config

# hms-git
Host hms-git
  HostName git.myoffice.com
  User git
  IdentityFile /home/sandarenu/.ssh/id_rsa

# github
Host github.com
  HostName github.com
  User git
  IdentityFile /home/sandarenu/.ssh/id_rsa_github


Once this config file is saved I could clone office git projects using
  git clone git@hms-git:myproject

To clone GitHub project I could use
  git clone git@github.com:sandarenu/task_tracker.git