1

I would like to ask about proper git workflow. Suppose that we have branch master and ultraFeature.

After creating ultraFeature devs added several commits to branch and master. They are causing conflicts.

What is the best way to update changes a make it fast-forward when merged? I also want that it will be done in one commit.

My actual solution. As far I understand I will be dangerous if someone is using my branch or created branch from my branch. How it could be done better?

git checkout master
git pull

git checkout ultraFeature
git pull

git reset --soft HEAD~N    // N is number of commits since creating branch
git commit -m "Summing all changes in branch..."

git rebase master
// solve conflicts

git checkout master
git merge ultraFeature
Newbie
  • 462
  • 7
  • 15
  • 1
    Git is extremely flexible so plenty of people will provide just as many different workflows. Me particularly, I like to keep feature branches "tight". No mergin on them. I rebase when "master" moves forward, say, so that I can get changes form other people and, in case this feature branch has to be cherry-picked/rebased, it can be easily done having the original revisions that were required for it one after the other (because there was no merge done on it, right?). – eftshift0 Sep 21 '18 at 19:10
  • 1
    I would assume your master is stable. If that is the case, just open pull requests to master, from the feature branches you have (each feature is in one branch, right? at least it should) and check the conflicts one by one, solving them in an external merge tool (don't use the git website to merge, it could mess your feature branch). – Leonardo Alves Machado Sep 21 '18 at 19:21
  • @LeonardoAlvesMachado And I need to put this to master in one commit. So the way I posted is correct? – Newbie Sep 21 '18 at 19:37
  • 1
    If your only concern is getting your code from your feature branch into master with a single commit, you may want to consider a `squash merge` https://stackoverflow.com/questions/5308816/how-to-use-git-merge-squash – Vlad274 Sep 21 '18 at 20:22

1 Answers1

2

If the goal is to put all the changes in one commit, you probably can squash them together by using (for N commits, N can be any number of commits):

git rebase -i HEAD~N

if the goal is to get the commits from master and get the "squashed commit" on top of these change (assuming your branch was created out of master), please consider the following:

>> // while being in <ultrafeature> branch
git fetch --all
>> git rebase origin/master

After these commands your commit(s) will be on top of newest master commits. Of course the rebase will fail if there are conflicts, so it will be possible to resolve them and then proceed with git rebase --continue

Last but not least, when you create a Pull Request and your colleagues start to comment and as a result you made more commits (say commitA, commitB, and commitC), you can squash everything and then "re-push" the change, by applying a forcefull push:

git push -f ultrafeature

So, all-in-all I use the following technique:

//while being on master

git checkout ultrafeature
git commit, commit, commit // here is my work, say 3 commits
git rebase -i HEAD~3 // create one commit out of 3 when I'm done
git push -u origin ultrafeature 
// create a pull request
// my colleagues make comments, so I commit again and again (1 more commit)
git rebase -i HEAD~2 // unify a squashed commit with a new commit, in a trivial case it can be also git commit --amend
// now I'll be able to "submit" my changes:
git fetch --all
git rebase origin/master
git push -f
// merge / ff merge into master, usually in UI where I've opened a pull request
Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97