Resetting a pushed commit?

Some disclosure, I never used git until starting this course.
I want to undo my upgrade commit, but I’m not sure how to do that for pushed commits. I read that the reset command shouldn’t be used if the commit have been pushed. And if I select checkout, I am warned that this will create a detached head, and I won’t be on any branch. I want to continue working on the master branch. Is it possible to rollback while keeping the newer commits on a different branch?

When you reset a git branch you are re-writing history, which is why it’s not a good idea to reset after pushing your branch. The underlying reason for this is because distributed version control, like git, is shared and rewriting shared history can screw with others sharing your project.

Aside from this you can accomplish what you want without having to use a reset if you are willing to do a ‘revert’. In SourceTree this is accomplished by right clicking the commit you would like to revert and saying ‘reverse this commit.’ Git will then undo the changes from that specific commit, committing these to your current branch at which point you can push. You can do this in master, or checkout a new branch and do it there first, it’s up to you.

If you are not sharing your project you COULD do a reset, at which point you would have to force push to your repository (I believe). This is almost always considered a last resort/bad practice.

Hope this helps, let me know if you have specific questions I’ve been using git for many years now and happy to help!

1 Like

Thanks, I think its becoming more clear now! Several times Ben mentions the,“checkout,” as being the best way to perform rollbacks. But what i’m understanding is you first have to create a new branch, then checkout the top commit on that branch. You don’t want to directly checkout a specific commit inside a branch as that can cause problems. Is this correct?

Also, sorry for the questions, but what is the general practice for branching? I take that you create a branch whenever you start on a new feature, then either leave/discard it, or merge it back to the master when finished.

Checking out a specific commit leaves you in a “detached head state” which can be used in some situations but generally is not what you want.

As for branching you are spot on with checking out branches for features and you can expand that to fixes or general “tasks” on your code/project as well. Merge these branches to master in when you are done with your work and confident it works and you have a basic workflow. Some do not go through this when working solo, however I prefer to use branches when I’m on any non-learning project solo or not.

Ask all the questions you like, cheers!

Privacy & Terms