I have more than 4 remote branches in my git repository. My goal is to push one code into all these branches with a git command.
So I want like following:
git add .
git commit -m "Do all with one command"
git push origin -all
Of course, the command above might not work properly.
Can I do this kind of action with only 3 commands like above?
There already is a git push --all
(note the double hyphen), but it's probably, or at least perhaps, not what you want. The real problem here is that you have not properly defined what it is that you want—or at least, I'm not entirely sure what you want to achieve here. The particularly confusing part, to me, is that this:
git add. git commit -m "Do all with one command"
makes only one new commit, on the current branch . If you then use git push
to send this one new commit, but want to send it to "four branches", that's probably not going to do what you wanted.
Had you used, say:
for branch in br1 br2 br3 br4; do
git checkout $branch
[do some editing and/or cherry-picking and so on here]
git commit
done
so that you had updates to your local branches named br1
, br2
, br3
, and br4
, and you now want to send four (or more) commits to the four corresponding branches on origin
, now it would all make sense.
For that matter, here:
git push --all origin
might do precisely what you wanted. However, it might do more than you want, since it means the same as:
git push origin "refs/heads/*:refs/heads/*"
ie, use all your local branch names to send requests to the other Git for the same name. If you had branches br5
and br6
as well, that would have the effect of sending any new commits on those branches, and asking origin
to update its branch names that are spelled the same as yours.
If this is indeed what you want to do, consider using --all
or an explicit refspec as in the above git push
. You can also change your push.default
setting to matching
. Most users probably should not use the matching
setting. This was the default in Git 1 and was changed to simple
in Git 2 because it was wrong for most users, but no one individual is ever "most users".
If your remote is origin
, you are on the master
branch, remote branches are first
, second
, third
and fourth
:
git add .
git commit -m "Do all with one command"
git push origin master:first master:second master:third master:fourth
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.