Tuesday, September 15, 2015

Git for Newbies


Git Quick Start

I summarized the commonly used git commands from my experience, this will help to certain extend to get quick hands-on,

Creating a Branch:

Use following commands to create a branch locally and push it to remote server

  • git checkout -b <newbranchname> - This will create you a branch locally from a master branch
  • git push origin <newbranchname> -This command will push your local branch to remote server  
  • git clone <url> (for example: git clone https://github.com:kathir2903/gpibcontroller.git) - This command will download the entire project and its version history
Deleting a Branch:

Branch deletion is two step process, first delete the local branch and push the changes to remote, which will delete the remote branch as well.

  • git branch -D <branchname> - This command will delete the branch locally
  • git push origin <branchname> - This command will delete remote branch
Add, Commit, Push code to a Branch:
  • git add <filename>  This command will add file to the local branch in preparation for version
  • git commit -m <Commit message> This command will commit your changes that were added for tacking 
  • git push origin <branchname> This command will push your changes from local to remote
  • git branch -r  This command will list remote branches
Undo commits:
  • git reset <commit_id>  This command will undoes all commits after <commit_id> given.
  • git reset --soft <commit_id> This command will keep your local changes in the working directory
  • git reset --hard <commit_id> This command will reset to the commit_id with wiping of the changes you have made locally.

Stashing and Switching between branches:

There will be a case that you are working in  a branch and still have lot more to work on that branch, but  you want to switch to another branch. In such case, use 'git stash' command to cache the changes you have made so far to the branch. Use these commands,
  • git stash - This command will save your changes in the branch you are work on
  • git stash list - This will return the list of files that you have made changes and stashed.
Now you are ready to switch to another branch without messing up with the current working branch,
  • git checkout <anotherbranchname> - This command is to checkout a different branch (note: -b option will create you a local branch for you which you don't want here)
You can now work on this 'anotherbranchname', once you are done with it and want to switch to the previous branch simply do this,
  • git checkout <oldbranchname>
  • git stash pop - This will pop up to the latest stash list that you were working on before switching to another branch
Commit logs and version history reviews:

Two important commands that will be used on daily basis to review the commit logs and history versions are 'show ' and 'log'

  • git log - This command will list the history of current branch
  • git log <filename> - This command will list the version history for a file
  • git show - This command will output the list of files and internal changes(line-by-line code) you have made the files.
  • git diff <filename> - This command will show the differences between your local changes to the file and the branch
Merging and Cherry-picking:

When you want to merge your changes in your private branch to the master branch. First, checkout master branch, update the local branch with origin/master, then do merge your private branch

  • git checkout master
  • git pull
  • git merge <you_private_branch>
  • git push origin master
Another way to do is with cherry-pick, this will apply changes to the master introduced by your any existing commits(it could be from any branch),
  • git checkout master
  • git pull
  • git cherry-pick <commit_id>
  • git push origin master
This is good link to read on cherry-pick, http://git-scm.com/docs/git-cherry-pick

To read more git internals, here is the ebook which is very good,

https://drive.google.com/file/d/0B-TqU5Xu5JToVnljNlN6VGxvME0/view?usp=sharing



No comments:

Post a Comment