Skip to content

Latest commit

 

History

History
224 lines (147 loc) · 3.91 KB

File metadata and controls

224 lines (147 loc) · 3.91 KB

Git cheat sheet

My Git cheat sheet to handle :octocat:

Menu

Verifying

Show the current work state (staged and non staged)

git status

Show the difference of the non staged work

git diff

Show the difference of the work, also with the staged work

git diff --cached

show the last commit(s)

git log -p

Tip: use -- FILENAME to list all the changes made to the given file.

Commiting

Stage all files ready to commit

git add .

Stage certain file ready to commit

git add FILENAME

Tip: use -p to go over all changes in that file by chunks so you can still choose what to stage or not.

Commit latest changes in your previous commit without changing the commit message

git commit --amend --no-edit

Fixup

git commit --fixup REF

Tip: will look like a normal commit on top of your branch. We can rebase these changes in the commit where it belongs to.

Branching

Create a new branch

git checkout -b BRANCH_NAME

Show all local branches

git branch

Tip: use -a to list all remote and local branches

Delete a local branch

git branch -d BRANCH_NAME

Tip: The -d option is an alias for --delete, which only deletes the branch if it has already been fully merged in its upstream branch. You could also use -D, which is an alias for --delete --force, which deletes the branch "irrespective of its merged status.

Pulling

Reset your master branch to remote equivalent

git fetch origin
git reset --hard origin/master

Pull changes from remote server but saving uncommitted changes

This command makes this for you:

  • Save your uncommitted changes locally with git stash.
  • Local changes you made will be rebased on top of the remote changes.
  • Return your uncommitted changes locally again.
git pull REMOTE_ORIGIN TARGET_BRANCH --rebase --autostash

Fetch all changes from remote server

git fetch really only downloads new data from a remote repository - but it doesn't integrate any of this new data into your working files. Fetch is great for getting a fresh view on all the things that happened in a remote repository.

git fetch --all

Tip: add --prune for cleaning outdated local branches

Pushing

Push changes to remote server

git push origin HEAD --force-with-lease

Tip: create an alias like git fp for this in your .gitConfig

Rebase

Rebase active branch to the given REF

git rebase REF -i --autosquash

Tip: use -i to handle it interactive yourself

Rebase active branch on origin/master

git rebase -i origin/master --autosquash

Tip: option --autosquash to handle fixups the correct way can be added as default in your .gitconfig

Abort the current rebase process

git rebase --abort

Continue the current rebase process

git rebase --continue

Stash

Stash your current changes

git stash

Apply your latest stash

git pop

Cherry-picking

Add the commit with REF to your branch

git cherry-pick REF

Revert

Revert a commit with REF from your branch

git revert REF

Advanced

Find and manage the history of all your Git stuff

git reflog

Show me the content of a certain file on another branch

git show BRANCH_NAME:FILE_NAME

Revert the changes of a given file in given commit

git show <COMMIT> -- <FILE> | git apply -R

Tip: first view file changes without | git apply -R

remove your local git repository (Attention!)

rm -rf .git