DEV Community

loading...

Git cheatsheet

Giulia Chiola
Front-end Developer. 👾 Media engineer. ⚡ Facing life one bug at a time. 🔥 Easily bribed with chocolate. 🍫 she/her.
Originally published at giuliachiola.dev Updated on ・2 min read

Few commands I found very useful during development.

Command Description
git commit -am "message" add and commit all trakced files
git fetch --all --prune fetch all remotes, delete remote branches which are dead
git reset --merge abort merge, reset as before (also if there are local commit not pushed yet)
git merge --strategy-option theirs [branch] merge branch into current branch, keeping their work in conflicts
git branch --unset-upstream remove upstream branch
git reset myfile.js remove from staging area
git commit --amend --no-edit amend without edit message
git commit --amend --no-edit --patch amend without edit message + choose interactively which changes to commit
git checkout - switch to previous branch
git checkout [branch] -- path/to/file.scss get file from another branch and copy to current branch
git stash -u stash all files (also untracked ones)
git reset --soft A remove files, but still available in staging area
git reset --mixed A git reset A (default) remove files also in staging area
git reset --hard remove files as they never existed
git stash clear delete all stashes
git fetch origin; git reset --hard origin/main restore as remote main branch
git log --tags --simplify-by-decoration --pretty="format:%ci %d" show tags details
git log --graph --abbrev-commit --decorate show commits with decorative branches
git log --decorate --oneline --graph --date-order master@{"4 days ago"}..master show commits of the last 4 days in a compact graph
`git diff --name-only uniq
{% raw %}git push origin :feature/branchname delete remote branch
git push origin --delete feature/branchname delete remote branch
git push origin --all push all local branches
git fetch --prune --prune-tags remove local tags, align tags to remotes
git ls-remote --tags origin list all remote tags

⚡️ Bonus tip

If there is a command you use often, you can save it as a global git alias.

git config --global alias.fixup 'commit --amend --no-edit'
Enter fullscreen mode Exit fullscreen mode

and then using it

git fixup
Enter fullscreen mode Exit fullscreen mode

Thanks to DarkWiiPlayer for pointing that out. 😎

Rename a git tag

git tag [new] [old]
git tag -d [old]
git push origin :refs/tags/[old]
git push --tags
Enter fullscreen mode Exit fullscreen mode

When you fetch remote tags, be sure you have the updated tags

git pull --prune --tags
Enter fullscreen mode Exit fullscreen mode

GitLab branch compare

  • Open the left sidebar
  • Click on Repository > Compare
https://gitlab.com/giuliach/super-blog-11ty/-/compare/[source]...[target]
Enter fullscreen mode Exit fullscreen mode

Example:

https://gitlab.com/giuliach/super-blog-11ty/-/compare/main...develop
Enter fullscreen mode Exit fullscreen mode

Discussion (2)

Collapse
darkwiiplayer profile image
DarkWiiPlayer • Edited

git commit -am "message"

There's nothing wrong with doing this every now and then, but I've seen repeatedly that people tend to use -am as their only way to make commits, which leads to poorly written/formatted commit messages and committing too many files. Don't overuse!

git commit --amend --no-edit

This combination is very useful, but it can be even better: git config --global alias.fixup commit --amend --no-edit --patch ;)

One personal addition:

git log --decorate --oneline --graph --date-order master@{"4 days ago"}..master

To see the commits of the last 4 days in a compact graph :D

Collapse
giulia_chiola profile image
Giulia Chiola Author

Thanks @darkwiiplayer , I really appreciated your feedback! 🙃

About the dangerous --all flag you are absolutely right. We should not overuse it! We should all strive towards making smaller commits involving a single functionality, and only then we could use the -a flag safely.

For the other tips you suggested, I edited the original post and added them. 😎 Thank you!

Forem Open with the Forem app