Technology Insider

git Primer : Quick Reference Guide

All Important Book:

http://git-scm.com/book/en/

Install Latest Version

sudo add-apt-repository ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git

Intial Configuration

git config –global user.name “Prashant Parashar”
git config –global user.email “email@myemailwhatever.com”
git config –global core.editor vim

To use https URL of repository and cache password:
git config –global credential.helper cache
git config –global credential.helper ‘cache –timeout=86400’

Clone a Repository

cd Directory_where_need_setup
git clone Repo_URL

Basic Operations

  • git pull origin branch_name : pull from remote & merge into local
  • git push origin branch_name : push local committed changes to remote. pull must precede the push.
  • git branch branch_name : creates a new branch. does not change the working branch. Must be followed by “git checkout”
  • git branch –set-upstream-to=origin/REMOTE_branch Local_branch_name :  sets remote-local association for the branch(tracking branch) so that you can simply do “git pull” or “git push” while working in a branch.
  • git checkout branchname : sets the branch as working branch in current directory. Entire code switches to “the” branch
  • git add filepath: to add a new/modified file to stage it in repository before commiting
  • git commit -m “commit message” : commits all the staged changes to repository
  • git commit -a -m “commit message” : adds and commits files. to avoid git add and git commit
  • git status : shows what needs to be added to staging and what needs to be committed.
  • git diff : shows actual differences with staged/committed repository
  • git diff –staged : shows what you have staged (using git add) which would get committed if you run “git commit”
  • git rm filename_or_path : removes a file from git. Remove from local tree first.
  • git rm –cached filepath : removes the files from git staging area but keep in your local tree. Useful in removing files you forgot to add to .gitignore
  • git log : shows the commit history
  • git log -5 : shows last 5 commit logs
  • git log -p -5 : shows differences as well
  • git log -10 –pretty=format:”%h – %an, cr : %s”  –graph : good way of printing pretty log
  • git commit –amend : commits the changes to the previous commit. In case you forgot some changes to earlier commit
  • git checkout — filename : destros local changes to file and get it from repository

Tags

git tag : shows all tags
git tag -a TagName -m “Tag message”  : Adds an annotated tag
git tag -a Tag -m “message” commithash : tag later using hash of commit
git push origin –tags : push all tags to origin. By default not tags pushed.

Working with Branches & Merges

Merge a branch with trunk/QA branch:

git checkout main_branch_name
git merge branch_to_merge_with_main_branch

git branch -d branchname : deletes a branch

git branch –merged : shows branches that have already been merged to another branch.

Stashing Changes

Before switching branches, make sure using “git status” that the working directory is clean. If not, commit or stash the changes first.

git stash : pushes the current changes to stash stack
git stash list : lists the stashes
git stash apply : pulls the stash from top of stack. stash remains on the stash until “git stash drop” is run
git stash drop : drops the to item off stash
git stash apply Stash_Name : applies particular stash
git stash drop Stash_name : drops particular stash
git stash branch branchname : creates a new branch out of stash

Removing a file from every commit

git filter-branch –tree-filter ‘rm -f filename.txt’ HEAD

 

Discussion

No comments for “git Primer : Quick Reference Guide”