Skip to content

Instantly share code, notes, and snippets.

@barbara-rogar
Last active June 25, 2021 07:04
Show Gist options
  • Save barbara-rogar/19d07195f60826db9ca0af8b8408f199 to your computer and use it in GitHub Desktop.
Save barbara-rogar/19d07195f60826db9ca0af8b8408f199 to your computer and use it in GitHub Desktop.
List of helpful git commands

Git cheat sheet

Set up

Set up global .gitignore

git config --global core.excludesfile '~/.gitignore'

Set upstream

git config --global push.default current

No Xcode

alias git="/usr/local/git/bin/git"

Set up name and email

git config --global user.name "Your Name"
git config --global user.email you@example.com

Set up fast forward pull

git config --global pull.ff only

Generating SSH keys

Use this.

Branches

Listing branches

Local and remote branches
git branch -a
Only remote branches
git branch -r
Only local branches
git branch
All prune branches
git remote prune origin --dry-run
All branches that match a pattern
git branch --list <pattern>

Pattern accepts wildcards

Deleting branches

Remote branch
git push origin --delete <branch>
Local branch with umerged status warning
git branch -d <branch>

Local branch without warning (force delete)

git branch -D
Delete multiple stale remote-tracking branches
git fetch -p

Difference between master and origin/master

master is a local "master" branch, while origin/master is a local copy of the branch named "master" on the remote named "origin".

Renaming a local branch

git branch -m <old_branch> <new_branch>

Add changes

Add all but one file

git add -u 
git reset -- path/to/file

Undoing changes

Checkout

git checkout <file> || <SHA>

On a file level, it removes changes from a working directory (instead of stagging area like git reset).

On a commit level, it switches between branches or commits.

Reset

git reset  <file> || <SHA>

On a file level, it removes a file from the stagging area.

On a commit level, it removes commits by moving the tip of a branch to a different commit.

Unlike reverting, reset changes git history.

Soft

git reset --soft <SHA>

Soft resetting has no affect on working directory or stagging area, all changes are stagged.

Mixed

git reset --mixed <SHA>

Mixed resetting is the default option.

It leaves all changes in the working directory, but not in the stagging area.

Hard

git reset --hard <SHA>

Changes added in the resetted commit are removed, as well as all uncommited changes.

Recovering from a reset

git reflog
git reset HEAD@{<n>}
Recovering deleted (uncommited) file
git reset HEAD <file>
git checkout <file>
Abort merge
git reset --hard HEAD
Unstaging all changes
git reset
Ustage changes in a specific file
git reset <file>

Revert

git revert <SHA>

Unlike reset and checkout, it's not possible to revert files, only commits.

Reverting undoes a commit by creating a new commit. It does not change the commit history.

Change a commit message

git commit --amend

With this command it is possible to change a commit message or changes in a commit.

Squashing commits

git checkout master
git pull origin master
git checkout -b <branch-squash>;
git merge --squash <branch>

Fetch

git fetch

Fetching remote changes, but doesn't merge them in local (origin/<master> is updated, but master is not).

Merge

git merge <branch>

Merging changes (usually after fetching).

Pull

Equivalent of git fetch + git merge.

Remote update

Similar to git fetch -a.

Stash

git stash

Applying stashed changes

Apply
git stash apply

Apply changes from the latest stash on stack and keep the stash on stack.

Or find a specific stash:

git stash list

and then apply it:

git stash apply stash@{<n>}
Pop
git stash pop

Apply changes from the latest stash and remove latest stash from stack.

(git stash pop == git stash apply + git stash drop).

Rebase

Non-interactive rebase

git checkout master
git pull
git checkout <branch>
git rebase master

and merge rebased changes on master branch:

git checkout master
git merge - -no - -ff <branch>
git push origin master

Interactive rebase

git rebase -i HEAD~<n>

Tags

Create a tag

git tag <tag_name>

Push tags

git push origin --tags

List tags

git tag 

Log

Log everything

git log

Log commits in one line

git log --oneline

Log all commits in one line (commit SHA and first line of the commit message).

Reflog

git reflog

If you have to use this one, something is totally messed up :)

Formatted logs

git log --all --format='%cN %cE'

List of committers with their email.

More about formatting here.

Cherry pick

git fetch
git checkout <branch-cp>
git pull origin <branch-cp>
git cherry-pick <SHA>
git push origin <branch-cp>

SHA is the commit identifier from merged pull request (from the oldest one to newer).

Split last commit into 2+ separate commits

git reset HEAD~
git add <file>
git commit -m”message here”

Repeat as many times as needed.

Split changes from one file
git add -p <file>

Allows splitting code into chunks (y,n,s,...); gives a choice of commands:

Stage this hunk [y,n,a,d,/,j,J,g,e,?]? ?
y - stage this hunk
n - do not stage this hunk
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help

GitHub stuff

Closing issues across repos with commit messages

fixes user/repo#issueNo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment