0

For a project, I made two commits, pushed them and called a pull request. So now my pull request contains 2 commits. But the two differ greatly and the second one is the correct solution to the issue. I tried

git rebase -i HEAD~2 

to squash the 1st commit into the 2nd, but I'm getting conflicts. There are a lot of conflicts since the two versions differ greatly. So, my question is, do I really have to squash the 2 commits into one and spend all that time in resolving the conflicts? Is there some way I can delete the 1st commit from the pull request or tell Github to only consider the 2nd commit?

mjsxbo
  • 2,116
  • 3
  • 22
  • 34
  • Conflicts *should not* be occurring, if you understand and have relayed your situation correctly. You might add the first few lines of `git log --all --graph --oneline --decorate` output here. – torek May 24 '17 at 11:20

2 Answers2

1

If I understand your question correctly then, first you have to resolve the conflict locally, then you push it. To resolve the conflict, you either squash the commit or delete one commit of them, which is not required.

To delete the last commit, you can do this commit (assuming you are sitting on the last commit)

git reset --hard HEAD~1

The HEAD~1 means the commit before head.

or you could look at git log git log to have a look at commit id, where you want to go back to and then do this.

git reset --hard <sha1-commit-id>

If you already pushed to Github, you will need to do a force push to get rid of it.

git push origin HEAD --force

Hope this helps!

Om Prakash
  • 2,675
  • 4
  • 29
  • 50
  • I have 2 doubts. Firstly, if I have already pushed my last commit, will `git reset --hard HEAD~1` delete it from the online repository too? Secondly, what if I want to delete my second last commit, but not my last commit? How do I do that? – mjsxbo May 24 '17 at 10:19
  • Doing `git reset --hard HEAD~1` won't delete it from online repo. To delete it from online repo, you have to do `git push origin HEAD --force` after doing hard reset. – Om Prakash May 24 '17 at 10:40
  • Deleting one commit from the middle is not possible. You can delete commits continuously e.g. last two commits, last three commits, last commit and so on. Though, you can achieve what you want but not by deleting. You can squash last two commits and choose `pick` for second last commit and `squash` for las commit. For more info check [this](https://stackoverflow.com/questions/1657017/how-to-squash-all-git-commits-into-one?noredirect=1&lq=1) and [this](https://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git?noredirect=1&lq=1) – Om Prakash May 24 '17 at 10:53
0

It depends on the policy of the project. Usually, commits should make sense, so it's OK to have two commits if e.g. one implements a feature and the second one fixes whitespace in surrounding code or refactors related fucntions. YMMV.

choroba
  • 231,213
  • 25
  • 204
  • 289
  • But in my case, the 2 commits have conflicts- so for my pull request to be merged into the project I have to resolve the conflicts right? I'm asking if there's some way around that..like deleting one of the 2 commits or ignoring it and only incorporating the 2nd commit. – mjsxbo May 24 '17 at 09:43
  • Do you mean there are commits in the pull request, or when squashing the commits? Does it make any sense to remove one of the commits? – choroba May 24 '17 at 09:46
  • Yes, there are 2 commits in the pull request. I tried squashing them but it doesn't allow me to. So is there some way I can simply delete the first commit? – mjsxbo May 24 '17 at 09:58