Skip to content

Instantly share code, notes, and snippets.

@ipeluffo
Last active July 8, 2019 15:24
Show Gist options
  • Save ipeluffo/0f2bc4a688ccc2d8a1ec to your computer and use it in GitHub Desktop.
Save ipeluffo/0f2bc4a688ccc2d8a1ec to your computer and use it in GitHub Desktop.
Git simple reference

Personal Git simple reference

Actions

List branches

git branch

List all branches (including remote branches)

git branch -a

List remote branches only

git branch -r

Update branch from master

git checkout master
git pull
git checkout branch_name
git pull
git merge master
git push origin branch_name

List remote urls

git remote -v

Example:

origin	https://github.com/ipeluffo/some.git (fetch)
origin	https://github.com/ipeluffo/some.git (push)

Create remote Git branch

First, you must create your branch locally

git checkout -b your_branch

After that, you can work locally in your branch, when you are ready to share the branch, push it. The next command push the branch to the remote repository origin and tracks it

git push -u origin your_branch

Teammates can reach your branch, by doing:

git fetch
git checkout origin/your_branch

As described below, the -u option sets up an upstream branch:

For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands.

Delete a Git branch both locally and remotely

Deleting a remote branch:

git push origin --delete <branch>  # Git version 1.7.0 or newer
git push origin :<branch>          # Git versions older than 1.7.0

Deleting a local branch:

git branch --delete <branch>
git branch -d <branch> # Shorter version
git branch -D <branch> # Force delete un-merged branches

git remote branch deleted but still appears in 'branch -a'

git remote prune origin

will remove all such stale branches. That's probably what you'd want in most cases, but if you want to just remove that particular remote-tracking branch, you should do:

git branch -d -r origin/coolbranch

From the git remote docs:

prune

Deletes all stale remote-tracking branches under . These stale branches have already been removed from the remote repository referenced by , but are still locally available in "remotes/".

With --dry-run option, report what branches will be pruned, but do not actually prune them.

Don't forget the awesome

git fetch -p

which fetches and prunes all origins.

Revert commit

git reset --soft HEAD@{1}

Squash last n commits together

http://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git/5201642#5201642

Rename branch (local & remote)

  1. Rename your local branch. If you are on the branch you want to rename:
git branch -m new-name

If you are on a different branch:

git branch -m old-name new-name
  1. Delete the old-name remote branch and push the new-name local branch.
git push origin :old-name new-name
  1. Reset the upstream branch for the new-name local branch. Switch to the branch and then:
git push origin -u new-name

Remove file from all previous commits

git filter-branch -f --tree-filter 'rm -rf path/to/file' HEAD

List the largest files in your git history

git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -10 | awk '{print$1}')"

Change remote's url

git remote set-url origin https://github.com/USERNAME/OTHERREPOSITORY.git

Specify a new remote upstream repository

git remote add upstream https://github.com/USER/OTHER_REPO.git

Configuring a remote for a fork

source: https://help.github.com/articles/configuring-a-remote-for-a-fork/#platform-mac

  1. Open Terminal

  2. List the current configured remote repository for your fork.

git remote -v
origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
origin  https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
  1. Specify a new remote upstream repository that will be synced with the fork.
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
  1. Verify the new upstream repository you've specified for your fork.
git remote -v
origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
origin    https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
upstream  https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)

Sync a fork

source: https://help.github.com/articles/syncing-a-fork/

Git branch has diverged after rebase, so why rebase?

From: https://stackoverflow.com/questions/34678950/git-branch-has-diverged-after-rebase-so-why-rebase

The idea is to rebase only if you haven't pushed yet, to replay your local commits. As soon as you have pushed (and are working in a team), you should not rebase the branch on top of master, as it rewrites its SHA1, forcing you to force push the new state of the branch.

Solution from: https://stackoverflow.com/questions/19016698/git-branch-diverged-after-rebase

git push -f origin branch-name

Clean branches

Taken from: https://stackoverflow.com/questions/28572293/can-i-delete-all-the-local-branches-except-the-current-one

Remove all branches except master:

git branch | grep -v "master" | xargs git branch -D 

Remove all branches except current one:

git branch --merged | grep -v '*' | xargs git branch -D

Others

Steps to setup IntelliJ to sign commits

https://intellij-support.jetbrains.com/hc/en-us/community/posts/206502489-Git-GPG-commit-signing-commit-s-from-IDE-in-effective-way?page=1#community_comment_115000025824

Cheat Sheets

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