情形:
我有一个新的分支A,想将A中的部分脚本文件,合并到master上。
使用git merge命令进行分支合并是比较常用的方法,但是会将两个分支的内容完全合并,而我只想合并一个文件。
如果想要和合并一个文件下的多个文件,怎么操作?
操作:
1.切换到master分支。
2.git checkout 分支A 文件名字
(pytorch) localhost:ch wang$ git branch
* master
(pytorch) localhost:ch wang$ git checkout A demo.py # 在ch这个文件夹下面,同步分支A的文件demo.py
(pytorch) localhost:ch wang$ git checkout A demo.py demo2.py #同步多个文件
(pytorch) localhost:ch wang$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: ch/demo.py
#如果你合并文件时,不加分支名字,会报错:
(pytorch) localhost:bert wang$ git checkout demo.py
error: pathspec 'demo.py' did not match any file(s) known to git
#然后提交合并到远程master上
(pytorch) localhost:bert wang$ git add demo.py
(pytorch) localhost:bert wang$ git commit -m 'test'
[master 378352d] test
1 file changed, 11 insertions(+)
(pytorch) localhost:bert wang$ git push
注:使用git checkout 某个文件到当前分支时,会当前分支的文件强行覆盖。
git checkout test.py:表示忽略test.py文件的修改,此时代码是未修改状态。
想要合并一个文件下的多个文件到master上,怎么操作?
1.切到master
2.进入到该文件夹下
3.git checkout A test.py test2.py
4.git status看看有没有合并到master上。
git clone 指定 目录
git clone xxx.git “指定目录”
eg:
git clone https://github.com/PearlW1/Chinese-BERT-wwm.git /Users/wang/chinese_copy
此时这个Chinese-BERT-wwm.git项目就在目录:/Users/wang目录下,项目名字是chinese_copy。
这么做有另外一个好处,比如,我想同时打开一个项目的两个分支,对照代码对一些操作,此时可以重新clone一份代码。
git命令查看本地代码的远程仓库
$ git remote -v