Skip to content

Instantly share code, notes, and snippets.

@dboune
Forked from guilhermewop/git-useful-commands.sh
Last active September 17, 2017 16:42
Show Gist options
  • Select an option

  • Save dboune/9483d25080ef8e2f08af to your computer and use it in GitHub Desktop.

Select an option

Save dboune/9483d25080ef8e2f08af to your computer and use it in GitHub Desktop.

Git Command Reference

Branches

List Branches

Local only

git branch

Local and Remote

git branch -a

Remote only

git branch -r

Rename branch

Local

git branch -m <old_branch> <new_branch>
git branch -m feature/old_branch feature/new_branch

Removing a branch

Local

git branch -D <branch>
git branch -D feature/branch

Remote

git push <remote-name> :<branch>
git push origin :feature/branch
git push <remote-name> --delete <branch>
git push origin --delete feature/branch

Tags

List Tags

Local

git tag

Remote

git ls-remote -t <remote-name>

Rename a tag

git tag new_tag old_tag
git push --tags
git push origin :refs/tags/old_tag
git tag -d old_tag

Add a tag

git tag -a {version} -m "Add tag for version {version}"

Then push tag to the origin

git push --tags

Commits

Rename commit message

Reference: http://darrinholst.com/post/359817782

git rebase -i <commit-hash>

Cleanup

List ignored files

git status --ignored

Remove ignored files

git clean -fdX

Keeping a clean GitHub fork.

Reference: http://blog.evan.pro/keeping-a-clean-github-fork-part-1
Wayback: https://web.archive.org/web/20130325080957/http://blog.evan.pro/keeping-a-clean-github-fork-part-1

Add an upstream remote

git remote add upstream <git-project-url>

Add any other remotes you may want

git remote add <label> <git-project-url>

Merge upstream into already checked out master

git fetch upstream
git merge --ff-only upstream/master

Shortcut for the previous in one command

git pull --ff-only upstream master

Update your fork

git push origin master

Fetch any other remotes you may have added

git fetch --all

Working with feature branches

Checkout a new feature branch

git checkout -b feature/<feature-name>

Push feature branch to origin

git push origin feature/<feature-name>

Properly excluding IDE-specific files with Git

Reference: http://blog.evan.pro/properly-excluding-ide-specific-files-with-git
Wayback: https://web.archive.org/web/20140326213018/http://blog.evan.pro/properly-excluding-ide-specific-files-with-git

git config --global core.excludesfile ~/.gitignore_global

Misc

Get the size of untracked files

git status --porcelain -u | sed 's#^...##' | while read file; do gdu -s -b "$file"; done | sort -nr | awk ' { total = total+$1 } END { printf("%.2fMb\n",total/(1024*1024)) } '

Note: On Linux, gdu should be changed to du, and on OS X you must install the coreutils package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment