Git cheat sheet


Basics:
> which git
> git --version
> git help
> git help add
> git status
> git add <file_name>
> git log
> git log <branch_name>
> git log -p <SHA1>
> git log --oneline
> git log --oneline 3
> git reflog
> git init
> git show <SHA1> (It will show what that commit is all about)

Config files:
  1. System - Applies to all users on the system
  2. Global/ User - This is present under the user directory and has information like aliases, username, email, mergetool etc. that user wants to use which are specific to him/her.
  3. Project - branches, remotes etc.
Branches:
> git branch (list the local branches)
> git branch <branch_to_create> HEAD
> git checkout branch (run it from branch from which you want to checkout)
> git checkout <branch_name> (To switch to that branch)
> git checkout -b <branch_name>
> git checkout -- <file_name> (To revert to the file -- denotes file from the current branch)
> git branch -d <branch_to_delete>
> git branch -D <branch_to_delete> (delete although there are some )
> git log --graph --online --all --decorate
Merges:
  1. Fast forward merge - If there are no more commits on <branch_1> to merge into <branch_2> just HEAD gets shifted in case of merge.
  2. real/ recursive strategy merge- If there are 1 or more commits on <branch_1> to merge into <branch_2> As HEAD is moved ahead to some change set / commit on <branch_1> in case of merge. Here the configured editor pops up to specify the merge message you can just save or add message and save to complete the merge.
  3. Conflict merge (merging state - need to resolve manually or using tools) - on resolution stage and commit them. Refer (from <<<< HEAD  till ==== till  >>>> <branch_name>) after resolving add and commit it will pop up the editor to specify the message - remove the conflict statements in the message and after providing the correct message proceed.

> git branch --merged (It will show you the current branch and the list of branches which are merged/ in sync with to the current branch)
> git merge <branch_name> (Run it from branch to which you want to merge)
> git merge --no-ff <branch_name> (Forces to create a new commit instead of doing a fast forward)
> git merge --ff-only <branch_name> (Do merge only if it is fast forward else abort)
> git merge --abort (in case of conflicts and you do not want to resolve)
> git log --graph --online --all --decorate
> git mergetool
Commits (Diff/ revert):
> git commit -m "message"
> git commit - am "message"
> git diff <sha1 or first few letters>..HEAD
> git diff <sha1>..HEAD~2
> git status --staged / git status --cached
> git rm <file_name>
> git mv <file_name> <new_name>
> git revert <SHA1>
> git reset --soft <SHA1> (Will keep both staged and working directory files)
> git reset --mixed <SHA1> (this is default option) (Will keep only staged files and not working directory files)
> git reset --hard<SHA1> (Will neither keep staged files nor working directory files)
> git clean

Stashing:
> git stash save "Message" (Stashed changes available across the branches)
> git stash list
> git stash show <stash_id>
> git stash show -p <stash_id>
> git stash pop <stash_id> (remove from stash)
> git stash apply <stash_id> (keep the copy in the stash as well)
> git stash drop <stash_id> 
> git stash clear
Remote Branches:
GitHub account (Free - only public repo can be created) - Create a remote repo and give a name.
> git remote
> git remote add origin <URL>
> cat .git/config
> git remote rm origin
> git push -u origin master (-u makes it as a tracking branch on remote)
> git branch -r (list the remote branches)
> git branch -a (list the remote + local branches)
> git clone <URL>
> git clone <URL> lynda_version
> git clone <URL> -b <Branch_name>
> git log --oneline origin/master
> git diff origin/master..master
> git push origin master (push the master branch to remote and have same name master)
> git push origin <branch_name>
> git push
Fetching and Pushing:
> git fetch origin
> git fetch (we just update the orgin/master and not the working branch i.e master)
> git merge origin/master (merge to be run from the branch to which you want to merge)
> git merge origin/<branch_name> (merge to be run from the branch to which you want to merge)
> git pull = git fetch + git merge
> git branch <branch_to_create> origin/<branch_name> (checkout/ switching to remote branch which is fetched)
> git checkout -b <branch_name> origin/<branch_name> (-b make it a tracking branch on local)

> git push origin :<branch_name> (Older way to delete the remote branch)
> git push origin <local_branch_name>:<remote_branch_name>
> git push origin --delete <branch_name> (Newer way to delete the remote branch)
> git push -u origin <branch_name> (-u make it a tracking branch on remote)
> git log -p <local_branch_name>..origin/<remote_branch_name>
Collaborating:
Enabling collaboration in GitHub > Admin > Collaborators > Add people username who can collaborate > they will get the invitation.
If you want to independently work on without getting the invitation for collaboration > Fork the project and once you work on some tasks/ issues and you see those can be part of actual project > Raise a pull request

Best practices:
  • Manage the new feature work in the new branch in case to send for review push it to remote with tracking as a new branch
  • Once approved / corrected by reviewer - fetch again re-verify and merge to master and push to master
  • Always fetch before push
  • Try to use fetch and merge instead of pull
  • Try to log the merge records instead of fast-forward merge
Alias, Tools:
> git config --global alias.co checkout
> git config --global alias.dfs "diff --staged"
> git config --global alias.logg "log --graph --decorate --oneline --abbrev-commit --all"

git config --global credential.helper wincred

SSH Tools: the remote URL starts with git@ instead of https://
Choose option on github and choose the SSH option for clone to get that URL

GIT User interface tools:
Github, Source tree, Tortoise git etc.

Hosting Git:
Github, Bitbucket, Gitorious

Gitosis, Gitolite: internal git deployment





Comments