SOFTWARE

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

Practical answers to day-to-day problems

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

The Git Cheatsheet 🗒

S️toring changes without committing them

This is a simple one, just run:

Getting rid of all uncommitted changes

Sometimes you want to try something and it just doesn’t work out. To get rid of all changes you made since the last commit, just run:

Syncing your fork with the main repo

When you fork a project, it’s important that you keep your fork up-to-date to avoid complicated merge conflicts when you make a pull request, or simply to make sure you have all the new features and security patches.

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

Erase the last X commits

Made some commits that you ended up needing to revert? You can do so in two ways:

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!)

If you want to get rid of all your "fix typo" commits and join them all together into one, you can do so with:

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

Checkout the state of the project at a past commit

To go back in time and see the state of your project at a given commit in the past, first run git log to see the commit history and select the commit you wish to go back to.

Ignoring a file you already added to Git

We’ve all been there — adding or committing something we shouldn’t have. To remove the file from Git tracking while keeping it in the system, just do:

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

Adding to a commit after committing

If you want to change your commit message, or add a new file to it, you can use git ammend .

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

Removing a file from Git and pruning its entire history

If you ever push sensitive data to a remote repository (e.g. on GitHub), you’ll not only need to remove the file from Git tracking, but also delete its entire history.

Record merge conflict resolutions

To avoid having to resolve the same exact merge conflicts multiple times, you can enable a Git cache of merge conflict resolution. This will store how a merge conflict was resolved and automatically resolve the same conflict if it comes up again:

git config --global rerere.enabled true

Commits made on the wrong branch

If you made a commit on the wrong branch, you should be able to use our knowledge about erasing commits to solve the problem, like so:

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

To change the name of a branch, use git branch -m . You can either change the name of the current branch:

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

Finding the commit with a bug

If you run into an issue that you know is unrelated to your commit, you’ll need to determine what commit in the past introduced this problem. This is common with tests, for example, when they aren’t passing due to a test completely unrelated to your work.

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

Medium's largest active publication, followed by +717K people. Follow to join our community.

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

Medium's largest active publication, followed by +717K people. Follow to join our community.

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

Medium's largest active publication, followed by +717K people. Follow to join our community.

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