我怎么做:

从另一个分支创建一个本地分支(通过 git 分支或 git checkout -b) 将本地分支推到远程存储库(即发布),但使其可追踪,以便 git pull 和 git push 工作。


当前回答

这里已经提供的解决方案的轻微变化:

创建一个本地分支,基于某些其他(远程或本地)分支: git checkout -b 分支名称 将本地分支推到远程存储库(发布),但使其可追踪,所以 git pull 和 git push 将立即运行 git push -u 起源 HEAD 使用 HEAD 是一个“手动的方式,将当前分支推到相同的名称在远程”。 来源: https://git-scm.com/docs/git-push 在 Git 条款中, HEAD (in)

其他回答

简单地说,要创建一个新的地方分支,做:

git branch <branch-name>

要将其推到远程存储库,请:

git push -u origin <branch-name>

我创建了一个标签,所以每当我创建一个新的分支,它将按下推和跟踪远程分支。

# Create a new branch, push to origin and track that remote branch
publishBranch() {
  git checkout -b $1
  git push -u origin $1
}
alias gcb=publishBranch

使用: 只是用 thuy/do-sth-kool 输入 gcb thuy/do-sth-kool 是我的新分支名称。

完整的 Git 工作流,以推动本地变更到另一个功能分支类似于此

排除所有远程分支

git pull --all

列出所有分支现在

git branch -a  

查询或创建分支(用分支名替换 <函数分支>):

git checkout -b <feature branch>

顯示目前的分支. 必須顯示與 * 前面

git branch      

添加您的本地变更(. 是目的在这里)

git add .

现在做你的改变:

git commit -m "Refactored/ Added Feature XYZ"

重要:从大师更新:

git pull origin feature-branch

点击您的本地变更:

git push origin feature-branch

我认为这是最简单的 alias,添加到你的 ~/.gitconfig

[alias]
  publish-branch = !git push -u origin $(git rev-parse --abbrev-ref HEAD)

你只是跑

git publish-branch

而且......它出版了分支。

git push --set-upstream 起源 <你的分支名称>

git push -u 起源 <你的分支名>