SOFTWARE

Git Ready: A Git Cheatsheet of Commands You Might Need Daily

Practical answers to day-to-day problems

Yakko Majuri
Aug 31, 2020 · 6 min read
Image for post
Image for post

The Git Cheatsheet 🗒

S️toring changes without committing them

Getting rid of all uncommitted changes

Syncing your fork with the main repo

git remote add upstream <upstream_repo_url>
git fetch upstream
git merge upstream/master

Erase the last X commits

git reset HEAD~2 # undo the commits but keep the changesgit reset --hard HEAD~2 # undo the commits and discard changes

Squash various commits into one (without rebase!)

git reset --soft HEAD~2 && git commit -m "your message"

Checkout the state of the project at a past commit

Ignoring a file you already added to Git

git reset <file> && echo <file> >> .gitignore

Adding to a commit after committing

git commit --amend -m "<new_message>"
git add <file> && git commit --amend

Removing a file from Git and pruning its entire history

Record merge conflict resolutions

git config --global rerere.enabled true

Commits made on the wrong branch

git branch <new_branch> && git reset HEAD~2 --hard 
git checkout <desired_branch> && git merge <branch_with_commits>
git checkout <branch_with_commits> && git reset HEAD~2 --hard
git checkout <desired_branch>
git cherry-pick <branch_with_commits> <branch_with_commits>~2
git checkout <branch_with_commits> && git reset HEAD~2 --hard

Changing a branch name

git branch -m <new_name>
git branch -m <old_name> <new_name>

Finding the commit with a bug

git bisect start
git bisect bad
git bisect good <commit_hash>
Bisecting: 2 revisions left to test after this (roughly 3 steps)[6ca4a67aeb4b0d9835ecf15e44505c48f93642c9] my-branch

The Startup

Get smarter at building your thing. Join The Startup’s +776K followers.

Yakko Majuri

Written by

Writing code and writing about writing code. Exploring the underrated topics in Software Development. I speak for myself only.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +776K followers.

Yakko Majuri

Written by

Writing code and writing about writing code. Exploring the underrated topics in Software Development. I speak for myself only.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +776K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store