My list of useful Git commands

After using Git for last three years extensively, I have compiled this list of commands I use frequently. Occasionally, I rely on posts like this one to get me out of the pickle.

Help

git help <commandName> (dont write git in front of it.. ex: git help remote) 

Git Staging

New Files -> git add .
Unstage/Delete -> git add -u
clean -> git clean -f

Undo

 Undo -> git reset --hard
 Undo Commit -> git reset --soft[hard] Head~1

Clone a Repo

clone  -> git clone [ssh/https Url]

Stash

stash -> git stash
apply -> git apply
drop -> git stash drop
clear -> git stash clear
pop and apply -> git stash pop
list -> git stash list

Tag

Create-> git tag [tagName]
show -> git show [tagName]
Push To Remote ->git push --tags
             git push origin [tagName]

Log

Fancy log -> git log --graph --oneline --all --decorate
Reference log 
(shows where the head was..useful for recovering delete) 
          -> git reflog

Alias

Ex:
Run -> git config --global alias.lga "log --graph --oneline --all --decorate"
Use -> git lga

Branching

create -> git branch [branchName]
create checkout -> git checkout -b [branchName]
checkout remote branch -> git checkout [remoteBranchName]
Move branch -> git branch -m [oldBranchName] [newBranchName]
Delete Branch -> git branch -d [branchName]
Delete Remote branch -> git push origin --delete [branchName]
Recover Deleted Branch -> git branch [branchName] [SHA1Hash]
From Tag -> git branch [branchName] [TagName]
create from SHA1 hash -> same as above

Push changes

git push <originname> <branchname>

Merge

Steps: 
    1) Checkout the  host branch 
    2) git merge [branchToMerge]

conflict -> git mergetool

Rebasing

replay all commits one at a time and make it look like they were always a part of a branch
Steps:
    1)Checkout the branch you want for Ex: Feature1
    2)Run-> git rebase [branchNameYouWanttoApplyTheFeatureOn]

Cherry-pick

steps:
    1)Checkout the branch
    2)Run -> git cherry-pick [SHA1]

Add multiple origins

add new origin with fetch url -> git remote add --fetch <originName> <fetchurl>
Setup push url for the origin -> git remote set-url <originName> --push <pushurl>
show all origins -> git remote -v show
fetch all the remotes ->  git remote update
remove stale branches -> git remote prune origin

Misc ((not all are Git related)

create-> touch fileName
remove -> rm fileName
See config -> cat ~/.gitconfig


comments powered by Disqus