Skip to content

Instantly share code, notes, and snippets.

@joaocruz04
Last active June 6, 2022 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaocruz04/8ae5df3628464bf3489ecdbf4fc0bc5e to your computer and use it in GitHub Desktop.
Save joaocruz04/8ae5df3628464bf3489ecdbf4fc0bc5e to your computer and use it in GitHub Desktop.
Git cheatsheet

Git cheat sheet

Setup

Starting project

git init

Branching

Creating branch

git checkout -b <branch_name>
git push origin <branch_name> // If pushing it also to remote

Rename local branch

git branch -m <oldname> <newname>

Committing

Commit staged changes

git commit -m “message”

Splitting a commit

git rebase -i HEAD~<position_in_the_log>
git reset HEAD^
Create commits as desired

Staging

Stage files

git add <file> // single file
git add -A // all unstaged files

Unstage file

git reset HEAD — <file>

Ignore changed files

git checkout .

Pushing

Push changes

git push --set-upstream origin <branch_name> // First time
git push // Remaining times
git push -f // If history changed

Rebasing

Rebase

git rebase <base_branch>

Interactive rebase

git rebase -i HEAD~<position_in_the_log>

Change base branch

git rebase --onto <new_base_branch> <old_base_branh> <target_branch>

1 - branches can be either names or hash values
2 - target branch only if target is not the current branch

Stash

Stash current work

git add -A 
git stash

Load from stash

git stash apply // If only entry
git stash apply stash@{<INDEX>} // If multiple entries

Misc

Check status

git status

Check diff of uncommited files

git diff

Fancy git log

git log --graph --pretty=oneline

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