0

TL;DR Make git "forget" the pending commits to local that are ahead of master.

Hello Folks,

I have a git setup. And here is the problem in step-by-step format:

  1. Create a subproject that has a 10+MB file
  2. git add .
  3. git commit -a -m "blah blah".
  4. Figure out this file shouldn't be in Git at all.
  5. Modify .gitignore by adding the following line largefiledir/

So, now my git is ahead of master, by a commit. I want git to forget this commit, and then redo the "git add . /git commit" process as if it's never seen the newer files before WHILE adhering to the modified .gitignore file.

How on earth do I accopmlish this? I apologize for my question, but I ran into some posts talking about using "git rebase" and last time I tried that, I ended up throwing away my working directory and checking out from master again.

Any help is greatly appreciated.

Black Dynamite
  • 4,067
  • 5
  • 40
  • 75
  • to avoid your pb of "throwing away my working directory" don't forget that you can create a branch before doing anything, so you will impact only that new branch. If you are not satisfied with the result delete the branch, and your old working branch is still here :). So you can do lots of tries at little cost. – jo_ Oct 24 '17 at 07:28
  • Possible duplicate of [How to undo the last commits in Git?](https://stackoverflow.com/questions/927358/how-to-undo-the-last-commits-in-git) – phd Oct 24 '17 at 10:26

2 Answers2

1

if you still didn't push your commit in the remote you can do

git reset HEAD~

make sure to do git status and check if it's clean

for addition it's better to git pull changes and git rebase from the working branch before committing and pushing your changes to the remote repository

you can refer to this link https://www.atlassian.com/git/tutorials/merging-vs-rebasing

0

First you should make the working directories clean (delete the untracked file .gitignore if you are not commit the changes now). And then use git status to double check the working tree is clean.

Then you can use

git reset HEAD~

It will reset the HEAD as previous commit while keeping the subproject as untracted files in your git repo.

Marina Liu
  • 36,876
  • 5
  • 61
  • 74