Skip to content

Instantly share code, notes, and snippets.

@edisplay
Forked from etoxin/git.md
Created December 28, 2017 13:44
Show Gist options
  • Save edisplay/cf1f896cbf42ab679b96f2a44e12512e to your computer and use it in GitHub Desktop.
Save edisplay/cf1f896cbf42ab679b96f2a44e12512e to your computer and use it in GitHub Desktop.
GIT Bash Commands
# search (regex)
git grep "regex"
# list all branches
git branch -a
# list remote branches
git branch -r
# checkout an branch on remote
# make sure you don't use `origin`
git fetch
git checkout branchName
# Create a new branch
# first create a branch
git checkout -b <branchName>
# Create a new branch from an existing branch
git checkout origin/branchName -b newBranchName
# Then push your new branch to the repo
git push origin <branchName>
# Create a branch from a commit
# AKA Recover a deleted branch
git checkout -b <branch> <sha>
# revert all changes in a branch. Removes staged and working directory changes.
git reset --hard
# Resets index to former commit; replace '56e05fced' with your commit code. You can use git log to get commit code
git reset 56e05fced
# revert a file to the most recent commit
git checkout HEAD -- /somePath/file.txt
# undo the last commit. Blow it out of the water.
git reset --hard HEAD~1
# undo your last commit but leave the files from that commit staged.
git reset --soft HEAD~1
# delete local (untracked) files
git clean -f
# If you want to also remove directories, run
git clean -f -d
# to discard changes in working directory
git checkout -- <file>
# Checkout a file from another branch
git checkout origin/branchName -- fileName.txt
# clean a folder
git clean -fxd {dir_path}
# commit a folder/file without staging it.
git commit /folderToCommit -m 'commit msg'
# list all branches (remote & local/remote only)
git branch -a
git branch -r
# Find out all branches a commit is on
git branch --contains <commit>
# display log with Tree
git log --pretty=format:"%h - %cr (%an) %s" --graph
## Merge Master into your local branch
git fetch
git merge origin/master
## a shortcut to this is. They are both the same
git pull origin master
# or, if it's a busy repo.
git pull --rebase <remote name> <branch name>
# list conflicts
git diff --name-only --diff-filter=U
grep -lr '<<<<<<<' .
## Diff a conflict
git mergetool -t opendiff
# pull a branch , merge if conflicted use remote.
git pull -s recursive -X theirs origin ra
# show log with merged files
git log -m -1 --name-only
# Show the changes between two branches.
git diff --name-status master..branchName > changelog.txt
### Stashes
# save a stash
git stash save "My changes."
# list your saved stashes
git stash list
# apply a stash (Where stash@{1} is the stash you want to apply.)
git stash apply stash@{1}
# delete a branch on origin
git push origin --delete <branchName>
# delete a branch locally
git branch -d <branchName>
# remove local branches that are not on remote
git remote prune origin
# Get all commits from a branch. For a release log, changelog etc.
git cherry -v develop mybranch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment