3

I am trying to squash 3 commits.

  • I clone the repository
  • I checkout the branch with the commits I want to squash
  • I run 'git rebase -i HEAD~3'
  • I 'pick' the top commit and I 'squash the second and third one. This is all fine. On my local branch the commits have now been squashed

When I now try to 'push' this is being rejected stating 'Updates were rejected because the tip of your current branch is behind'. Now I have not made any changes to any of the files. Does anyone know how I can push this 'squash' to the remote repository?

Lieuwe
  • 1,734
  • 2
  • 27
  • 41
  • Possible duplicate of [Updates were rejected because the tip of your current branch is behind](https://stackoverflow.com/questions/39399804/updates-were-rejected-because-the-tip-of-your-current-branch-is-behind) – phd Jul 22 '19 at 10:52
  • https://stackoverflow.com/search?q=%5Bgit%5D+Updates+were+rejected+because+the+tip+of+your+current+branch+is+behind – phd Jul 22 '19 at 10:52

2 Answers2

2

This is an expected behavior since from git's perspective you've removed 2 commits from the tip of the branch. You will attempt to change the history when pushing and to do it you have to git push --force.

tomdol
  • 381
  • 3
  • 14
1

Use the force (-f) since you're basically re-writing the history of your branch. Be careful though you shouldn't be doing this on any long-term branches such as master or develop.

As an aside, if you're using a git repository management software such as github/gitlab/bitbucket, you could do this via their web interface, with pull requests and using a squash option when merging.

A. M.
  • 384
  • 1
  • 6
  • I am using a (local) gitlab. Thanks for the suggestion. – Lieuwe Jul 22 '19 at 09:39
  • Great! I recommend that you check this out then: https://docs.gitlab.com/ee/user/project/merge_requests/squash_and_merge.html – A. M. Jul 22 '19 at 09:56