'Are there some rebase best practices with multiple pushes?
I started a new job at the beginning of the year, whose git practices are different than what I am used to, and I keep running into problems.
The documented process is
- Create branch
- Checkout branch
- Make changes, and commit
git fetch
git rebase origin/develop
git push origin HEAD:branch-name
- Create merge request and merge to
develop
This works fine, only once. After I push the changes, if I make more local changes this typically fails in various ways...
Not being a rebase
expert, I tend to avoid rebase
as much as possible, so I am a rebase
neophyte.
Some of our developers use git pull --rebase origin develop
but this has the same problems, it works only once, and subsequently creates an insane mess every time.
Is there something essential I am missing?
What I want is to be able to push multiple commits from my local environment to origin
as smoothly and easily as possible on the same branch, such that we have a really clean history in our GitLab Monolithic Project. Is this possible?
Solution 1:[1]
Is the 6th step in your workflow
git push origin HEAD:branch-name
the first time you're pushing to remote?
As you have noticed, this works fine the first time around, because it's the first time that you're pushing your branch, hence it's the first time that remote is aware of your branch.
In subsequent rebases, you have to force push your branch because you're re-writing history and performing destructive actions. Otherwise remote will reject your push.
For example at the second rebase, you can check git status
, which will likely give you the following output using branch-name
as your feature branch name:
On branch branch-name
Your branch and `origin/branch-name` have diverged,
and have x and y different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
where x
is the number of outgoing commits and y
the number of incoming commits
In this case, you would not want to follow Git's recommendation to merge remote, because you are aware that you just performed a rebase.
In case you you try to do a normal push, you're then likely to get the following:
! [rejected] branch-name -> branch-name (non-fast-forward)
error: failed to push some refs to 'gitlab.com:yourgroup/yourproject.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Again, you would not want to follow Git's hint to pull, because you remind yourself that you just performed a rebase. Therefore you need to force push to update the remote of branch-name
:
git push --force origin branch-name
You can read more about force push here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | lyzlisa |