Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do references to local branches get pushed when you push a branch in git?

Tags:

git

If I have a project "Proj” on my local machine with local branches localA and localB that correspond to remote branches “remoteA” and “remoteB”. I’m not ready yet to push localA and localB into remoteA and remoteB, but I want to switch machines and have access to localA and localB. If I’m on master and push to master (I know that’s a no-op, but just want to understand in principle), then pull on another machine, would I get access to localA and localB? In other words, do the references to local branches get pushed when you push a branch? If not, is there another way to do that?

like image 824
gkeenley Avatar asked Sep 03 '19 18:09

gkeenley


Video Answer


2 Answers

No, pushing from your local master branch to the remote master branch will have no affect on either the local or remote copies of localA or localB.

To push the tip of a local branch up to it's corresponding remote would take a command specific to that branch.

If you want to gain access to the local state of localA on Machine1 from Machine2, without updating the remote state of localA, you could perhaps use a temporary branch like this:

From Machine1:

git checkout localA # start out at localA
git checkout -b localA_temp # create temp branch
git push origin localA_temp # push temp branch to remote

From Machine2:

git fetch # fetch all content from origin
git checkout localA_temp # checkout temp branch

Of course, localA_temp will be out there for anyone to see who can fetch from your remote, so this technique will not be sufficient if you don't want your localA changes to be seen by others until they are ready.

like image 51
Jonathan.Brink Avatar answered Sep 22 '22 15:09

Jonathan.Brink


Not by default

Git pushes the branch you are in by default

So git push origin will push your current branch

Also git push branch_name pushes the specific branch "branch_name"

Two Exceptions:

If you use a wildcard for the branch names, e.g.

git push *

or if you use --all

git push --all

this will attempt to push all your new/updated branches to origin

https://git-scm.com/docs/git-push

From the link:
When the command line does not specify where to push with the argument, branch.*.remote configuration for the current branch is consulted to determine where to push. If the configuration is missing, it defaults to origin. When the command line does not specify what to push with ... arguments or --all, --mirror, --tags options, the command finds the default by consulting remote.*.push configuration, and if it is not found, honors push.default configuration to decide what to push (See git-config[1] for the meaning of push.default). When neither the command-line nor the configuration specify what to push, the default behavior is used, which corresponds to the simple value for push.default: the current branch is pushed to the corresponding upstream branch, but as a safety measure, the push is aborted if the upstream branch does not have the same name as the local one.

like image 39
Michael Durrant Avatar answered Sep 25 '22 15:09

Michael Durrant