4

I am new to git I have few doubts with git pull and git push commands.

Example:
Let us assume I have two branches in my local machine "master" and "newbranch"(local branch).

$ git checkout newbranch

$ git branch

  *newbranch           //Assume this is local branch only 
    master

Now what exactly the below commands do?

git pull origin master -> It will pull the changes from remote "master" to "newbranch"(currently checkout local branch).

git pull origin/master -> It will pull the latest changes in the 'local' "master" to "newbranch" (currently checkout local branch).

git push origin master -> will it push the new changes in "newbranch" (currently checkout local branch) to 'remote' "master" .

git push origin/master -> ?? what exactly this one do?

Can anyone please clarify the doubt .

                                                         Thanks in advance 

2 Answers 2

2

In general, the syntax of this command is:

git push <remote> [refspec]

If the refspec is omitted it is simplified to this:

git push <remote>

And its behaviour depends on what is set in git config push.default variable. The git message says:

When push.default is set to 'matching', git will push local branches to the remote branches that already exist with the same name.

In Git 2.0, Git will default to the more conservative 'simple' behavior, which only pushes the current branch to the corresponding remote branch that 'git pull' uses to update the current branch.

To set 'matching' mode:

git config --global push.default matching

And to set 'simple' mode:

git config --global push.default simple

Let's go back to your specific case. Pushing commits to the remote named "origin/master" will fail because there is no "origin/master" remote existing (default remote is called "origin"). To make this working you would have to add such remote manually, e.g. by calling:

git remote add origin/master <git-repository-url>

But please note that such operation will make your local git very confused, and you will have to deal with errors like this:

$ git push origin/master
Counting objects: 5, done.
Writing objects: 100% (3/3), 253 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:sarxos/test.git
   820474f..3706ea9  master -> master
error: unable to resolve reference refs/remotes/origin/master/master: Not a directory
error: Cannot lock the ref 'refs/remotes/origin/master/master'.

And some others:

$ git fetch origin/master
error: unable to resolve reference refs/remotes/origin/master/master: Not a directory
From github.com:sarxos/test
 ! [new branch]      master     -> origin/master/master  (unable to update local ref)
error: some local refs could not be updated; try running
 'git remote prune origin/master' to remove any old, conflicting branches

So I do not recommend using it.

If you want to push to origin/master (remote named 'origin' and remote branch named 'master') you should do:

git push origin master
Sign up to request clarification or add additional context in comments.

2 Comments

So , currently i am on new branch named (newbranch) in local. i want to push changes from the newbranch (currently that i am checkout to newbranch ) directly to "remote master". For this can i do directly "git push origin master"
Vivek, the "git push origin master" is resolved to "git push origin master:master", so it looks for the local branch "master" and push changes to remote branch also named "master". If you want to push from a branch you are currently checked in (e.g. newbranch, bubu, or whatever) you can do "git push origin HEAD:master" which means: push from the HEAD of a current branch to the remote "master" branch on my remote called "origin". For more details you can check git-scm.com/docs/git-push
1

git pull origin master or git pull origin/master

It will pull the changes from remote master to "newbranch" (currently checkout local branch).

Exactly - both will do the same
The minor difference is that origin/master will refer to the local copy stored in your repository.

The best practice is before pull to update your local repository with:

git fetch --all --prune

git push origin master

will push the new changes in "newbranch" (currently checkout local branch) to 'remote' "master" .

Kind of,
Git will try to push the current branch to the master on remote, buy because you are on different branch then master (depends on you local git version) git will not let you do it.

If the branch exist in the remote it will try to push to the same branch name (tracked branch) and again depend on your git version it will push to the remote or will throw an error.

enter image description here


git push origin/master

It will push to the remote tracked branch.
Git will use the origin/master as the remote name and will try to push to this remote the current branch name.

In your case if you are on newbranch it will try to push to the remote named origin/master a b branch name newbranch

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.