Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to rewrite a remote history?

A coworker accidentally committed a large binary file which has no business being in source control thus causing the repository to be unusually large. He then pushed this commit to the common server and since then there were a bunch of other commits and pushes. I'm looking for a way to undo that commit or just remove the binary file from it on the remote so that the repository would regain its usual size.

Our common remote is at assembla.com so I don't have direct shell access to it, just git.

Assuming this is possible, what would be the consequences for other downstream nodes? Will everybody need to clone a fresh repository? (That's fine if that's the case)

like image 379
shoosh Avatar asked Sep 24 '14 05:09

shoosh


People also ask

How do I rewrite a previous commit?

You can modify the most recent commit in the same branch by running git commit –amend. This command is convenient for adding new or updated files to the previous commit. It is also a simple way to edit or add comments to the previous commit. Use git commit –amend to modify the most recent commit.

How do I reset my github history?

If you want to restore the project's history as it was at that moment in time use git reset --hard <SHA> If you want to recreate one or more files in your working directory as they were at that moment in time, without altering history use git checkout <SHA> -- <filename>

Does git force push rewrite history?

The --force option for git push allows you to override this rule: the commit history on the remote will be forcefully overwritten with your own local history. This is a rather dangerous process, because it's very easy to overwrite (and thereby lose) commits from your colleagues.


1 Answers

You could rebase your branch to remove the false commit. Then you have to use a forced push to push that rebased branch to the repository at Assembla. All developers then either would have to do a fresh clone or to fetch the remote branch and then do a hard reset of their local branch to the new remote branch.

Basically the commands would be (not necessarily complete):

To remove the false commit:

git rebase -i $(commit id before false commit)

git commit

git push -f origin master (assuming that the branch is master and the remote at assembla is called origin)

git rebase -i will start the interactive rebase mode where you can remove the commit.

To update a developer's clone:

git fetch

git reset --hard origin/master

Or just do a fresh

git clone $(repositoryurl)

Here comes the big fat BUT:

When you do that, you should definitely inform all developers to commit and push their work, before you do the changes. Then do a backup from the old repository, so you're able to restore it, if anything goes wrong

like image 124
dunni Avatar answered Sep 19 '22 10:09

dunni