I don't know what I changed, but recently when I try to push up one of my branches by using "git push" it tries to push to the remote branch and to master.
➜ module1 git:(my-branch) git push
Counting objects: 40, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (25/25), done.
Writing objects: 100% (40/40), 3.68 KiB | 0 bytes/s, done.
Total 40 (delta 14), reused 0 (delta 0)
To git@github.corp.company.com:Project-1/SomeProject.git
1000000..000000e my-branch -> my-branch
! [rejected] master -> master (non-fast-forward)
Luckily each time it has happened I have gotten a rejected message because my branch has been behind master. I used to be able to just type git push, and it would only push the branch I was tracking. I have looked into my git config and it doesn't look like it should be pushing to master.
[remote "origin"]
url = git@github.corp.company.com:Project-1/SomeProject.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[branch "my-branch"]
remote = origin
merge = refs/heads/my-branch
Any way I can prevent this from happening?
答案 0 :(得分:2)
push.default
You want to set push.default
to one of simple
, upstream
, or current
. In fact, you probably want simple
.
To set simple
mode for yourself, for all repositories where you have not set some other value, use:
git config --global push.default simple
To override this in one specific repository, use, e.g.:
git config push.default upstream
(assuming you want upstream
mode for the current repository, regardless of your --global
setting).
In Git versions before 2.0, the initial default is matching
. This is the behavior you are seeing now and is clearly not what you want.
In Git versions 2.0 or later, the initial default is simple
. If this is what you want and git --version
tells you that you have 2.0 or later, you don't have to set anything.
simple
and upstream
meanIn Git, each of your branches (your local branch names, like master
and my-branch
) can have one "upstream" setting. (Or it can have no upstream setting, but that's not very useful to us!) The main command to set or change the upstream is git branch --set-upstream-to
.
The upstream is itself essential two parts: the name of a remote, like origin
, and the name of a branch on that remote, like master
. With git branch --set-upstream-to
you just name the remote-tracking branch, origin/master
, to set both at once, and it does the obvious thing.
Once set, git push
will pick the remote from the name-of-the-remote part, and the branch to push to from the name-of-the-branch part. (The branch you are pushing from defaults to your current branch, of course, and it's this current branch's upstream that sets the other two parts.)
Let's say that your current branch is $branch
and that its upstream is $remote/$upstream_branch
. Using simple
or upstream
means that:
git push
means:
git push $remote $branch:$upstream_branch
The simple
setting adds one extra constraint: this default push will stop (refuse to run) if $upstream_branch
is not the same name as $branch
.
current
meansNote that both of the above require that the current branch have an upstream. Using current
is sort of like using simple
, except that Git doesn't require a full upstream setting. It only needs the "remote" part. (You can write git push origin
to supply that part, or your current branch can have a remote set, with or without the other half of the upstream.) Instead of needing $upstream_branch
, git push
now just does:
git push $remote $branch:$branch
Again, the new default is simple
and that's usually the right setting.
答案 1 :(得分:0)
I would be careful in using git push
without specifying the repository and refspec arguments. According to the git-push documentation, git will default to the branch.*.remote
repo and the remote.*.push
refspec.
In personal practice I always push with something like the following...
git push origin my-branch
You can always use git branch -vv
to display what remote each branch is tracking. Would be helpful for further debugging.
Edit: Here is a Stack Overflow answer that looks to be a great explanation on the default behavior of git push