Skip to content

Instantly share code, notes, and snippets.

@InsulaVentus
Last active January 24, 2019 08:28
Show Gist options
  • Save InsulaVentus/f1a6f14f7365678abae9 to your computer and use it in GitHub Desktop.
Save InsulaVentus/f1a6f14f7365678abae9 to your computer and use it in GitHub Desktop.
Miscellaneous git commands with descriptions

Reset commited changes to one or more files. (HEAD~1) refers to the commit prior to HEAD

git checkout HEAD~1 -- file1/to/restore file2/to/restore

Pull a branch without checking it out

git fetch origin name_of_branch:name_of_branch

Move branch pointer to different commit

git branch -f branch-name new-tip-commit

List all branches and their current commits

git show-branch

Delete remote branch

git push origin :name_of_branch

Find all remote branches

git remote update

Track remote branch, create and switch to local copy

git checkout --track origin/name_of_branch

Create remote branch ready for pull

git push -u origin name_of_branch

Edit global git config

git config --global --edit

Reset last X commits

git reset --soft HEAD~X

Create branch from a given commit

git branch name_of_branch 6436d7ac

See all branches merged into current branch

git branch --merged

See all branches not merged into current branch

git branch --no-merged

List all branches containing a given commit

git branch --contains 6436d7ac

Create a copy of a given commit in current branch

git cherry-pick 6436d7ac

Remove untracked files (use -n for preview, -d for directories)

git clean -f

Print path to gitignore

git config --get core.excludesfile

Set gitignore path

git config --global core.excludesfile path_to_gitignore

Ignore an already tracked file

git update-index --assume-unchanged name_of_file (--no-assume-unchanged to reset)

Checkout an older commit

git checkout 6436d7ac . (for current branch) git checkout 6436d7ac -b name_of_branch (for another branch)

Determine which branches contain a given commit

git branch -a --contains 6436d7ac

Search for remote branches by name

git branch -r | grep 'DRAGE'

During merge, choose either theirs or ours

git checkout --theirs file_name
git add file_name

Remove stale branches from remote

git remote prune origin

Find best common ancestor between two branches (usually branching point)

git merge-base name_of_branch1 name_of_branch2

List all merge commits

git log --merges

Prevent loss of merges when rebasing

git rebase --preserve-merges origin/name_of_branch name_of_branch 

Determine which commmits exists localy but not remotely

git log origin/name_of_branch..name_of_branch

Rewrite most recent commit message

git commit --amend

Print commit log by author

git log --author="name regex" (--name-only for affected files only)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment