Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete unpushed git commits, but keep all local work

Tags:

git

git-commit

I have some massive files in my local directory, included in a commit which I think are preventing my git push from proceeding:

Counting objects: 194, done.
Delta compression using up to 4 threads.
Connection to bitbucket.org closed by remote host.
fatal: The remote end hung up unexpectedly
Compressing objects: 100% (190/190), done.
error: pack-objects died of signal 13
error: failed to push some refs to 'myrepo'

At least, I'm fairly sure it's the size of files is causing the push to fail. I just want to delete the record of the commit, so I can go back in and add a .gitignore for those files, commit and push again.

A git status gives:

On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean

It is possible to remove the commits, whilst retaining all the changes in my working directory? I'm struggling to find an answer that doesn't seem to involve resetting my repository to an earlier commit from the master (I want to keep all my recent local work!). Any advice would be much appreciated.

like image 486
Tom Avatar asked Dec 11 '22 18:12

Tom


2 Answers

Run:

git reset origin/master 

This moves the basis commit back to origin/master, but leaves the working tree as is. From this point you can add your .gitignore and do a new commit.

The commit will still exist in your local repo, but will not be ancestors of your new commit, and hence won't be transmitted when you do a push.

like image 176
Gary van der Merwe Avatar answered Dec 24 '22 11:12

Gary van der Merwe


You can use git reset --soft <commit id> for this purpose. To get quickly 3 commits back, you can use HEAD^3 in place of the commit id.

This commands will reset the commit state to an earlier date, without changing any of your files. This will have as effect that all the files that you did change is the removed commits as changed, and then you can commit them again without the large files.

From man git-reset:

--soft

Does not touch the index file or the working tree at all (but resets the head to <commit>, just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.

like image 43
Ferrybig Avatar answered Dec 24 '22 11:12

Ferrybig