Git commands

Useful Git commands

  • Get short hash (6 digit)
    git rev-parse --short <version> # HEAD for current 
  • Remove un-tracked files from working directory (link)
    git clean -f  #remove ignored files
    git clean -f -d #remove ingnore files & directories
    git clean -f --dry-run #preview
    git clean -f -x #include ignored files
  • How to clean working directory (link)
    git checkout <file_name> #restore one file
    git checkout HEAD^ foo #file to previous rev
    git reset --hard # restore entire repository 
  • Undo last git commit (link) / Throw away local commit (link)
    git reset --soft HEAD^  # undo & keep files in working dir
    git reset HEAD^ # or HEAD~1
    git reset --hard origin/master  
    git reset --hard <tag/branch/commit id> 
  • Restore modified file
    git checkout HEAD -- <file>
  • Unstage file
    git reset HEAD <file> # undo git add
  • Restore deleted file (link)
    git rev-list -n 1 HEAD -- <file_path> #find out which commit deleted
    git checkout <deleting_commit>^ -- <file_path> #checkout version before (^ or ~1) delete
  • Filter commits from git log (link)
    • ex: View all delete files
    • Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R)
    git log --diff-filter=D --summary #D for delete, A for Added
  • Git diff (link)
    git diff          # diff index against working tree
    git diff --cached # diff staged files (index) against HEAD
    git diff HEAD     # diff HEAD against working tree
  • Show file changes from log (Name Status)
    git log --pretty=oneline --abbrev-commit --graph --decorate #lol
    git log --name-status --graph --abbrev-commit
    git diff --name-status <SHA1> <SHA1>^   #between 2 commits
  • Delete file from git repo (link)
    git rm file1.txt 
    git commit
  • Delete branch (link)
    git branch -D crazyexperiment  #local branch
    git push origin --delete <branchName> #remote branch
  • git branch -m <oldname> <newname> # rename local branch
    git branch -m <newname> # rename current branch
  • Set origin URL (link)
    git remote add origin 
    git pull master # might have to merge
  • Update origin URL (link)
    git remote set-url origin git:// 
  • More to come
Tagged with:
Posted in Git

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: