Git Tricks

When git repo is connected using ssh:

For checking username/keys use:

 ssh -T git@github.com 
 #If this is returns your user name that means you are connected with remote repo.

 ssh -vT git@github.com
 #If there are multiple ssh keys in your system then sometime you get confused which key your repo is using.So use this command to find out the ssh key your repo is using.

git config files:

Files which store the user details and branch details and some config settings like color etc.There are three config files on system:

Repository itself: /.git/config (for repo specific settings)

User home directory: ~/.gitconfig (for a particular user)(use –global to access this)

System-wide directory: $(prefix)/etc/gitconfig (for system wide settings)(use –system to access this)

Useful commands:

#For checking config settings you can use the command:
git config --list

#If you want to change the username or email you can simplay do:
git config --global user.email 'abc@gmail.com' #for user global config file
git config  user.email 'abc@gmail.com' #for repo config file

#You can check the username and email in the same way:
git config --global user.email #for user global config file
git config --system user.name  #for system config file

Default merge/Remote tracking branch:

Under [branch “master”], try adding the following to the repo’s Git config file (.git/config):

[branch "master"]
    remote = origin
    merge = refs/heads/master

This tells Git 2 things:

When you’re on the master branch, the default remote is origin.

When using git pull on the master branch, with no remote and branch specified, use the default remote (origin) and merge in the changes from the master branch.

If you don’t want to edit the config file by hand, you can use the command-line tool instead:

$ git config branch.master.remote origin
$ git config branch.master.merge refs/heads/master

#you can do this by checking out out the branch you want to set remote for:
git branch -u origin/branch_name
 
or

git branch --set-upstream-to origin/branch_name

Setting push default as current:

You can set in git what you want to push when you do git push. There are many options like matching, nothing , current, upstream , simple . If always prefer to use current. When you use current you don’t have to mention the remote branch everytime.It automatically gets mapped to the same name remote branch.Like branch name in local is branch1 then on git push it tries to push into remote branch branch1.

you can do this using:

git config --global push.default current

Reverting commits:

#If you want to revert a commit to a particular commit you can use:
git reset -hard commit_hash #use commit hash


#If you want to revert some particular commits use:
git revert commit1hash commit2hash 



#reverting to previous commit:
git reset --hard HEAD


# Reverting a merge commit
git revert -m 1 <merge_commit_sha>

#reverting last two commits
git revert HEAD~2..HEAD

Rebasing:

Pull with rebase instead of merge

$ git pull --rebase
# e.g. if on branch "master": performs a `git fetch origin`,
# then `git rebase origin/master'

Setting rebase instead of merging on pull :

# make `git pull` on master always use rebase
$ git config branch.master.rebase true


# setup rebase for every tracking branch
$ git config --global branch.autosetuprebase always
comments powered by Disqus