marinehero / fantastic-git

:christmas_tree: Useful git commands

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool



Reading ๐Ÿ“”


  • gitflow Git extensions to provide high-level repository operations for Vincent Driessen's branching model
  • diff-so-fancy Good-lookin' diffs with diff-highlight and more


  • gitify GitHub Notifications on your menu bar.
  • twitter-for-github Twitter handles for GitHub
  • octotree Code tree for GitHub and GitLab
  • isometric-contributions Render an isometric pixel art version of your contribution graph in Chrome and Safari.
  • github-s3 Shell scripts that make it really easy to archive and restore repositories between GitHub and AWS S3
  • mention-bot Automatically mention potential reviewers on pull requests.


Tag ๐Ÿ”ข

List all tags

git tag

Tag a commit

git tag -a v1.4 -m "my version 1.4"

Delete remote tags

git push --delete origin tagname
git push origin :tagname

Push tag to remote

git push origin tagname

Rename tag

git tag new old
git tag -d old
git push origin :refs/tags/old
git push --tags

Move tag from one commit to another commit

git push origin :refs/tags/<tagname>
git tag -fa tagname
git push origin master --tags

Remote โ˜๏ธ

List all remote

git remote

Rename remote

git remote rename old new

Branch ๐ŸŽ‹

List all branches

git branch

Create a branch

Create the branch on your local machine and switch in this branch

git checkout -b branch_name

Create branch from commit

git branch branch_name sha1_of_commit

Push the branch to remote

git push origin branch_name

Rename local branch

Rename other branch

git branch -m old new

Rename current branch

git branch -m new

Rename remote branch

git branch -m old new                # Rename branch locally    
git push origin :old                 # Delete the old branch    
git push --set-upstream origin new   # Push the new branch, set local branch to track the new remote

Delete a branch

git branch -D the_local_branch
git push origin :the_remote_branch

Commit โœ๏ธ

Undo last commit

git reset --hard HEAD~1

Squash last n commits into one commit

git rebase -i HEAD~5
git reset --soft HEAD~5
git add .
git commit -m "Update"
git push -f origin master

Move last commits into new branch

git branch newbranch
git reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.*1
git checkout newbranch


git cherry-pick hash_commit_A hash_commit_B


Show reflog

git reflog

Get commit

git reset --hard 0254ea7
git cherry-pick 12944d8


git revert --no-commit 0766c053..HEAD
git commit


git commit --amend
git commit --amend --no-edit
git commit --amend -m "New commit message"
git commit --amend -m "New commit message"
git push --force <repository> <branch>

Checkout ๐Ÿ

Checkout a tag

git checkout tagname
git checkout -b newbranchname tagname

Checkout a branch

git checkout destination_branch

Use -m if there is merge conflict

git checkout -m master // from feature branch to master

Checkout a commit

git checkout commit_hash
git checkout -b newbranchname HEAD~4
git checkout -b newbranchname commit_hash
git checkout commit_hash file

Stash ๐Ÿ“ฆ


git stash save "stash name"
git stash

List all stashes

git stash list

Apply a stash

git stash pop
git stash apply
git stash apply stash@{2}

Rebase ๐ŸŽ


git rebase base // rebase the current branch onto base

.gitignore ๐Ÿฏ


git rm -r --cached .
git add .
git commit -am "Remove ignored files"

Index ๐Ÿ“‡

Remove untracked files

git clean

Remove file from index

git reset file

Reset the index to match the most recent commit

git reset

Reset the index and the working directory to match the most recent commit

git reset --hard

Misc ๐Ÿ‘ป

Conflicting git rebase

git checkout --ours foo/
git add foo/

Resolve git merge conflict


git pull -X theirs
git checkout --theirs path/to/the/conflicted_file.php
git checkout --theirs .
git add .
git checkout branchA
git merge -X theirs branchB

Merge from master into feature branch after push

git checkout feature1
git merge --no-ff master


:christmas_tree: Useful git commands

License:MIT License