1

I am reading about GIT branching strategy that involves master, develop, hotfix, release, feature.

We are 5 developers working on a python website. Following is branching strategy I want to use on GitHub.

  1. Production code is in the master branch.
  2. I create a Task branch off the master branch.
  3. I work on the Task branch, and before pushing the code to git I switch to master and pull the code, switch to Task and merge with master. This is so that my code is in sync with remote master
  4. Push code
  5. Raise PR from Task branch into master branch.

What happens if master has got additional code that I am missing in my Task branch and I forget to run step 3?

variable
  • 8,262
  • 9
  • 95
  • 215

2 Answers2

1

You won't lose code. If your Task branch is pushed without being synced to master first, GitHub will tell you if there are any conflicts.

In case of conflicts, you will get this message on the PR:


enter image description here


Then you can pull master locally, fix the conflicts and push back the changes to Task.

If there are no conflicting files, you can merge without updating your Task branch, even if master is ahead.

evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
thchp
  • 2,013
  • 1
  • 18
  • 33
  • What do you mean by `If there are no conflicting files` - do you mean no conflicts with respect to entire file or specific lines conflicting? – variable Nov 23 '21 at 14:31
  • I mean any conflicts you would encounter when you merge locally: lines that git cannot merge itself. It can be a whole file (deleted file vs. modified file) or specific lines. But if you modify the same file in two different parts, there will be no conflicts – thchp Nov 23 '21 at 14:45
  • Is there any way I can prevent merge even when there is no conflict (so no line conflict) but code has changed in other places? – variable Nov 23 '21 at 14:50
  • You can enforce fast-forward merge on your repo https://stackoverflow.com/questions/60597400/how-to-do-a-fast-forward-merge-on-github But what would be the point of doing that? If you want to protect your production branch, you shouldn't allow your team to merge into it except for those who have the right (maintain role vs. write role maybe?) – thchp Nov 23 '21 at 14:59
  • By prevent merge I meant prevent PR. – variable Nov 23 '21 at 15:02
  • You cannot prevent PR. You can protect branches so that only admins can merge a PR, and require that PRs are up to date by enforcing linear history – thchp Nov 23 '21 at 15:09
  • What about the setting mentioned in the other answer? – variable Nov 23 '21 at 15:14
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239490/discussion-between-thchp-and-variable). – thchp Nov 23 '21 at 15:20
1

If you're using GitHub, you can add a Branch Protection Rule that requires branches to be up-to-date with the master branch before they are allowed to be merged in a Pull Request:

GitHub's 'require branches to be up to date before merging' protection rule

The branch must be up to date with the base branch before merging.

See the GitHub Branch Protection rule documentation here.

Adil B
  • 14,635
  • 11
  • 60
  • 78
  • The other answer seems to point that this is automatic? – variable Nov 23 '21 at 14:36
  • This is an extra safety check for your `master` branch. If this branch protection rule is disabled, the docs say: "Status checks may fail after you merge your branch if there are incompatible changes with the base branch." – Adil B Nov 23 '21 at 14:52
  • Does this prevent merge even when there is no conflict (so no line conflict) but code has changed in other places? Thereby forcing the developer to pull latest code and merge before making a PR? – variable Nov 23 '21 at 14:56
  • Yes, that's the purpose of this branch protection rule. The documentation page has some helpful details, too. – Adil B Nov 23 '21 at 14:59
  • Where is the setting to prevent push to master without PR? – variable Nov 24 '21 at 10:54
  • Enable the `Restrict who can push to matching branches` branch protection rule and you can restrict direct pushes to `master`, including admins of the repository. – Adil B Nov 24 '21 at 14:18