pull(拉取)
- 作用和我的理解:把数据从远端拉到本地目前的分支上并进行自动合并
- 对本地文件的影响:如果有有冲突会自动合并,有些合并不了的会提示用户手动合并。
fetch(获取)
- 作用和我的理解:获取远端仓库信息 , 比如现在远端的版本比本地的版本先进,用
git branch -a
可以查看远端的分支,但是这时我们查询的分支也是旧的 , 当使用git fetch
获取到远端信息之后,此时用git branch -a
查询出来的远端分支是最新的,我们将远端分支和本地分支进行合并,并且解决冲突在进行commit
和push
- 对本地文件的影响:和本地文件无关
截图展示区别:
比如现在远端的版本比本地的版本先进,对仓库中的
test.txt
文件进行操作:
这里对本地进行了更改操作,然后我们提交代码并push
git add . //跟踪文件 git commit -m "更改test.txt前两行内容" //提交到本地仓库 git push
出现报错:(大意为当前版本不是最新的,请先获取最新版本在进行上传)
To https://gitee.com/gbmom/test.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://gitee.com/gbmom/test.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
这里如果用
pull
来拉的话会让远端和本地的数据进行自动合并,因为每一行数据都不一样,会提示开发者手动合并。
然后重点说下fetch
命令,如果此时用fetch
,会获取远程的信息,对本地文件不会造成影响。操作步骤:
1.获取远端master信息 git fetch origin master //获取master的信息,不过不写origin master,默认是获取所有分支信息 2.切换到远程master分支 git checkout origin/master //切换分支的时候如果在 分支名的前面加 origin/ 则代表切换的远程分支,这个分支现在时项目最新版本,应为第一步的时候已经获取过远程master的信息了 3.观察远程master分支,然后切回本地分支 git checkout master 4. 合并远程master分支 git merge origin/master
重新提交并上传即可成功,操作中也可以省略一二步
冲突解决:
git pull和fetch的区别,git fetch怎么用,fech用法演示
最新推荐文章于 2024-06-15 22:25:10 发布