DEV Community

Kelly Stannard
Kelly Stannard

Posted on

An easy safety in git

So, everyone's git right of passage is to nervously try to run a git command and then realize with horror that you just lost your work on your local repo. Then you run to google and try to find out how to reverse what you just did and run some archaic commands to find the lost commits.

With a small amount of foresight, you can just create a new branch called "backup" pointing to your work and reset back to it if necessary.

$ git branch -C backup
# mess up your git branch
$ git reset backup --hard
# good to go
Enter fullscreen mode Exit fullscreen mode

Discussion (6)

Collapse
natescode profile image
Nate

Why make a branch when you can just commit? Merges can be undone as well.

Collapse
kwstannard profile image
Kelly Stannard Author

I am unclear how that is any easier

Collapse
natescode profile image
Nate • Edited on

Not easier but better. You're using GIT wrong.

The whole point of commits is they're save points. You don't need 300 branches. Branches are to separate incomplete features.

If you want to save a solid point then you MERGE into your master / main branch from develop, but don't branch to save work.

PLEASE don't ever do this professionally.

Thread Thread
kwstannard profile image
Kelly Stannard Author

I feel like maybe you are leaping to conclusions about what I am trying to do here. I am not even talking about develop or main. This is about doing stuff locally.

Thread Thread
kwstannard profile image
Kelly Stannard Author

I am not sure where you got 300 branches from either

Thread Thread
natescode profile image
Nate • Edited on

Nope

"nervously try to run a git command and then realize with horror that you just lost your work"

  1. Why are you losing work? You're doing something wrong if you are. Merges can easily be undone.
  2. Branches aren't for backups. Again, why do you need a backup? That's what commits are for.

Wasn't try to be mean. Just that isn't the proper way to use GIT. Doesn't make sense.

You can have master and develop branches locally. I branch locally all the time.