Avoiding Git panic — Part I: Novice

If you have been using Git for the past few months/years and you’re still struggling with it daily, you landed at the correct place. I expect a little exposure as a prerequisite. Honestly admitting, I ain’t saint & neither a certified expert in Git, but I’ve learned a lot during my quest in resolving issues. I’ll try to make this post from beginners’ to an advanced level. Consider this as a Git FAQ. For the sake of convenience, I’ll be breaking the blog in multiple parts. So, stop panicking!

Git panic
Git Panic


This blog isn’t a walkthrough or TL;DR for some best practice that we follow around in MayaData, that will be covered probably in some other blog. This blog consists of the workaround or the fixes of some gitty situation where people start panicking and screws-up the working directory.

  • Switching your branch when your workspace is dirty or there are some uncommitted changes. Example:

$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
Please commit your changes or stash them before you switch branches.


Method 1:

If you need these changes later, stash them and check out the branch. You can pop changes anytime on any branch you want.

$ git stash
Saved working directory and index state WIP on eslint--fix: b6579b1 [eslint] Run eslint app/ --fix
$ git checkout master
Switched to branch 'master'

To get the changes back again

$ git stash pop

Method 2:

If you don’t want any of those changes and want to discard them.

$ git checkout -- .

ProTip: If you want to discard some file and stash some, use both in the combination of git checkout -- path/to/filename followed by git stash.

  • Left few files during committing and don’t want to create a new for the same
    Let’s assume you did changes in 5 files and forgot to 1 file during committing and now you have an incomplete commit & don’t want to commit again.

Add the files and commit again with --amend flag.

$ git add app/index.js # index.js is missing from commit
$ git commit --amend

ProTip: git commit --amend can be used for removing unwanted files from the commit as well as rewriting the commit message.

  • Moving a single commit
    If you want to move commit from branch to branch
$ git cherry-pick commit-hash
  • Committed on the wrong branch or committed before switching branch
    This happened with me most of the time when I was working on multiple projects simultaneously. To fix this soft reset, you branch with the latest commit(the one you committed), checkout to the desired branch & then commit again
$ git reset HEAD --soft
$ git checkout my-feature-branch
# Now stage files and commit again

ProTip: To reuse your previous commit message do git commit --reuse-message=HEAD@{1} or for a shorter version of this git commit -C HEAD@{1}

  • fatal: bad revision while cherry-picking a commit!
    This happens when your local git remains oblivious of the existence of the commit. To fix this, fetch all the branches, and then cherry-pick again.
$ git fetch -a
$ git cherry-pick 1acb2e
  • Too many untracked files
    The scenario when you have so many untracked files in git and you want to get rid of it, git checkout -- . won’t work, not even git stash will work. rm -rf will work, but that may involve too many steps. Then how to get rid of all the untracked files in one shot.

$ git clean -fd

Bonus: Git lifehacks

# The elegant git log
$ git log --all --graph --decorate --oneline --simplify-by-decoration
# Unstage and remove paths only from the index
$ git rm --cached
# View diff on staged file
$ git diff --staged

I hope you find this blog useful during your quest. In the next follow-up blog, I’ll write about Rebase: went wrong, multiple scenarios, and their fixes. Till then, GodSpeed!

Saumya Sharma
Saumya is a Digital Marketing Specialist at MayaData.
Kiran Mova
Kiran Mova is a Passionate Technologist with 17 years of extensive experience working for product companies like Cisco, Lucent, Novell. Kiran has led efforts in performance engineering, simplifying the products in terms of usability, operation and deployment complexities, introducing multi-tenancy and enabling SAAS services in domains like IP/Optical Networks, Storage and Access Management. At MayaData, Kiran leads overall architecture and is responsible for architecting, solution design and customer adoption of OpenEBS and related software. Kiran evangelizes open culture and open source execution models and is a lead maintainer and contributor to the OpenEBS project Passionate about Storage Orchestration. Contributor and Maintainer OpenEBS projects. Chief Architect MayaData Inc. Open Source Dreamer
Murat Karslioglu
VP @OpenEBS & @MayaData_Inc. Murat Karslioglu is a serial entrepreneur, technologist, and startup advisor with over 15 years of experience in storage, distributed systems, and enterprise hardware development. Prior to joining MayaData, Murat worked at Hewlett Packard Enterprise / 3PAR Storage in various advanced development projects including storage file stack performance optimization and the storage management stack for HPE’s Hyper-converged solution. Before joining HPE, Murat led virtualization and OpenStack integration projects within the Nexenta CTO Office. Murat holds a Bachelor’s Degree in Industrial Engineering from the Sakarya University, Turkey, as well as a number of IT certifications. When he is not in his lab, he loves to travel, advise startups, and spend time with his family. Lives to innovate! Opinions my own!