2 minutes
Git Commands
Setup & Configure
dont go alone here …
-
git –help
-
git help [command]
Configuration and tweaking
git config
--local //this repo
--global //all repos
Getting & Creating Projects
Create Repo
-
git init [name] //this will create repo inside dir.
-
git clone //this will download remote repo
Basic Snapshotting
So whats happening?
- git status //show you current status
Show changes in files
git diff //unstaged
--cashed //staged
--staged //staged
[HEAD] //staged vs. unstaged
[SHA1] [SHA2] //commit lvl. diff
Add files to index
git add //add to staged
[] //optional
<> // required
- git add -A . //add everything
Revert changes and rewrite history
git reset
[file] //unstage changes
--soft<SHA> //reset change to SHA and keep files
--hard<SHA> //reset changes to SHA and drop files
Save staged changes to repo history
git commit
-m "<message>" //skip text editor
-a //add all tracked
--amend //modify last commit
Branching & Merge
Managing and tracking branches
git branch //list all branches
[name] //create new branch
-d //delete local branch
Changing branches
git checkout
<branch> //change to branch
-b<name> //make and checkout new branch
[SHA] [file] //checkout single file
Display history: commit, SHA, message
- git log [branch]
Merge branch into current one
- git merge
Save and hide tracked files to stash stack
git stash //add to stash stack
list //show all stashed changes
apply //apply last stash changes
drop //drop last stash changes
pop //apply + drop
--keep-index
--include-untracked
Patching
Apply supplied commit to current branch
- git cherry-pick
Detach current branch and apply on top of selected branch
git rebase <branch>
-i //list all commit in text editor to select operations
p pick //use selected commit
s squash //squash with previous
d drop //drop selected commit
Undo commit by adding negative commit
- git revert
Sharing & Updating Projects
Sync local repo with remote. Download objects and refs.
- git fetch
Fetch and merge remote branch into it’s current local active counterpart
- git pull
Update remote repo with selected objects and references
- git push [remote] [branch]
Show author of changes to a file
- git blame
This will untrack work, Remove untracked files from repo
git clean
-f //force
-d //files and directories
-x //including ignored
-X //only ignored files
-n //dry-run -> printout
Optimize local repo if it is sluggish
- git gc
Voodoo magic when unexpected deletion or any such event occurs
- git reflog
And finally that’s about it :)