公众号:测度空间
下载代码库到本地
$ git clone https://github.com/xxx/git_practice.git
Cloning into 'git_practice'...
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), done.
复制代码
创建本地代码分支
- 查看当前代码分支
$ git branch
* master
复制代码
通常,我们不直接在主分支(master branch)中工作。相反,我们一般会建立自己的分支并在那里进行编码。这里我们创建一个新的分支exp1
。
- 创建新分支
$ git checkout -b exp1
Switched to a new branch 'exp1'
$ git branch
* exp1
master
复制代码
在新分支下编写新代码后,我们可以将其推送到远程库(remote repo),或者在本地与主分支合并。
- 将新分支下的新代码提交到远程库
$ git add .
$ git commit -m "new changes"
[exp1 2983e7d] new changes
1 file changed, 28 insertions(+)
$ git push origin exp1
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 4 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 950 bytes | 950.00 KiB/s, done.
Total 6 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 1 local object.
remote:
remote: Create a pull request for 'exp1' on GitHub by visiting:
remote: https://github.com/xxx/git_practice/pull/new/exp1
remote:
To https://github.com/xxx/git_practice.git
* [new branch] exp1 -> exp1
复制代码
- 查看当前git状态
$ git status
On branch exp1
nothing to commit, working tree clean
复制代码
现在,我们的exp1
库拥有最新的代码。我们可以进一步将它与master合并,并将master分支推送到远程库,并将其作为我们更新后的官方包。
- 切换到主库(master branch)
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
复制代码
- 确保主库有最新的远端代码
$ git pull origin master
复制代码
- 合并分支库的最新代码
$ git merge exp1
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
复制代码
编辑有冲突的文件后,我们可以将新更改提交到master分支。
$ git add .
$ git commit -m "merge exp1"
$ git push
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 306 bytes | 306.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/xxx/git_practice.git
535d021..69a5279 master -> master
复制代码
总结
# Clone remote repo to local
$ git clone https://github.com/xxx/git_practice.git
# Make your own work branch
$ git checkout -b exp1
# Check local branches
$ git branch
# Make some changes in exp1 and commit to remote repo
$ git add .
$ git commit -m "new changes"
$ git push origin exp1
# Change to master branch
$ git checkout master
# Make sure master branch have the latest code from remote repo
$ git pull origin master
# Merge exp1 with master
$ git merge exp1
# Push latest version to remote repo
$ git push origin master
复制代码