git创建分支提交远程分支将分支branch合并到主干master

14次阅读

共计 1402 个字符,预计需要花费 4 分钟才能阅读完成。

使用 git 进行系统开发时, 刚开始采用主干 master,在项目更新频繁的情况下,就需要新建分支进行开发,每次将新的分支 branch 提交到 gitee 上,开发完毕时新分支合并到主干 master 上。

  • 从已有的分支创建新的分支(如从 master 分支), 创建一个 test 分支
    git branch test 创建新分支
    git checkout test 切换到新分支
    上面命令等用于 git checkout -b test

  • 创建完可以查看一下, 分支已经切换到 test
    git branch * 表示在当前分支
  • 提交该分支到远程仓库
    git push 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 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_… 解决
  • 再次上传
    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
正文完
 0