I´m working in a branch and I changed the structure of the project. If people make some changes to the master, how can I bring those changes to the my branch without messing up my change structure.
-
What kinds of changes are you expecting? Changes to existing files? New files? Or what? – Code-Apprentice Aug 24 '18 at 17:45
3 Answers
The first step is to merge master
into your change branch:
git checkout change
git merge master
Changes made to existing files will automagically apply to the files in their new directories, possibly with merge conflicts if you've made changes on the same lines of those files.
You will need to move new files manually and make a new commit.

- 81,660
- 23
- 145
- 268
-
1One minor gotcha is that in the event of major conflicts, both their version in yours can end up in their original locations and need to be resolved in a more manual way. I've only had this happen a couple of times, but it throws me every time it does. – LightBender Aug 24 '18 at 18:14
Let's assume you have the following two branches where each letter represents a commit hash:
C---------D feature
/
A---B---E---F---H master
While you're working on the feature
branch, it is a good idea to keep it in sync with master
so that you reduce the amount of merge conflicts when you eventually create a Pull Request into master
.
To keep the feature
branch in sync, you have a few options:
1. Adding all commits from master
to feature
:
git checkout feature
git merge master
Example: If you are at commit D
on the feature
branch and you run above commands, you will apply commit E
, F
and H
to the feature branch.
Read more about it here: git merge
2. Adding only specific commits from master
to feature
git checkout feature
git cheery-pick <commit-hash>
Example: Running git cherry-pick F
will only add commit F
to your feature
branch.
Read more about it here: git cherry-pick
3. Adding only specific files from master
to feature
git checkout master <path/to/filename>
Example: Running git checkout master src/index.js
will add the index.js
file from commit H
to your feature
branch.
If you like to add the index.js
from commit F
instead, you can use the following commands:
git checkout master~1 src/index.js
or
git checkout <commit-hash> <path/to/filename>
Read more about the command: git checkout
4. Adding only specific lines of a file from master
to feature
git checkout master <path/to/filename> -p
Example: If you run git checkout master src/index.js --patch
you can decide which hunks of the src/index.js
file from commit H
you want to add to the feature
branch.
I mostly use option 1. and 2. to sync my branches - sometimes also 3. Of course, there are many other ways of achieving the same with git, you can also have a look on this thread for some alternatives.

- 934
- 8
- 17
-
This is a very thorough explanation of the options. Note that in steps 3 and 4, the OP will also need to do a `git add` and `git commit` (or `git commit -a`). A fifth option is `git rebase`. – Code-Apprentice Aug 24 '18 at 19:01