Git Commands
git init - Sets up new repo in current directory
git remote add origin https://github.com/user/repo.git - Add a remote repo like bitbucket
git status
git config --global color.ui true - Add color to output for better readability
git config --global user.name - Change username
git config --global user.email - Change email address
git config --global merge.tool opendiff - set diff custom diff tool
git config --global alias.co checkout - creates a shortcut for checkout called co, git co
git log
git log --pretty=oneline
git log --pretty=oneline -p - for patch output to see what lines were removed and added
git log --pretty=format:"%h %ad- %s [%an]"
git log --global alias.mylog or alias.lol then just use git mylog or git lol
git log --online --graph -Shows branches and commits on them
git log --until=1.minute.ago
git log --since=1.day.ago
git log --since=1.month.ago --until=2.weeks.ago
git log --since=2000-1-1 --until=2012-12-21
git add
git commit
git commit -a -m 'Comment' - Commits and stages all at once.
git commit --amend -m 'Comment' - Will amend previous commit with whatever is on staging.
git push - Puts files on remote server. The name of our remote is origin and the default local branch name is master. The -u tells Git to remember the parameters, so that next time we can simply run git push and Git will know what to do.
git push origin
- Pushes local branch to remote repo
git push origin : - Adding the ':' will delete the remote branch
git pull origin master - Get changes from remote location (origin)
git diff HEAD - Show diff of changes by others.
git diff HEAD^ - Parent of latest commit
git diff HEAD^^ - grandparent of latest commit
git diff HEAD~5 - five commits ago
git diff HEAD^..HEAD - second most recent commit vs most recent
git diff sha..sha - compare by sha1 hash of two different commits
git diff branch1 branch2 - compare branches
git diff --since=1.wee.ago --until=1.minute.ago
git diff --staged
git reset
- removes file from staging
git reset --soft HEAD^ - undoes the last commit
git reset --hard HEAD^ - undoes the last commit and deletes the files from staging
git checkout --
- Files can be changed back to how they were at the last commit
git branch
- Creates a new branch
git branch - Shows all the branches
git branch -r - Shows all remote branches on the repo
git checkout - Switches between branches
git checkout -b - Creates a new branch and switches to it at the same time
git branch -d - Deletes the branch. Us -D to delete even if unmerged commits exist
git rm
- removes files
git rm --cached - stops tracking changes on a file
git merge
- Merge changes from one branch to another. Do this command from the destination branch.
git help
git clone
git remote -v - lists all the origins
git remote show origin - Shows all the remote branches and if they are tracked
git remote prune origin - Cleans up any deleted branches from the repo
git tag - list all tags
git checkout
git tag -a v.0.0.3 -m "version 0.0.3" - Add a new tag
git push --tags - Push them to the remote repo
git rebase
git rebase --continue
git blame
--date short
Exclude files/folder by adding name to .git/info/exclude
Exclude by pattern as well, *.mp4, logs/*.logs, etc.
Use .gitignore for ignoring log files.
git rebase -i HEAD~3 - redo last three commits
git stash apply - gets the saved files
git stash list - displays all the stashed code
git stash show stash@{0} - shows details of one particular stash
git stash show --patch - shows the file diffs
git stash apply stash@{1} - gets the stashed code at position 2
git stash drop - to remove from the list
git stash pop - runs both git stash apply and get stash drop
git stash save - saves modified files, restores last commit
git stash save "add optional comment"
git stash save --keep-index - causes the staging area not to be stashed
git stash save --include-untracked - causes untracked files to be stashed too
git stash branch
stash@{0} - gets stashed code into new branch
git stash clear - clears away the stash list
-- Deleting History --
git filter-branch --tree-filter 'rm -f
' -- --all - Goes through all branches and removes file from each commit
git filter-branch -f --prune-empty -- --all - Drops empty commits that don't alter any files
-- Line Ending --
git config --global core.autocrlf input - For Unix based systems
git config --global core.autocrlf true - For Windows
-- Cherry Picking --
git cherry-pick
git cherry-pick --edit - Allows you to edit the comment
git cherry-pick --no-commit - Pulls in changes and stages them without committing them
git cherry-pick -x - Adding '-x' adds a commit saying where it was picked from. Only useful if cherry picked from public branch
git cherry-pick --signoff -- SubModules --
git submodule add
cat .gitmodules - Shows contents of the file
git submodule init
git submodule update
git merge - To add commits without a branch
** Push twice when editing submodules.
git push --recurse-submodules=check
git push --recurse-submodules=on-demand - Force submodules to be pushed with the master branch is pushed.
git config alias.pushall "push --recurse-submodules=on-demand" -Creates shortcut to alwasy push submodules.
-- Reflog --
Restore deleted commit.
git reflog - Shows local copy of log for all actions
git reset --hard
git log --walk-reflogs - Gives greater detail for commits
git branch