Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A single command to git pull a branch?

Tags:

git

For example, I'm now in develop branch, and want to pull remote master to local master, what I do is:

$ git stash
$ git checkout master
$ git pull
$ git checkout develop
$ git merge master

Question 1: How to pull remote master to local master when I in develop branch?

Question 2: Is it possible to merge remote master to local develop in one command? currently I use 4 commands

like image 529
Sato Avatar asked Jan 19 '17 03:01

Sato


People also ask

How git pull a branch?

In case you are using the Tower Git client, pulling from a remote is very easy: simply drag the remote branch and drop it onto your current HEAD in the sidebar - or click the "Pull" button in the toolbar.

What is the command for git pull?

The git pull command is actually a combination of two other commands, git fetch followed by git merge . In the first stage of operation git pull will execute a git fetch scoped to the local branch that HEAD is pointed at. Once the content is downloaded, git pull will enter a merge workflow.

How do I pull one branch from GitHub?

You need to checkout the branch. git pull origin todo-mvvm-databinding will fetch and merge this branch into your local one. Show activity on this post. The above answer works well but I wanted to post with fetch and checkout which works fine as well.

How do you pull a current branch?

Git already only pulls the current branch. If you have branch set up as a tracking branch, you do not need to specify the remote branch. git branch --set-upstream localbranch reponame/remotebranch will set up the tracking relationship. You then issue git pull [--rebase] and only that branch will be updated.


2 Answers

Answer-1: Your working procedure is ok for question-1.

$ git fetch
$ git stash
$ git checkout master
$ git pull origin master

$ git checkout develop
$ git stash apply

Answer-2: You can pull origin/master into your local develop branch directly.

$ git pull origin master
like image 194
Sajib Khan Avatar answered Nov 21 '22 16:11

Sajib Khan


There are already other good answers to your question 2 (see sajib's). The short answer to question 1 is, "You can't".

However, If I can rephrase your question slightly to: "How can I pull remote master onto master while keeping develop checked out?" then you can use git worktree to your advantage. It gives you another working tree to do all the normal things you'd do with a working tree.

If you're on the develop branch and want to update master, for example:

git worktree add ../second-repo master
git -C ../second-repo merge origin master

Now master has been updated in your local repo.

To explain the above commands, git worktree will checkout master into another folder (../second-repo). The second command will execute git merge origin master only after virtually changing directories to the new worktree. Note that once you have a worktree set up, there's no need set it up again. You can just keep using the same worktree to update master.

like image 42
Jeremy Fortune Avatar answered Nov 21 '22 14:11

Jeremy Fortune