Pages

Wednesday, September 18, 2013

GIT commonly used commands

GIT Structure
  • Central repo (remote-repo)
  • Local repo (local-repo): Local copy of remote-repo. If there are new pushes to remote-repo, local-repo will be behind. If you have unpushed checkins, local-repo will be ahead of remote-repo.
  • Local index (index): Changed or new Files need to be staged/added to index, before it can be checked in.
  • Personal workspace (ws): Working copy of the local-repo.  
Creating local-repo and ws for the first time
  • git branch -a # Show all branches including remote
  • git checkout <branch> # Switch to branch
  • git status # Check current branch
  • git log # commit/push history
  • git pull origin <branch> # pull branch from remote to current branch. To update current ws branch, use same branch name.

Sequence of Steps
  • pull / rebase [remote to ws]
  • git pull origin <branch> # pull branch from remote to current branch. To update current ws branch, use same branch name.
  • fetch [remote to local]
  • checkout HEAD [local to ws]
  • checkout [index to ws]
  • diff HEAD [b/w ws and local]
  • diff [b/w ws and index]
  • add (-u) [ws to index]
  • commit [index to local]
  • commit -a [ws to local]
  • push [local to repo]
  • git push origin <branch> # To push current ws branch, use same branch name. Default is all local branches.

Review changes
  • Commit history: git log
  • Last commit changes: git show
  • Commit history with changes: git log -p 
  • Check if a commit is merged to current branch: git branch --contains  <6 digits of commit number>
Revert local changes
  • git checkout <specific-file>
  • git checkout -- . # all unstaged changes will be lost 
  • git reset HEAD <file> or git rm --cached # to remove file from commit. reset is the preferred way as rm without --cached could delete the file loosing changes
Merge
  • git checkout main-branch
  • git pull to latest...
  • git merge side-branch
  • resolve conflicts manually, see git status and do git add <resolved file>
  • git branch -d <local branch>
  • git push origin :remote-branch     # : implies delete remote branch
Stash
  • git stash
  • git stash list
  • git stash pop
  • git stash drop
Alias
To use git <alias_name>:
  • git config --global alias.unstage 'reset HEAD --'; 
    • git unstage myfile
  • git config --global alias.uncommit 'reset --soft HEAD^';
Delete/Cleanup Remote/Local branch
  • git remote prune origin // remove invalid local references to deleted remote branch
  • git gc --prune=now // local cleanup
  • git branch -d trac29-consol-wsdl / /delete local branch