到此,我们就相当于有了两个用户,分别在 Linux 和 windows 上针对于同项目进行协作开发,我们的准本工作到此结束。
目前,我们的仓库中只有一个 master 主分支,但在实际的项目开发中,在任何情况下其实都是不允许直接在 master 分支上修改代码的,这里为了保证主分支的稳定。所以在开发新功能时,常常会新建其他分支,供开发时进行迭代使用。
那么接下来,就让我们在 gitee 上新建 dev 远程分支供我们使用:
# 先 pull 一下拉取最新的远端仓库
[Lotso@VM-4-4-centos git_studying]$ git pull From gitee.com:huang-qiruiqq/git_studying * [new branch] dev -> origin/dev Already up-to-date.
# 之前讲的 git branch 只能看到本地分支
[Lotso@VM-4-4-centos git_studying]$ git branch -a * master remotes/origin/HEAD -> origin/master remotes/origin/dev remotes/origin/master
# 创建并切换分支
[Lotso@VM-4-4-centos git_studying]$ git checkout -b dev origin/dev Branch dev set up to track remote branch dev from origin. Switched to a new branch 'dev'
拉取后便可以看到远程的 dev 分支,接着切换到 dev 分支供我们进行本地开发。要说明的是,我们切换到的是本地的 dev 分支,根据示例中的操作,会将本地分支和远程分支进行关系链接。
对于小伙伴要操作的是:
现在,你和小伙伴就可以在 dev 上完成开发。
首先,让我们在 dev 分支上进行一次开发,并 push 到远程,例如:
[Lotso@VM-4-4-centos git_studying]$ vim file.txt [Lotso@VM-4-4-centos git_studying]$ cat file.txt hello git complete the first function!
[Lotso@VM-4-4-centos git_studying]$ git add file.txt [Lotso@VM-4-4-centos git_studying]$ git commit -m "first function"[dev 1658f31] first function1file changed, 1 insertion(+), 1 deletion(-)[Lotso@VM-4-4-centos git_studying]$ git push warning: push.default is unset; its implicit value is changing in Git 2.0 from 'matching' to 'simple'. To squelch this message and maintain the current behavior after the default changes, use: git config --global push.default matching To squelch this message and adopt the new behavior now, use: git config --global push.default simple See 'git help config' and search for'push.default'for further information. (the 'simple' mode was introduced in Git 1.7.11. Use the similar mode 'current' instead of 'simple'if you sometimes use older versions of Git) Counting objects: 5, done. Delta compression using up to 2 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 289 bytes |0 bytes/s, done. Total 3(delta 1), reused 0(delta 0) remote: Powered by GITEE.COM [1.1.5] remote: Set trace flag 8a45c88 To [email protected]:huang-qiruiqq/git_studying.git 97a0741..1658f31 dev -> dev
# 切换至 master 分支,pull 一下,保证本地的 master 是最新内容
[Lotso@VM-4-4-centos git_studying]$ git checkout master Switched to branch 'master'[Lotso@VM-4-4-centos git_studying]$ git pull Already up-to-date.
# 切换至 dev 分支,合并 master 分支# 这么做是因为如果有冲突,可以在 dev 分支上进行处理,而不是在 master 分支上解决冲突。# 这是一个很好的习惯
[Lotso@VM-4-4-centos git_studying]$ git checkout dev Switched to branch 'dev'[Lotso@VM-4-4-centos git_studying]$ git merge master Already up-to-date.
# 切换至 master 分支,合并 dev 分支
[Lotso@VM-4-4-centos git_studying]$ git checkout master Switched to branch 'master'[Lotso@VM-4-4-centos git_studying]$ git merge dev Updating 97a0741..5a136de Fast-forward file.txt |5 ++++- 1file changed, 4 insertions(+), 1 deletion(-)[Lotso@VM-4-4-centos git_studying]$ cat file.txt hello git complete the first function! complete the second function!
# 将 master 分支推送至远端
[Lotso@VM-4-4-centos git_studying]$ git status # On branch master# Your branch is ahead of 'origin/master' by 3 commits.# (use "git push" to publish your local commits)# nothing to commit, working directory clean [Lotso@VM-4-4-centos git_studying]$ git push warning: push.default is unset; its implicit value is changing in Git 2.0 from 'matching' to 'simple'. To squelch this message and maintain the current behavior after the default changes, use: git config --global push.default matching To squelch this message and adopt the new behavior now, use: git config --global push.default simple See 'git help config' and search for'push.default'for further information. (the 'simple' mode was introduced in Git 1.7.11. Use the similar mode 'current' instead of 'simple'if you sometimes use older versions of Git) Total 0(delta 0), reused 0(delta 0) remote: Powered by GITEE.COM [1.1.5] remote: Set trace flag 3045cfbe To [email protected]:huang-qiruiqq/git_studying.git 97a0741..5a136de master -> master [Lotso@VM-4-4-centos git_studying]$ git status # On branch master nothing to commit, working directory clean
此时,查看远端仓库,master 已经是最新代码了:
此时,dev 分支对于我们来说就没用了,那么 dev 分支就可以被删除掉,我们可以直接在远程仓库中将 dev 分支删除掉:
# 必须先拉取远端仓库内容
[Lotso@VM-4-4-centos git_studying]$ git pull remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3(delta 1), reused 0(delta 0), pack-reused 0(from 0) Unpacking objects: 100% (3/3), done. From gitee.com:huang-qiruiqq/git_studying * [new branch] feature-2 -> origin/feature-2 There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1)for details git pull <remote><branch> If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=origin/<branch> feature-1 # 可以看到远程已经有了 feature-2
[Lotso@VM-4-4-centos git_studying]$ git branch -a dev * feature-1 master remotes/origin/HEAD -> origin/master remotes/origin/dev remotes/origin/feature-1 remotes/origin/feature-2 remotes/origin/master
# 切换到 feature-2 分支上,可以和远程的 feature-2 分支关联起来# 否则将来只使用 git push
[Lotso@VM-4-4-centos git_studying]$ git checkout -b feature-2 origin/feature-2 Branch feature-2 set up to track remote branch feature-2 from origin. Switched to a new branch 'feature-2'[Lotso@VM-4-4-centos git_studying]$ ls a.so b.ini file.txt function2 README.en.md README.md
使用命令 git remote show origin ,可以查看 remote 地址,远程分子,还有本地分子与之相对应关系等信息。
[Lotso@VM-4-4-centos git_studying]$ git remote show origin * remote origin Fetch URL: [email protected]:huang-qiruiqq/git_studying.git Push URL: [email protected]:huang-qiruiqq/git_studying.git HEAD branch: master Remote branches: master tracked refs/remotes/origin/dev stale (use 'git remote prune' to remove) refs/remotes/origin/feature-1 stale (use 'git remote prune' to remove) refs/remotes/origin/feature-2 stale (use 'git remote prune' to remove) Local branches configured for'git pull': dev merges with remote dev feature-2 merges with remote feature-2 master merges with remote master Local ref configured for'git push': master pushes to master (up to date)