Make "Git Pull" Ask for Confirmation When Pulling Different Branch

git - pulling from specific branch

See the git-pull man page:

git pull [options] [<repository> [<refspec>...]]

and in the examples section:

Merge into the current branch the remote branch next:

$ git pull origin next

So I imagine you want to do something like:

git pull origin dev

To set it up so that it does this by default while you're on the dev branch:

git branch --set-upstream-to=origin/dev dev

How to 'git pull' into a branch that is not the current one?

You've got a worktree you don't want to touch, so use another one. Clone is cheap, it's built for this.

git fetch origin master       # nice linear tree
git clone . ../wip -b master # wip's `origin/master` is my `master`
cd ../wip # .
git pull origin origin/master # merge origin's origin/master
git push origin master # job's done, turn it in.
cd ../main
rm -rf ../wip # wip was pushed here, wip's done

git checkout master # payload

The problem with all the other answers here is, they don't actually do the pull. If you need the merge or rebase you've got pull set up for, you need another worktree and the above procedure. Otherwise just git fetch; git checkout -B master origin/master will do.

How do I force git pull to overwrite local files?

⚠ Warning:

Any uncommitted local changes to tracked files will be lost.

Any local files that are not tracked by Git will not be affected.


First, update all origin/<branch> refs to latest:

git fetch --all

Backup your current branch (e.g. master):

git branch backup-master

Jump to the latest commit on origin/master and checkout those files:

git reset --hard origin/master

Explanation:

git fetch downloads the latest from remote without trying to merge or rebase anything.

git reset resets the master branch to what you just fetched. The --hard option changes all the files in your working tree to match the files in origin/master.



Maintain current local commits

[*]: It's worth noting that it is possible to maintain current local commits by creating a branch from master before resetting:

git checkout master
git branch new-branch-to-save-current-commits
git fetch --all
git reset --hard origin/master

After this, all of the old commits will be kept in new-branch-to-save-current-commits.

Uncommitted changes

Uncommitted changes, however (even staged), will be lost. Make sure to stash and commit anything you need. For that you can run the following:

git stash

And then to reapply these uncommitted changes:

git stash pop

How to update my working Git branch from another branch (develop)?

You just merge develop to feature1:

git checkout feature1
git merge develop

There is no need to involve another branch such as master.

Can I create a pull request using the same branch as the base and compare branch?

If the PR will be issued from the same github repo, then it has to be done via branching, i.e. create a branch off of a known branch and make changes, then create PR by comparing the two branches. Thanks to everyone who helped confirm this.

If someone makes a fork of a github repo, then changes in the forked repo can be used to issue PR based on seemingly same branch (in two different repos, i.e. one original, the other the fork). This is where I was confused because I saw the PR came from seemingly the same branch, but actually, it is from a different repo (a fork), technically.

Git pull asks me to write merge message

git pull is basically two actions at once: git fetch followed by a git merge (unless you use git pull --rebase, in which case you can guess what happens).

The reason you're seeing this is because Git can't do a fast-forward merge, like it can most of the time. The reason for that is usually because you've git committed locally to the branch you're trying to pull, and now you need to merge the remote changes with your local ones.

It's also worth noting that Git pre-populated the merge message for you, so you don't really need to type anything. Just save and exit, and the merge should be complete. (Unless, of course, there are merge conflicts).

GitHub pull request showing commits that are already in target branch

It looks like the Pull Request doesn't keep track of changes to the target branch (I contacted GitHub support, and received a response on 18 Nov 2014 stating this is by design).

However, you can get it to show you the updated changes by doing the following:

http://githuburl/org/repo/compare/targetbranch...currentbranch

Replace githuburl, org, repo, targetbranch, and currentbranch as needed.

Or as hexsprite pointed out in his answer, you can also force it to update by clicking Edit on the PR and temporarily changing the base to a different branch and back again. This produces the warning:

Are you sure you want to change the base?

Some commits from the old base branch may be removed from the
timeline, and old review comments may become outdated.

And will leave two log entries in the PR:

Sample Image

Git fetch remote branch

Update: Using Git Switch

All of the information written below was accurate, but a new command, git switch has been added that simplifies the effort.

If daves_branch exists on the remote repository, but not on your local branch, you can simply type:

git switch daves_branch

Since you do not have the branch locally, this will automatically make switch look on the remote repo. It will then also automatically set up remote branch tracking.

Note that if daves_branch doesn't exist locally you'll need to git fetch first before using switch.



Original Post

You need to create a local branch that tracks a remote branch. The following command will create a local branch named daves_branch, tracking the remote branch origin/daves_branch. When you push your changes the remote branch will be updated.

For most recent versions of Git:

git checkout --track origin/daves_branch

--track is shorthand for git checkout -b [branch] [remotename]/[branch] where [remotename] is origin in this case and [branch] is twice the same, daves_branch in this case.

For Git 1.5.6.5 you needed this:

git checkout --track -b daves_branch origin/daves_branch

For Git 1.7.2.3 and higher, this is enough (it might have started earlier, but this is the earliest confirmation I could find quickly):

git checkout daves_branch

Note that with recent Git versions, this command will not create a local branch and will put you in a 'detached HEAD' state. If you want a local branch, use the --track option.

Full details are here: 3.5 Git Branching - Remote Branches, Tracking Branches



Related Topics



Leave a reply



Submit