How can I copy files from a branch to another using git?

Git

Git Problem Overview


I have a git repository with two branches: master and gh-pages. If I want to copy a file foo from master to gh-pages without merging them. What command should I use? Many thanks.

Git Solutions


Solution 1 - Git

You can:

git checkout gh-pages
git checkout master foo
git commit -m 'Add file foo to gh-pages.'

Solution 2 - Git

If you want to compare all the diffs between 2 branches: you can use git difftool master gh-pages or git difftool <SHA1 of gh-pages> .

If you want to get diff for specific list of files follow this:

git diff master gh-pages -- path/to/file

Solution 3 - Git

Also may do cherry-pick from master to gh-pages after commit foo.(After cherry-pick must execute git add [path to foo] and commit)

Solution 4 - Git

The Way I would do it: When you commit you have a choice of what to commit and push. So commit only foo from master then push it, Then just merge that on github/bitbucket whatever to gh-pages

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChongxu RenView Question on Stackoverflow
Solution 1 - GitcforbishView Answer on Stackoverflow
Solution 2 - Git0x90View Answer on Stackoverflow
Solution 3 - GitAlexandrView Answer on Stackoverflow
Solution 4 - GitdsumaView Answer on Stackoverflow