1、git fetch
git fetch [alias] #从远程获取代码(提取更新数据)
该命令执行后需要执行git merge [alias]/[branch]远程分支到你所在的分支
29447@GW64 /d/myProject (branch_1)
$ git fetch origin
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 2), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (4/4), 1.69 KiB | 49.00 KiB/s, done.
From github.com:bai-cao/python_clone
7337b01..93e0a29 main -> origin/main
29447@GW64 /d/myProject (branch_1)
$ git merge origin:main
merge: origin:main - not something we can merge
29447@GW64 /d/myProject (branch_1)
$ git merge origin
Updating 7337b01..93e0a29
Fast-forward
add_update.txt | 1 -
commit_no.txt | 0
2 files changed, 1 deletion(-)
delete mode 100644 add_update.txt
delete mode 100644 commit_no.txt
2、git pull
从远程获取代码并合并本地版本
git fetch和git merge的简写
git pull [remote_name] [branch]
- remote_name:通常时origin,默认的远程仓库名
- branch : 合并的远程分支,如main或master
git pull origin main:branchtest# 远程origin的main分支拉取过来,与本地branchtest分支合并
git pull origin main# 远程origin的main分支与当前分支合并
29447@GW64 /d/myProject (branch_1)
$ git pull origin main
From github.com:bai-cao/python_clone
* branch main -> FETCH_HEAD
Updating 1d2db8f..7337b01
Fast-forward
add.txt | 1 -
add_no.txt | 0
add_update.txt | 1 +
commit_no.txt | 0
commit_update.txt | 9 +++++++++
file1.txt | 2 --
file3.txt | 1 -
new.txt | 1 -
update.txt | 1 -
9 files changed, 10 insertions(+), 6 deletions(-)
delete mode 100644 add.txt
create mode 100644 add_no.txt
create mode 100644 add_update.txt
create mode 100644 commit_no.txt
create mode 100644 commit_update.txt
delete mode 100644 file1.txt
delete mode 100644 file3.txt
delete mode 100644 update.txt
3、git push
从本地分支上传到远程分支并合并
git push <remote_name> <本地分支名>:<远程分支名>
若本地分支名与远程分支名相同,则可以省略冒号,git push <remote_name> <branch>
git push --force origin master # 强制推送(本地版本与远程版本有差异时)
git push origin --delete branch_1 # 删除主机origin的branch_1分支
29447@GW64 /d/myProject (main)
$ git pull origin main
From github.com:bai-cao/python_clone
* branch main -> FETCH_HEAD
CONFLICT (modify/delete): commit_update.txt deleted in 30f1a1cedd3d76dd6c38847c1caad8b09d2d5357 and modified in HEAD. Version HEAD of commit_update.txt left in tree.
Automatic merge failed; fix conflicts and then commit the result.
29447@GW64 /d/myProject (main|MERGING)
$ git commit -a -m "update commit_update.txt"
[main d17fa53] update commit_update.txt
29447@GW64 /d/myProject (main)
$ git push origin main
Enumerating objects: 12, done.
Counting objects: 100% (12/12), done.
Delta compression using up to 16 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (9/9), 1.35 KiB | 693.00 KiB/s, done.
Total 9 (delta 4), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (4/4), completed with 1 local object.
To github.com:bai-cao/python_clone.git
30f1a1c..d17fa53 main -> main
$ git push origin --delete branch_1 # 删除远程仓库分支
To github.com:bai-cao/python_clone.git
- [deleted] branch_1
$ git branch -r # 查看远程仓库分支
origin/HEAD -> origin/main
origin/main
$ git branch # 查看本地仓库分支
branch_1
* main
$ git branch -d branch_1 # 删除本地仓库分支
Deleted branch branch_1 (was 30f1a1c).
$ git branch # 查看本地仓库分支
* main
4、git submodule
用于管理包含其他Git仓库的项目
通过子模块,将外部库作为项目的一部分管理,而不必将其直接合并到主仓库
$ git submodule init subgit # 初始化子模块
error: pathspec 'subgit' did not match any file(s) known to git
$ mkdir subgit
$ cd subgit
$ git submodule init
$ git submodule update # 更新子模块
$ git submodule add git@github.com:bai-cao/python.git # 添加子模块
Cloning into 'D:/myProject/subgit/python'...
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 9 (delta 0), reused 6 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (9/9), done.
warning: in the working copy of '.gitmodules', LF will be replaced by CRLF the next time Git touches it
$ git submodule # 列出子模块
0e02d19bda7b3c106205583934f1382bd3d72411 python (heads/main)
$ git submodule deinit # 移除子模块1
fatal: Use '--all' if you really want to deinitialize all submodules
$ git submodule deinit python
error: the following file has changes staged in the index:
subgit/python
(use --cached to keep the file, or -f to force removal)
fatal: Submodule work tree 'python' contains local modifications; use '-f' to discard them
$ git submodule deinit -f python
Cleared directory 'python'
Submodule 'subgit/python' (git@github.com:bai-cao/python.git) unregistered for path 'python'
$ git rm python # 移除子模块2
error: the following file has changes staged in the index:
subgit/python
(use --cached to keep the file, or -f to force removal)
$ git rm -f python
rm 'subgit/python'
$ git submodule #再次列出子模块
5、参考
1、git fetch 命令 | 菜鸟教程
2、git pull 命令 | 菜鸟教程
3、git push 命令 | 菜鸟教程
4、git submodule 命令 | 菜鸟教程













网友评论