标签云

微信群

扫码加入我们

WeChat QR Code


What I really wanted to know was whether this will necessarily effect the remote branch when/if you push

2018年12月16日53分15秒

PandaWood: it will add the new branch when you push, but won't delete the old branch. If you use git push -f --mirror, then it will rename the branch on the remote, but you should only use this method if the remote is simply to be a copy of your current repository. See also this question: stackoverflow.com/questions/1526794/git-rename-remote-branch

2018年12月16日53分15秒

PandaWood, it depends on how push.default is configured. By default (matching) it will push to a remote whose name matches. You would have to do git push origin <newname>:<oldname> or you will create a new remote branch. However, if push.default is set to upstream, then you can push origin head and things will go to the oldname on the remote.

2018年12月16日53分15秒

NightOwl888: the -m probably is short for "move", following the Unix convention of using the mv to rename files. The reason for this is that moving and renaming, in a directory-based inode file system, are entirely equivalent.

2018年12月15日53分15秒

The long name of the -m option is --move, e.g., git branch --move master renames the current branch to be called "master".

2018年12月15日53分15秒

You will need to use -M to rename if you are only changing capitalization, as git will tell you that branch already exists.

2018年12月15日53分15秒

You saved me with this comment.

2018年12月15日53分15秒

cjspurgeon Git keeps on amazing me by being so intuitive, to an unexpected extent. Since years.

2018年12月16日53分15秒

In which step would one unset the upstream? Before step 4?

2018年12月16日53分15秒

Cyclonecode: yes. after step 4. But make sure you run git status to check if its has reference to old branch.

2018年12月15日53分15秒

This one worked better for me. Here the 2 steps gave me the following errors: error: dst ref refs/heads/<old-name> receives from more than one src.; error: failed to push some refs to 'gituri:foo/bar.git'

2018年12月16日53分15秒

You got the problem when running the command git push origin :old-name new-name right?

2018年12月15日53分15秒

Yep exactly (sorry I meant "2nd step", not "2 steps" -- tired)

2018年12月15日53分15秒

What happens to the overwritten branch?

2018年12月16日53分15秒

It is overwritten by the new name/branch. For example if you have the following branches in git: master b1 <-- current branch b2 after you do 'git branch -M b2' you will only have: master b2 <-- current branch b1 will be gone and if you wish to recover it you should check it out by its hash. You can see it by typing 'git reflog'. Cheers.

2018年12月15日53分15秒

The -M flag is also useful to force a rename if you are just correcting the case of the branch name, e.g. changing myBranch to MyBranch. (With -m, git returns fatal: A branch named 'MyBranch' already exists.)

2018年12月15日53分15秒

Couldn't you just have renamed the file in refs/heads?

2018年12月15日53分15秒

Ditto. If you have to dig into the directory structure to do this magic, go all the way and do a 'mv -- -dumb-name brilliant-name' Do a 'git branch -av' and you'll see an directory structure of .git/refs. Or maybe 'grep -R ^ .git/refs' to see the hashes directly.

2018年12月16日53分15秒

You could probably have used reflog

2018年12月15日53分15秒

Honestly, if that's the route you wanted to take, I'd avoid the (IMO confusing and potentially dangerous if you don't know what you're doing) jaunt through .git directory in the first place, and just do it with some normal commands with some "git log" parsing (using appropriate flags to show branches, and to figure out which shasum you want to checkout a new branch from), and then do it. Then, remove the wonky-named branch. I despise that git insists that you need to understand all of its inner workings to do some things, but greatly appreciate that you can do those things.

2018年12月16日53分15秒

It's harder to create a branch with a bad name in 2.10.1+. If you do somehow do it, you can use git branch -v to get the short hash version of your branches(add -r for remote). You can then use git rev-parse <shorthash> to get the full hash if you need it.

2018年12月15日53分15秒

Note that this situation might also arise on a Mac, which is also (exceptionally annoyingly) case insensitive in its file system.

2018年12月16日53分15秒

Alternatively, you can use -M instead of -m to do this kind of "casing fix" rename in a single step.

2018年12月16日53分15秒

I wouldn't call it pain. The git command is very easy to use, once you've seen this answer, you'll probably never come back again. The problem is more that, so it seems, the documentation of the git command-line isn't intuitive enough.

2018年12月16日53分15秒

True but with SourceTree I hardly ever need to worry about checking documentation. Everything is generally intuitive - just right click and see what the options are. (BTW I'm not affiliated with them in any way - just like the tool!)

2018年12月16日53分15秒

This seems like a repeat of the existing answers.

2018年12月16日53分15秒