# 修改master分支代码
niko@niko-notebook:~/StydyGit$ git branch -v
hot_fix 40376b9 fix bug by xvge
* master 40376b9 fix bug by xvge
niko@niko-notebook:~/StydyGit$ vim file3.txt
niko@niko-notebook:~/StydyGit$ git add .
niko@niko-notebook:~/StydyGit$ git commit -m "add festure by master"
[master cbd7ce1] add festure by master
1 file changed, 1 insertion(+)
# 修改hot_fix分支代码
niko@niko-notebook:~/StydyGit$ git branch -v
* hot_fix 40376b9 fix bug by xvge
master 40376b9 fix bug by xvge
niko@niko-notebook:~/StydyGit$ vim file3.txt
niko@niko-notebook:~/StydyGit$ git add .
niko@niko-notebook:~/StydyGit$ git commit -m "add feature by fix"
[hot_fix 6cceae3] add feature by fix
1 file changed, 1 insertion(+)
# 将master合并到hot_fix上
niko@niko-notebook:~/StydyGit$ git branch -v
* hot_fix 6cceae3 add feature by fix
master cbd7ce1 add festure by master
niko@niko-notebook:~/StydyGit$ git merge master
Auto-merging file3.txt
CONFLICT (content): Merge conflict in file3.txt
Automatic merge failed; fix conflicts and then commit the result.
# 解决冲突
niko@niko-notebook:~/StydyGit$ vim file3.txt
Hi, FullStackDev.
fix this bug by xvge.
<<<<<<< HEAD
add festure by fix.
=======
add feature by master.
>>>>>>> master
# 提×××并
niko@niko-notebook:~/StydyGit$ git add .
niko@niko-notebook:~/StydyGit$ git commit -m "merge code by conflict"
[hot_fix 088f6c5] merge code by conflict
创建远程仓库github.com,如果勾选Initialize this repository with a README选项可能首次推送失败。
将GitHub地址保存到本地
niko@niko-notebook:~/StydyGit$ git remote add origin https://github.com/xvGe/StudyGit.git
查看本地保存了什么地址
niko@niko-notebook:~/StydyGit$ git remote -v
origin https://github.com/xvGe/StudyGit.git (fetch)
origin https://github.com/xvGe/StudyGit.git (push)
将本地库推送到远程库
niko@niko-notebook:~/StydyGit$ git push origin master
Username for 'https://github.com': xvGe
Password for 'https://xvGe@github.com':
Counting objects: 19, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (19/19), 1.44 KiB | 52.00 KiB/s, done.
Total 19 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
remote:
remote: Create a pull request for 'master' on GitHub by visiting:
remote: https://github.com/xvGe/StudyGit/pull/new/master
remote:
To https://github.com/xvGe/StudyGit.git
* [new branch] master -> master
克隆远程库(git clone)
niko@niko-notebook:~/StydyGit$ cd ../
niko@niko-notebook:~$ rm -rf StydyGit/
niko@niko-notebook:~$ git clone https://github.com/xvGe/StudyGit.git
Cloning into 'StudyGit'...
remote: Counting objects: 19, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 19 (delta 2), reused 19 (delta 2), pack-reused 0
Unpacking objects: 100% (19/19), done.
拉取远程库(git pull和git fetch)
niko@niko-notebook:~$ git init StudyGit2 # 创建新项目
Initialized empty Git repository in /home/niko/StudyGit2/.git/
niko@niko-notebook:~$ cd StudyGit2/ # 进入项目目录
niko@niko-notebook:~/StudyGit2$ git remote add origin https://github.com/xvGe/StudyGit.git#添加地址
niko@niko-notebook:~/StudyGit2$ git fetch origin master # 拉取项目
remote: Counting objects: 19, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 19 (delta 2), reused 19 (delta 2), pack-reused 0
Unpacking objects: 100% (19/19), done.
From https://github.com/xvGe/StudyGit
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
niko@niko-notebook:~/StudyGit2$ git branch -a # 查看所有分支
remotes/origin/master
niko@niko-notebook:~/StudyGit2$ git branch -r # 查看远程分支
origin/master
niko@niko-notebook:~/StudyGit2$ git merge origin/master # 合并分支
niko@niko-notebook:~/StudyGit2$ ls
file.txt file2.txt file3.txt
niko@niko-notebook:~/StudyGit2$ cd ../ # 返回上层
niko@niko-notebook:~$ git init StudyGit3 # 初始化一个项目
Initialized empty Git repository in /home/niko/StudyGit3/.git/
niko@niko-notebook:~$ cd StudyGit3/ # 进入项目目录
niko@niko-notebook:~/StudyGit3$ git pull https://github.com/xvGe/StudyGit.git # 拉取项目
remote: Counting objects: 19, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 19 (delta 2), reused 19 (delta 2), pack-reused 0
Unpacking objects: 100% (19/19), done.
From https://github.com/xvGe/StudyGit
* branch HEAD -> FETCH_HEAD
niko@niko-notebook:~/StudyGit3$ ls
file.txt file2.txt file3.txt
git fetch和git pull的区别是,前者会创建新分支,需要合并操作,但是更加安全;后者直接修改本地代码。
跨团队协作远程克隆仓库
团队拉取远程仓库修改并推送