A list of all the Git Commands that I find useful
Repository
Working with a remote repository
git remote add upstream git://github.com/diaspora/diaspora.git
Status
Current
status
Commits
To view commits on a branch
git log --graph the_branch_name
To view commits on a branch pretty printed
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
To add pretty print alias to your git config
Add alias to config
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
To view pretty printed commits with an alias
Branches
Create and checkout new branch
git checkout -b new_branch_name
Create and checkout a new branch from a branch
git checkout -b new_branch_name dev_branch
Delete unmerged local branch
git branch -D the_local_branch
Rename current local branch
git branch -m new_branch_name
Push local branch to remote Git repository
git push -u origin the_local_branch
Create local branch that fetchs and tracks remote branch
git checkout --track origin/the_remote_branch
Push local branch with detached head to remote Git repository
git push origin HEAD:the_local_branch
Pull down all remote branches locally
git fetch --all
git pull --all
View all local branches including hidden branches
List local git branch names, ordered by most recent commit
git for-each-ref --count=30 --sort=-committerdate refs/heads/ --format='%(refname:short)'
View locally the remote branch
git checkout origin/the_remote_branch
Work locally with the remote branch
git checkout -b the_remote_branch origin/the_remote_branch
Enter merge commit message to merge changes from updated remote branch into local branch to enable push
press "i"
write your merge message
press "esc"
write ":wq"
then press enter
Inspecting Changes with Diffs
Diff a modified file before staging
List file name and status differences between two branches
git diff --name-status branch_one..branch_two
View line differences in a named file between two branches
git diff mybranch master -- myfile.css
View side by side differences in difftool
git difftool path/to/file
Unstaged Changes
Discard a file with unstaged changes
git checkout path/to/file/to/revert
Discard all unstaged changes
Discard all uncommmited changes
Delete unstaged local files
Show files that will be deleted
Delete files
Delete Directories
Undo Add
Commits
Add and commit all modified files, but not newly created files
git commit -am "commit message"
View all commit messages in current branch
git cherry -v development current_branch
List all files in commit
git show --name-only <sha>
Revert Commit
Revert back one local commit
Revert everything from the HEAD back to the commit hash
This will revert everything from the HEAD back to the commit hash, meaning it will recreate that commit state in the working tree as if every commit since had been walked back. You can then commit the current tree, and it will create a brand new commit essentially equivalent to the commit you "reverted" to.
git revert --no-commit 0766c053..HEAD
git commit
Revert branch to preceeding commit
Verify changes to make
git log --oneline
git checkout HEAD~1
git reset hard origin/master
Switch branches to go back to the correct branch
git checkout development
git checkout master
git pull
Revert commit and push to remote repository
git log
git revert 9121c997065b043228763356865c307115c47538
Type message and then esc, :x
Reword current commit message
git commit --amend -m "New commit message"
git push --force origin current_branch_name
Reword previous commit message
List previous commits since branching
git cherry -v parent_branch
Enter interactive rebase for commit N (where N is the number of commits back)
Enable INSERT or Replace mode and type reword as per instructions
Then escape, write and quit
Edit Commit message then escape, write and quit
To quit the interactive rebase
Rebase
Pull remote master updates (since branching) into local branch
git pull --rebase origin master
Undo a rebase
First make a backup
If some goes wrong you can run
Use git relog to find the desired commit
Then reset head to desired commit
git reset --hard HEAD@{5}
Or alternatively rebase saves your starting point to ORIG_HEAD so this command will revert to pre-rebase state
git reset --hard ORIG_HEAD
Diffs
Compare diffs side by side
Launch 'opendiff' [Y/n]: Y
Merging
Merge locally
git checkout local_branch_to_merge_into
git merge local_branch_to_merge
To exit a merge
To undo a local merge
To find commit to revert to
To revert to previous commit
git reset --hard hash_of_commit_to_revert_to
Tracking
Remove untracked files and directories
Dry run
Perform delete of specfied untracked file or directoty
git clean -df directory/to/be/untracked/
Perform delete of all untracked
Overwrite remote with local
Overwrite remote branch with local branch
git push -f origin the_branch_name
Untracking a file that is already tracked
git rm --cached path/to/file/to/stop/tracking
git commit -m "Removed file that shouldn't be tracked"