How to sync some files between two branches in git?

I have a problem with git and my repo.

I write an example first

git checkout Proj1 # modify index.html git commit -am "Modify Index.html" git checkout Proj2 git show Proj1:index.html > index.html git commit -am "Modify Index.html" git checkout Proj1 

This is my way to keep the same file contents in two branches.

Of course, I can use the cherry pick if the commit contains only the file that I want to sync. How can we sync files between two branches in git?

You have a short solution to this problem.
Maybe some kind of pseudonym to write?
Where can I save a file with a list of files for synchronization?

+6
source share
1 answer

If you don't care about the story, you can make a separate commit on branch 2 containing the file, just like on the commit tip in Proj1:

 git checkout Proj2 git checkout Proj1 -- index.html git commit -am "Modify Index.html" 

Essentially, this means the file needs to be EXACTLY since it looks in the Proj1 branches.

git checkout -- means that after the dash you specify the file name (or path). This can be useful if you want to modify some files in your repository and save others.

Then, if you want, you can also reuse the commit message from the commit made in Proj1, as follows:

 git checkout Proj2 git checkout Proj1 -- index.html git commit -a -C Proj1 

Git commit -C means reusing the commit comment you already had.

From the Git documentation "git checkout" :

Git checkout [-p|--patch] [<tree-ish>] [--] <pathspec>

When or --patch , git check does not switch branches. This updates the named paths in the working tree from the index file or on behalf of (most often - commit).

[...]

Git checking with or --patch used to restore modified or deleted paths to their original contents from an index or to replace paths with contents on behalf of (most often commit-MTF).

And from git documentation, "git commit"

git commit -C <commit>

--reuse-message=<commit>

Take the existing commit object and reuse the journal message and attribution information (including the timestamp) when creating the commit.

0
source

All Articles