A collection of GIT commands that I personally use because they're really useful. This is a work in progress, please feel free to Contribute.
- Basic Add, Commit, Push
- Basic Pull
- Create new branch
- Merge new branch to master
- Rebase master onto new branch
- Merge vs Rebase
- See all branches
- Delete new branch
- Show list of files changed in a Commit
- Show contents of a Commit
- Revert a Commit
- Revert a Commit from History
- See a summary of changes between the Branch and the Origin
- You can replace master with your branch
- or just use the following to dynamically check your current branch
- See a log of Incoming Changes before you do a
git pull
- See a log of Outgoing Changes before you do a
git push
- Undo the last
git commit
- Undo the last
git commit
without rebasing - Remove git commited files or folders
- Tell git to remember your git login
- Set the cache to timeout after 1 hour (setting is in seconds)
- Pretty Git Log
- Stash Changes
- Re-apply Stashed Changes and delete from Stash List
- Re-apply Stashed Changes but do not delete from Stash List
- View Stashed Changes
- Stash Changes and Pull Updates
- Stash Changes and Merge a Branch
- Push to current branch without specifying the name
- Push to all remote branches
- Push a specific branch to all remotes
- Create an alias for pushing to all remote branches
- Bundle a GIT repo to a zip file
- Overwrite a branch entirely with another branch
- Exclude a file from the git repo without using .gitignore
- Exclude a file from the git repo which is already tracked
git add .
git commit -m "Description"
git push origin master
git pull origin master
git checkout -b new-branch
git checkout master
git merge new-branch
Warning: Never rebase a public/shared branch.
git checkout new-branch
git rebase master
Merge
just means you are combining two branches together.Rebase
on the otherhand means you are moving the base of the branch to a different position. (e.g. When you checkout to a new branch, you basically copy all the commits from the source branch. That separation is called the base of the new branch. Now, Rebasing means you will no longer use that separation because you will move the base to the latest commit of the source branch a.k.a. Rebase).- Anyways, here is The best explanation
git branch -a
git branch -d new-branch
git diff-tree --no-commit-id --name-only -r <commit hash>
git show <commit hash>
git revert <commit hash>
git revert --strategy resolve <commit hash>
# You can replace master with your branch
git diff --stat origin/master
# or just use the following to dynamically check your current branch
git diff --stat @{u}
# You can replace master with your branch
git fetch && git log ..origin/master
# or just use the following to dynamically check your current branch
git fetch && git log ..@{u}
# You can replace master with your branch
git fetch && git log origin/master..
# or just use the following to dynamically check your current branch
git fetch && git log @{u}..
Warning: Create a new branch before rebasing to undo the last commit. Never rebase a public/shared branch.
git rebase -i head~2
# Then delete the 2nd line (e.g. pick XXXXXXX desription)
git reset --soft head~1
(e.g. sensitive data which were accidentally pushed)
git filter-branch --tag-name-filter cat --index-filter 'git rm -r --cached --ignore-unmatch <name of file or folder>' --prune-empty -f -- --all
# Mac Only
git credential-osxkeychain
git config --global credential.helper osxkeychain
# Windows Only
git config --global credential.helper wincred
# Linux Only
git config --global credential.helper cache
# Set the cache to timeout after 1 hour (setting is in seconds)
git config --global credential.helper 'cache --timeout=3600'
# Variation 1
git log --date-order --graph --format="%C(green)%h%Creset %C(yellow)%an%Creset %C(blue bold)%ar%Creset %C(red bold)%d%Creset%s"
# Variation 1 with --all
git log --date-order --all --graph --format="%C(green)%h%Creset %C(yellow)%an%Creset %C(blue bold)%ar%Creset %C(red bold)%d%Creset%s"
# Variation 2
git log --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" --abbrev-commit
# Variation 2 with --all
git log --all --graph --pretty=format:"%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset" --abbrev-commit
git stash
git stash pop
- Useful when applying the stash to multiple branches
git stash apply
git stash list
git stash
git pull
git stash pop
git stash
git merge <branch name>
git stash pop
Instead of using git push origin really-long-name-for-a-branch
, use:
git push origin HEAD
git remote | xargs -L1 git push --all
git remote | xargs -L1 -I R git push R master
git config --global alias.pushall '!git remote | xargs -L1 git push --all'
git pushall
git bundle create <Destination Folder> --all
For example, we want to overwrite master
entirely with dummy
git checkout dummy
git merge -s ours master
git checkout master
git merge dummy
Edit the exclude file and add the folders/files you wish to ignore similar to .gitignore
vi .git/info/exclude
git update-index —assume-unchanged path/to/file.txt
git update-index —assume-unchanged path/to/folder/