git提交本地代码到新分支、改变已有分支名字

## 提交本地代码到新分支:

背景:

从branchA分支拉了一份代码,做了一些修改,但是不想提交到branchA分支,想新建一个分支branchB保存代码。

操作方法:

  • 添加本地需要提交代码: git add .
  • 提交本地代码 :git commit -m "add my code to new branchB"
  • push 到git仓库 :git push origin branchA:branchB

仓库中原本没有branchB,提交后会生成新分支branchB,并将本地基于branchA修改的代码提交到branchB中.

But! ——现在branchA分支里面多了一个刚才的commit,git log可以看出来. 怎么办?
答:git log里找到branchA最后一个commit到线上了的hash值(即commit_id),然后 git reset --hard commit_id .

  • 切换到新分支: git checkout -b branchB origin/branchB

注意:

      如果是checkout到某个tag或者commit下面然后修改了一些代码,那么提交到新分支就是这样:

      1. 先创建本地分支: git branch <new-branch-name> 4c62b30

      2. 再提交到远程:git push origin <new-branch-name>


## 更改本地和远程分支的名字: 

git pull origin old_branch # 一定!要先把本地分支跟服务器上的分支代码同步一下!因为下面的操作会删掉服务器上的分支!!
git branch -m old_branch new_branch # Rename branch locally 
git push origin :old_branch # Delete the old branch in remote server.
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
原文地址:https://www.cnblogs.com/xiaouisme/p/7500729.html