How can i copy a file from local commit on a different branch?

旧巷老猫 提交于 2021-02-05 06:51:28

问题


I committed a file on master branch but not pushed remote. Now i am working on feature branch and i want that file to be copied to feature branch from master branch.

How can i do this?


回答1:


You can checkout a specific file from another branch:

git checkout master -- path/to/file

The file will be copied from the branch as is into the working tree, and automatically staged.

Note that the relative path from the working tree root is important. For example if you are in the root of the working tree (where there is the .git directory), then the path to the file in the branch must also be relative from the repository root. If you are in a subdirectory, then adjust accordingly.

To verify the correct path, the ls-files command can be helpful, for example:

git ls-files --with-tree master | grep file


来源:https://stackoverflow.com/questions/37203545/how-can-i-copy-a-file-from-local-commit-on-a-different-branch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!