Skip to content

Instantly share code, notes, and snippets.

@kenny-evitt
Last active May 9, 2017 19:44
Show Gist options
  • Save kenny-evitt/6210688 to your computer and use it in GitHub Desktop.
Save kenny-evitt/6210688 to your computer and use it in GitHub Desktop.
Various commands for Git
# Remove a file from the repository, but not the file system; the 'f' option forces the removal; the 'r' option performs a recursive removal (if a directory is specified)
git rm -f -r --cached './Mri.Web.Mvc/DocumentArchiveErrors_*.xls'
# Fix commits (not already pushed to a remote repository); also, see the much more verbose example below
git rebase --interactive $parent_of_flawed_commit
git rebase --interactive flawed_commit_hash^ # Note the '^'
# Change message of last commit; this is only safe is it has not already been pushed to a remote repository
git commit --amend -m "New commit message"
# List commits made to all branches of the local repository that have not been pushed to (all? any?) remote (repository)
git log --branches --not --remotes
# List the last three commits in the current branch
git log HEAD~3..
git log -3
# See diff details for the last commit
git log -1 -p
# See diff 'stats' for the last commit
git log -1 --stat
# See the diff between the current committed version and the working copy
git diff HEAD README.md
# Use Compare It! as the diff tool
git difftool -t compare-it -y README.md
git difftool -t compare-it -y --cached README.md # Compare the latest committed version and the current staged ("cached") version
# List tags in a remote repository
git ls-remote --tags kiln
# Rename remote
git remote rename origin kiln
# Interactive 'add', i.e. for staging partial changes
git add -i
# Merge commits from the branch 'case165', but not as a fast-forward merge (i.e. retain branch history)
git merge --no-ff case165
# List commits made to remote repo not yet merged into local repo
git fetch kiln
git log master..kiln/master
# Delete branch 'case196' locally and then from the 'kiln' remote repo
git branch -d case196
git push kiln --delete case196
# Amend a previous commit [with the hash starting "27e9e787"] (that's not the last commit) with *some* of the currently un-commited changes
git stash # Save all of the un-commited changes and remove them from the Git working tree
git rebase --interactive 27e9e787^ # Note the '^', which means the previous commit from the one with the specified hash
# For the interactive rebase, in vi(m), edit the commit you want to edit by changing "pick" to "edit" and then 'saving' the changes.
git stash pop # Un-stash all of the un-commited changes
# Stage the changes that you want to add to the previous commit; I used the Git GUI to state one hunk for one file.
git commit --amend # Add the changes to the commit being edited
git stash # Save the other un-commited changes and remove them from the Git working tree
git rebase --continue # Finish the rebase and reset the HEAD to the latest commit (?)
git stash pop # Un-stash all of the remaining un-commited changes
# Squash all changes into a single change by merging from branch 'master' without committing
git merge --no-commit --squash master
# Undo a merge commit (if it's the last commit)
git reset --hard HEAD~1
# Create a local branch from an upstream branch (e.g. for working with forks for reviewing GitHub pull request)
git checkout -b miraleung-electron-name-change upstream/atom-shell # Where 'upstream' is a local remote
# Stage only part of a new file
git add -N new-file
git add -p
# Page the output of `git status`
git --paginate status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment