Maya
Maya

Reputation: 51

Recommended workflow for keeping git branch up to date

I have two goals, and I need a workflow to achieve them while working on my features (master is an enterprise github repo).

Goals:

I am new to github, but I tried few scenarios to achieve this and they don't help me.

Initially, I create a new branch (tracks origin/master) and I make a code change.

I commit the change and use git push origin to publish this change to github.

Then I create a PR and let people comment.

I get good feedback, I make code changes and commit on top of my 1st commit.

What to do to keep my branch in sync with origin/master?

I tried updating my local branch by merging via git pull. It goes ok, but now if I do git show I won't see my changes, I will see latest master commit.

I tried updating my local branch by rebasing via git pull --rebase. I can use git show to see my changes, but my local and remote branches are out of sync, and I have to use git push origin -f to update my github PR with second change.

I am open for suggestions, thank you!

Upvotes: 0

Views: 891

Answers (1)

martin.malek
martin.malek

Reputation: 2218

What you are doing is correct approach. There is a big difference between pull and pull --rebase. It will do git fetch && git merge origin/master resp. git fetch && git rebase origin/master. Both of them are correct approach. The difference is in the tree history and in most cases it's company policy what to rather use.

With rebase you will always change history and will need to force push. With merge, you will not change history, but last commit is the merge.

Do not expect that you will have only one commit which will be pushed, so the diff should not be only for one commit but for whole branch. If you only need to see the diff, you can run:

git show HEAD~1

or git show HEAD~4..HEAD~1

to see all changes in last few commits except of the last one (merge).

Or you can try something like this

git diff --boundary HEAD~1..origin/master

P.S.: rebase and force push can be pretty dangerous when more people are working on the same branch. Force push only when you are sure that you are the only one working on the branch.

There can be some problems with the comments as well. Not sure how Github, but Gitlab will not show comments for completely changed or removed lines after force push.

Upvotes: 1

Related Questions