Coconut
Coconut

Reputation: 184

Remove unpushed commit without removing local changes

I was writing some computational codes locally and sync them with Github. I recently wrote some new code and tested it with some large size data file inside the folder. I did not pay attention to the size of the file, until I add and commit the whole folder and try to push it. However errors indicating file size is too big to push.

I am quite new to git, so I figured just to remove the generated data file from the folder, but only realized the commit has already log the oversized data file. I look around and found options like git revert command. But it seems like once I git revert, all the code I've updated with go back to the previous commit. I just wanna recommit without the oversized data file included so that I can make a successful push. Is there any one can help me with that?

Upvotes: 1

Views: 526

Answers (3)

Manuel Schmidt
Manuel Schmidt

Reputation: 2427

You can simply tell git to remove the file from index and then amend your last commit.

git rm --cached path/to/your/file
git commit --amend

With git rm --cached you just remove the file from index but keep it on your file system and with git commit --amend you modify your last commit including these changes. Every change you have staged will be incorporated. You get even the chance to modify the commit message.

Upvotes: 2

Soumya Kumar
Soumya Kumar

Reputation: 47

git reset --soft HEAD~1
git reset path/to/file
git commit

Explanation

git reset -soft HEAD~1 will reset your last commit (HEAD~1 is reference to the last commit) but keep the changes staged, from there we can unstage the file by git reset path/to/file.

Upvotes: 1

Jack Gore
Jack Gore

Reputation: 4252

Try running:

$ git reset HEAD^

it will remove your latest commit, but keep the changes in your working tree so you can create a new commit but modify what is included.

Upvotes: 1

Related Questions