取得 - git ローカルブランチ 削除
新しいローカルブランチをリモートのGitリポジトリにプッシュして追跡するにはどうしたらいいですか? (9)
私は次のことができるようにしたい:
他の(リモートまたはローカル)ブランチに基づいてローカルブランチを作成します(
git branch
またはgit checkout -b
経由で)ローカルブランチをリモートリポジトリ(公開)に
git push
ますが、追跡可能にしてgit pull
とgit push
がすぐに機能するようにします。
それ、どうやったら出来るの?
私はGit 1.7の--set-upstream
について知ってい--set-upstream
が、それは作成後のアクションです。 ブランチをリモートリポジトリにプッシュする際に同様の変更を加える方法を見つけたいと思います。
1.7より前のバージョンのGitLabでは、
git checkout -b name_branch
(name_branch、ex:master)
リモートリポジトリにプッシュするには、次のようにします。
git push -u origin name_new_branch
(name_new_branch、例:feature)
Git 1.7.0以降では、新しいブランチをチェックアウトすることができます:
git checkout -b <branch>
ファイルを編集し、追加してコミットします。 次に、 -u
( --set-upstream
)オプションを押します。
git push -u origin <branch>
Gitはプッシュ中に追跡情報を設定します。
あなたはすでに次のようなプロジェクトをクローンしていると思います。
git clone http://github.com/myproject.git
次に、ローカルコピーで、新しいブランチを作成してチェックアウトします。
git checkout -b <newbranch>
あなたのサーバーで "git bare --init"を作成し、myapp.gitを作成したとすると、次のようになります。
git remote add origin ssh://example.com/var/git/myapp.git git push origin master
その後、ユーザーは
git clone http://example.com/var/git/myapp.git
注:私はあなたのサーバーを稼働させていると仮定しています。 そうでない場合、動作しません。 いいハウツーがhereにありhere 。
ADDED
リモートブランチを追加する:
git push origin master:new_feature_name
すべてが良いかどうかをチェックする(原点を取り出し、リモートブランチをリストする):
git fetch origin
git branch -r
ローカルブランチを作成し、リモートブランチを追跡します。
git checkout -tb new_feature_name origin/new_feature_name
すべてを更新する:
git pull
ここでの答えに少し踏み込んで、私はこのプロセスを単純なBashスクリプトとしてラップしました。もちろんGitエイリアスとしても使用できます。
私にとって重要なことは、これはコミットする前に単体テストを実行するように促し、デフォルトで現在のブランチ名を渡すということです。
$ git_push_new_branch.sh
Have you run your unit tests yet? If so, pass OK or a branch name, and try again
usage: git_push_new_branch {OK|BRANCH_NAME}
e.g.
git_push_new_branch -> Displays prompt reminding you to run unit tests
git_push_new_branch OK -> Pushes the current branch as a new branch to the origin
git_push_new_branch MYBRANCH -> Pushes branch MYBRANCH as a new branch to the origin
git_push_new_branch.sh
function show_help()
{
IT=$(CAT <<EOF
Have you run your unit tests yet? If so, pass OK or a branch name, and try again
usage: git_push_new_branch {OK|BRANCH_NAME}
e.g.
git_push_new_branch.sh -> Displays prompt reminding you to run unit tests
git_push_new_branch.sh OK -> Pushes the current branch as a new branch to the origin
git_push_new_branch.sh MYBRANCH -> Pushes branch MYBRANCH as a new branch to the origin
)
echo "$IT"
exit
}
if [ -z "$1" ]
then
show_help
fi
CURR_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$1" == "OK" ]
then
BRANCH=$CURR_BRANCH
else
BRANCH=${1:-$CURR_BRANCH}
fi
git push -u origin $BRANCH
公開リポジトリのローカルブランチをアップロードするには、公開リポジトリにcd
から次のコードを使用する必要があります。
git push -u origin branchname
新しいローカルブランチを作成するには、次のようにします。
git branch <branch-name>
リモートリポジトリにプッシュするには、次のようにします。
git push -u origin <branch-name>
既存のブランチから分岐して新しいブランチを作成するには
git checkout -b <new_branch>
この新しいブランチをリポジトリにプッシュする
git push -u origin <new_branch>
これにより、すべてのローカルコミットが作成され、新しく作成されたリモートブランチのorigin/<new_branch>
時代遅れの編集 、ちょうどgit push -u origin $BRANCHNAME
ウィリアムのその他のGitツール ( gitorious repoとclone )のgit publish-branch
使用してください。
OK、Rubyはありませんので、セーフガードを無視してください! - スクリプトの最後の3行を取り出し、bashスクリプトgit-publish-branch
を作成します。
#!/bin/bash
REMOTE=$1 # Rewrite this to make it optional...
BRANCH=$2
# Uncomment the following line to create BRANCH locally first
#git checkout -b ${BRANCH}
git push ${ORIGIN} ${BRANCH}:refs/heads/${BRANCH} &&
git config branch.${BRANCH}.remote ${REMOTE} &&
git config branch.${BRANCH}.merge refs/heads/${BRANCH}
その後、 git-publish-branch REMOTENAME BRANCHNAME
実行します.REMOTENAMEは通常は原点です(デフォルトの設定などでスクリプトを修正することができます)
私は単にやる
git push -u origin localBranch:remoteBranchToBeCreated
既にクローンされているプロジェクトに適用されます。
GitはremoteBranchToBeCreated
で行ったコミットの下にremoteBranchToBeCreated
という名前の新しいブランチを作成しlocalBranch
。