Skip to content

Instantly share code, notes, and snippets.

@bouyagas
Forked from magicznyleszek/git.md
Created September 2, 2017 23:28
Show Gist options
  • Save bouyagas/d06b31d502c9132d2c640492a4c874b8 to your computer and use it in GitHub Desktop.
Save bouyagas/d06b31d502c9132d2c640492a4c874b8 to your computer and use it in GitHub Desktop.
Git Cheatsheet

Git Cheatsheet

Some reference notes of the most common used commands.

Contents:

  1. Configuring Git
  2. Starting a new project
  3. Managing project
    1. Change commits author in history
    2. Merging conflicts
  4. Managing branches
  5. Viewing history

Configuring Git

Set up new global name and email:

git config --global user.name "John Doe"
git config --global user.email [email protected]

Change current repo name and email:

git config user.name "smutnyleszek"
git config user.email "[email protected]"

Colors in terminal for Git:

git config --global color.ui true

Turn on case-sensitive file name changes:

git config core.ignorecase false

Changin a remote url in config:

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

Starting a new project

New project

git init
git add .
git remote add origin URL
git commit -m "initial commit"
git push origin BRANCHNAME

Clone project

git clone URL [DIRECTORY]

Managing project

Self-explanatary

git status
git add PATH
git rm PATH
git mv OLDFILE NEWFILE

Create commit and push it to remote

git commit -m "Message"
git push origin BRANCHNAME

Good commit message template:

A short summary in max 72 characters.

- first done thing after a blank space in max 72 characters
- second done thing in max 72 characters

Undo git commit before push

git reset --soft HEAD~1

Show differences:

git diff

Revert to current HEAD:

git reset --hard HEAD

Check if file is being tracked by git

git ls-files FILENAME --error-unmatch

Change commits author in history

Create fresh repo clone:

git clone --bare https://github.com/user/repo.git
cd repo.git

Rewrite history:

#!/bin/sh
 
git filter-branch --env-filter '

OLD_EMAIL="[email protected]"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="[email protected]"

if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

Push corrected history:

git push --force --tags origin 'refs/heads/*'

Merging conflicts

To checkout your own version:

git checkout --ours -- FILE
git add FILE

To checkout the other version:

git checkout --theirs -- FILE
git add FILE

Managing branches

Create a branch

git branch BRANCHNAME

Switch to a branch

git checkout BRANCHNAME

List branches: local, remote or all

git branch
git branch -r
git branch -a

List branches with sha1 and last commmit

git branch -v

Delete branch

git branch -d BRANCHNAME

Delete branch on remote

git push origin --delete BRANCHNAME

Refresh list of remote branches

git remote update origin --prune

Make local branch track remote branch from origin

git branch -u origin/BRANCH BRANCH

Rename current branch

git branch -m NEWNAME

Rename current branch on remote

git push origin --delete OLDNAME
git push --set-upstream origin NEWNAME

Merge current branch with BRANCHNAME

git merge BRANCHNAME

Merge current branch with BRANCHNAME with only one output commit

git merge --squash BRANCHNAME
git commit -m "EXPLANATION"
git push

Send commit to branch

git push origin BRANCHNAME

Viewing history

Show log with or without default pager:

git log
git --no-pager log

Show only a range of history

git log OLDER_SHA..NEWER_SHA

Some useful flags:

  • --graph - show branching and merging
  • --oneline - commit info in one line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment