开发者

Using Git when I want my branches to be always updated [duplicate]

开发者 https://www.devze.com 2023-02-27 18:42 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Can “git pull --all” update all my local branches?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Can “git pull --all” update all my local branches?

I'm a happy Git user. But I find that I'm often doing a lot of pulling, and maybe this can be automated.

I mean, I'm working on a local repo, and the remote is Github. I have several branches, say master, dev and bugfixing. I work on multiple computers, so often I have to push a branch from one computer and pull it from the other.

This is tedious. This can sometimes take a lot of time. For example, say I'm on master. I can do git pull and have the updated origin/master merged into it. But then say that origin/dev was updated too. Now I need to git checkout dev and git merge origin/dev and finally git checkout master. That's really annoying. And don't get me started on these times where the branches aren't set to track their respective remote branches and I have to do git branch --set-upstream master origin/master.

Is there a way to have all the branches merge开发者_Go百科 in the remote branches automatically? We can assume that all of the merges are fast-forward. (If they aren't I have no problem with just getting an error message so I'll manually merge.)


This isn't something I'd want to do myself, but out of interest I've written a script that (I think) does what you want:

  • git-fetch-and-fast-forward-all-branches.sh

This first updates your remote-tracking branches from every remote that doesn't have remote.<name>.skipDefaultUpdate set to true. Then it goes through all of your local branches (i.e. those under refs/heads) and for each one that has an upstream branch set, sees if your local branch can be fast-forwarded. If so, it updates your branch.

Finally, it tries to merge the upstream for your current branch (if it is set) into the current branch, as would happen with git pull.

I've tested this a bit, but note that because of the use of the plumbing command git update-ref to do the fast-forward, it's potentially dangerous if there's some bug in the script, so no warranties, please test carefully, understand what it's doing, etc.

And don't get me started on these times where the branches aren't set to track their respective remote branches and I have to do git branch --set-upstream master origin/master

The only problem I have with that command is the really confusing results if you forget to include master - an easier alternative is to use git push -u <remote-name> <refspec> on one of the occasions when you push the branch.


If you want to work this way there's no reason to keep your local branches up to date. Just use git fetch origin to get what's new on the other side, and sync our local branches only when you want to commit to them.

Until then, you can do just git checkout origin/dev to inspect the code there.


Automatically - I can only think of using hooks to do all thinks automatic that you want out of your repos. Say a post-receive hook ( on server side) to push, merge, checkout etc. Or a post-commit hook ( on client side )

Git Automatically push to Dev and Production from Central Repository depending on branch pushed


I'm not sure i've understood what you want, but how about:

git pull --all

?

0

精彩评论

暂无评论...
验证码 换一张
取 消