Skip to content

Instantly share code, notes, and snippets.

@helderberto
Last active March 18, 2019 20:52
Show Gist options
  • Save helderberto/bf845b280452a4679a4f4a781ebae157 to your computer and use it in GitHub Desktop.
Save helderberto/bf845b280452a4679a4f4a781ebae157 to your computer and use it in GitHub Desktop.
Git Cheat Sheet

CONFIGURATION

See all local configs:

git config --list

Get username or email:

git config user.name
git config user.email

Set username or email:

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

Change default editor:

git config --global code.editor vim

Set editor to diffs/mergs:

git config --global merge.tool vimdiff

Set push/pull to current branch:

git config --global push.default current
git config --global pull.default current

START PROJECT

Initialize new repository:

git init

Download repository:

git clone <link>

Get latest updates from remote:

git pull
git pull origin <branch>

REMOTE

List remote paths:

git remote -v

Set remote path:

git remote set-url origin <link>

COMMIT

Add files to stage:

git add .
git add <file>

Add files to stage and commit message:

git -am "All files included"

Set a commit message:

git commit -m "Files has been included"

Reset files from HEAD:

git reset HEAD .
git reset <file>

CHECKING

Check if something was changed in the branch:

git status

Check the diff from files:

git diff

BRANCHS

List existing branchs:

git branch

Create new branch and join in the branch:

git checkout -b <branch_name>

Create new branch:

git branch <branch_name>

Delete a local branch:

git branch -d <branch_name>

Delete a remote branch:

git push --delete origin <branch_name>

Join in another branch:

git checkout <other_branch_name>

Push files from branch to remote:

git push origin <branch_name>

Push all branchs to remote:

git push --all origin

Merge branchs:

git checkout master
git merge other_branch
git push origin master

UNDOING

Undoing from stage:

git reset <file>
git reset .

Undoing and set files to the last commit:

git checkout <file>
git checkout .

Undoing, but set changes to stage:

git reset --soft HEAD~1

Undoing to last commit, don't set to stage:

git reset --hard HEAD~1

Undoing to specific commit:

git revert hash

Undoing last commit:

git reset --hard HEAD~1 && git push -f origin master

LOGS

View all logs:

git log

View logs filtered by number:

git log -p -2

View summary logs:

git log --pretty=oneline

Make logs better:

git log --pretty="%h = %an, %ar - %s"

%h: abbr hash;
%an: autor name;
%ar: date;
%s: comment

git/pretty-formats

Logs from user:

git log --author=<author_name>

TAGS

Create new tag:

git tag 0.0.1

List all tags:

git tag

Create tag with annotation:

git tag -a 0.0.1 -m "Bump version 0.0.1"

Create tag from commit hash:

git tag -a 0.0.1 <hash>

Push tag to remote:

git push origin 0.0.1

Push all tags:

git push origin --tags

STASH

Stashing takes the dirty state of your working directory — that is, your modified tracked files and staged changes — and saves it on a stack of unfinished changes that you can reapply at any time.

Save all unstaged files in stash:

git stash

Save in stash with annotation:

git stash -u "Message"

List all from stash:

git stash list

Delete last stash entry:

git stash pop

Revert from stash last entry:

git stash apply

Revert from stash specific entry:

git stash apply stash@{number}

References:

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