使用git进行系统开发时,刚开始采用主干master,在项目更新频繁的情况下,就需要新建分支进行开发,每次将新的分支branch提交到gitee上,开发完毕时新分支合并到主干master上。
从已有的分支创建新的分支(如从master分支),创建一个test
分支
git branch test
创建新分支
git checkout test
切换到新分支
上面命令等用于git checkout -b test
提交该分支到远程仓库
git push origin test
从远程获取test
git pull origin test
到这一步创建分支提交远程分支基本完成了
从远程获取test
git pull origin test
设置git push,pull默认的提交获取分支,这样就很方便的使用git push 提交信息或git pull获取信息
git branch --set-upstream-to=origin/test
取消对master的跟踪
git branch --unset-upstream master
随便修改一下工程文件的内容,然后git commit
,git push
,之后就可以直接提交到远程的test分支中,而不会是master,若想设置回默认提交获取master,切换master后,重复操作上一步即可
在分支test上开发完成后,需要合并到主干master
从当前分支切换到主干master上
git checkout master
合并某个分支到主干master
git merge test
上传代码
git push origin
报出如下:
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
参考:https://blog.csdn.net/benben_2015/article/details/78803753解决
再次上传
git push --set-upstream origin master
报错如下:
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to ‘https://gitee.com/tahara/blue…’
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push --help’ for details.
出现这个问题是因为gitee中的一些文件不在本地代码目录中,可以通过如下命令进行代码合并
git pull --rebase origin master
合并后删除本地分支信息
git branch -d test
若报出如下错误:
error: The branch ‘test’ is not fully merged.
If you are sure you want to delete it, run ‘git branch -D test’.
使用git branch -D test
删除
删除远程分支git push origin --delete test
遇到的问题
1)master合并分支时提示“Already up-to-date”,但当前分支和 master 分支代码不同步。
解决方法:
git checkout master
git reset --hard dev
git push --force origin master
注意:在不考虑配置文件等测试、生产不同的文件,可以从dev推到master,但是个人分支推送dev分支千万不要用,不然会把其他人的冲掉