Remove Changes from a Commit Before Pushing

Chris Achard
InstructorChris Achard
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

If we've already committed changes that we don't want to push, we can also remove those with git reset, but we're going to reset back to a specific commit, either with:

git reset HEAD~1

or

git reset [HASH]

and that will effectively "undo" the commits before that hash.

Then we can add and commit only the changes that we want.

Instructor: [00:01] We're in @.js here and let's make a function called hello world and that function is just going to alert hello. Then sometime later we close this file and open our index.html file and we're going to put a description in here. That will say, "Here's how to fix some common git mistakes."

[00:26] What we want to do is commit just this index to html file, but if we're careless and we just do a Git Add-A without looking and we commit that, then we have Git Commit-M adds a description to index. Now if we do Git Status, we can see we're ahead by two commands.

[00:48] We almost PUSH, but before pushing we're smart and what we want to do is see what we're about to PUSH. We do a diff on origin master with our current head. What we see is that we have app.js changes that are about to go up, as well as our HTML changes. We realize that we want to get rid of this JavaScript before we push.

[01:11] Let's take a look at the log with git log, one line. What we want to do is undo this commit because this commit is the one with the mistake in it. To do that, we're going to use git reset. Git reset takes us back to where we want to reset to. There's two ways we could specify that.

[01:31] We want to reset back to this commit here. We could say head and then go back one. That's head ~ 1 and that will go to head and then back on the 301 or we could say git reset and then we could copy this hash exactly so we know what we're resetting to. Now we have reset and we have unstaged changes.

[01:55] If we do a git status, now it says we have these changes that are not staged for commit. It's like we didn't even commit them in the first place. For this reason, you really want to be careful. If we had pushed this commit already and then we reset it, then that means we are changing the history that other people may have already downloaded.

[02:14] We only want to use reset on branches that we haven't pushed yet. Now, we could git add index.html, which is the only change we want to add and then double check with git status. We have the index to be committed and we are not committing app.js.

[02:33] Now we can commit a description for index. If we do a git status now, we have app.js not staged for a commit. We are two commits ahead of origin/master. We can do git log --oneline now. We've gotten rid of that old commit. We are replacing it with this new one.

egghead
egghead
~ 20 seconds ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today