1

I have a branch called file_migration and I wanted to merge it with the master branch. I only want the files I have updated/added to be merged into master branch. If other people have made changes to master branch that are not captured in my branch, I don't want my merge to affect other peoples' changes.

For example, let's say the master's branch contains only 2 folders: folder_1 and folder_2. My file_migration branch contains changes I made to folder_1. I have NOT changed anything in folder_2, but someone else has made changes to folder_2. So folder_2 in my branch will be older than folder_2 in master. I want to make sure my merge to master branch only updates folder_1 and should not affect folder_2 in master branch (because folder_2 in my branch is older than folder_2 in master branch so I don't want my merge to affect folder_2).

What will be the best protocol to ensure I only apply my changes made to folder_1 to master branch without changing anything in folder_2? I've read this other question Safe way to merge my branch where people suggest they can simply git merge to master branch. But I am not sure if the situation mentioned in that question applies to my case.

Stanleyrr
  • 858
  • 3
  • 12
  • 31

3 Answers3

1

back merge master(commit and push the changes from master) to your file_migration branch. If their are any conflicts in folder 2, take the changes of master branch. and if their are any conflicts in folder 1, then resolve according to your need.

Now, try to merge the changes from file_migration branch to master. Ideally it will not show any changes in folder 2. But if it shows then, donnot stage the changes. Stage only relevant changes, and commit push.

KushalSeth
  • 3,265
  • 1
  • 26
  • 29
  • Thanks, @Kushal. Could you elaborate what you meant by 'back merge'? – Stanleyrr Oct 23 '20 at 05:12
  • 1
    Back merge means, merging the parent branch to feature branch. So that if there are any conflicts, it can be resolved. So, merge master to feature branch. – KushalSeth Oct 23 '20 at 05:14
  • There's no point in merging from master and then immediately merging to master. Save a merge and merge straight to master. If it goes wrong, undo it. – Schwern Oct 23 '20 at 05:20
  • 1
    I have suggested the back merge because, if there are any conflicts in folder2 then we can take the master version. and yeah, if you not not made any changes then anyways it will not show any commit changes – KushalSeth Oct 23 '20 at 05:22
1

The safest way to merge using Github is a pull request from your branch to master. You can read more here https://docs.github.com/en/free-pro-team@latest/github/collaborating-with-issues-and-pull-requests/about-pull-requests

Yuri G.
  • 4,323
  • 1
  • 17
  • 32
  • I have already submitted a pull request (PR) and my appears have approved it. You mentioned git knows how to handle the difference. Does that mean if git notices data in a folder in master branch are ahead of my commits, it will preserve those data even if my branch contains older data for that folder? – Stanleyrr Oct 23 '20 at 05:13
  • 1
    Note that if you're submitting a branch as a PR you should bring it up to date by merging master into your branch before submitting it. This allows you to resolve conflicts and test that it works with the latest changes in master. It means less work for the reviewers. – Schwern Oct 23 '20 at 05:25
1

Just merge it.

Git is smart enough to know what was changed and what was not. It doesn't matter if your branch is out of date, if you did not change a file Git will not merge any change to the file. You can read more about merge strategies; Git uses several and will pick the most appropriate.

Don't worry if the merge goes wrong, as with many things in Git you can undo it.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • Thank you, @Schwern. It's good to know that when git merge to remote branch it will only merge files that I've updated in my branch. I still got a lot to learn about git, but this gives me the peace of mind. – Stanleyrr Oct 23 '20 at 05:20
  • 1
    @Stanleyrr Note that master is not a remote branch. It is a local branch which has a remote. That's important because nothing you do to master will affect anyone else until you push it. That's why is safe to experiment with your local master branch. – Schwern Oct 23 '20 at 05:22
  • 1
    @Stanleyrr Git is genuinely hard, but very worth the effort to learn. – Schwern Oct 23 '20 at 05:27
  • Btw @Schwern, is it okay if I do the merge by clicking the "Merge pull request" button in the Pull Request in github? (the PR has already been approved by my colleagues) – Stanleyrr Oct 23 '20 at 05:35
  • 1
    @Stanleyrr Yes, if it passed review it's fine to merge. In the future, before submitting your PR bring your branch up to date with a `git merge master` and make sure it still works with the latest changes. – Schwern Oct 23 '20 at 05:39