50

So I'm working on a sensitive project with a group and all our sources are on Github. I recently pushed a commit and later realized there were lot of mistakes in my push. I've since fixed all those mistakes on my local copy and am about to push again. However is there any way I can push and overwrite my last commit? My reason being, I don't want the others to look up my initial commit and the changes it had...

Basically I want to overwrite the old commit with my new one.. so no information about the old commit remains for other group members to see.

Machavity
  • 30,841
  • 27
  • 92
  • 100
sparta93
  • 3,684
  • 5
  • 32
  • 63
  • 1
    You can amend your commit (`git commit --amend`), or if the data is more sensitive there are instructions at https://help.github.com/articles/remove-sensitive-data/. – adrianbanks Feb 21 '16 at 23:43
  • 1
    This is changing history. Also, since you've already made another commit, this is most likely going to be a little bit more difficult. For future reference, to change the last commit you made, you can use `git commit --amend`; this will also require a force push. – Jeremy Rodi Feb 21 '16 at 23:43
  • @ABMagil My question has nothing to do with the commit message. It is about the changes and the history. – sparta93 Feb 22 '16 at 00:12
  • 1
    a) The solution is the same. b) The commit message is treated by git the same as the code changes you're committing. If you want to change either, you're changing history as far as git is concerned. – ABMagil Feb 22 '16 at 00:13

2 Answers2

60

Usually, once something is out on Github (or a public repo), there's no way to make sure no one else has the bad commit.

If you simply want cleaner code history though, the way to do what you ask requires you to overwrite history both locally and on the remote project.

What you want is to either do a git commit --amend to modify your old commit if you've not already created a fresh commit with your changes.

If you've already created a fresh commit, you'll want to use git rebase -i to squash your commit on top of the old one.

After you've made this change locally, and verified your commit looks the way you want it to, you'll have to git push --force to overwrite history on the Github remote.

Here's a tutorial on how to re-write history in git, it explains both the approaches listed in this answer.

Emzor
  • 1,380
  • 17
  • 28
ffledgling
  • 11,502
  • 8
  • 47
  • 69
  • 9
    `git push --force`, dangerous but love it – Andrzej Rehmann Sep 20 '16 at 13:53
  • I had a similar problem recently, but in my case I already had few more commits locally. The solution was to squash them together into one single commit and push --force it to upstream. The good thing is that I was doing a Pull Request to master in Github, and it updated the PR with the new history and just 1 commit. This [question](https://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git/5201642#5201642) helped me to achieve it. – VictorHMartin Nov 28 '18 at 10:49
1

I would not advise you to do this as GIT isn't meant to remove and/or edit commits. You could just revert your previous commit and then commit the "valid" code again.

Or you can Rebase your branch. Or take a look at this Answer.

questionto42
  • 7,175
  • 4
  • 57
  • 90
Stijn Bernards
  • 1,091
  • 11
  • 29
  • 2
    "GIT isn't meant to remove and/or edit commits" I'd say that good development practices advise against such actions, but GIT is "meant to" in that it's capable and there are very good reasons for this; like when sensitive info is committed that shouldn't be. – rainabba Aug 31 '16 at 22:19