Git on the road - push error '[remote rejected] master -> master (branch is currently checked out)'

StackOverflow https://stackoverflow.com/questions/17961283

  •  04-06-2022
  •  | 
  •  

Pregunta

Let say I'm on the road with a friend, we are offline from the bare git repository. I have a working copy (with .git folder) and my friend want to help me out so he clone my git repository from my working copy and work on its own and of course both copy are checkout so basically I can not commit and he can not push until one of us make a branch. Is there a better way to do this? is there a way to not have our local master checkout? I guess I can always create a bare git locally and both of us can point to it but that seem a bit over killing. The goal is for him to merge easily his change to mine and then when I'm online I will commit all of that.

¿Fue útil?

Solución

There are some details missing from the question, but I'll attempt to answer anyways. First of all, if you both have your own copies of the repo, then you both should be able to commit changes to your own local copies.

If your friend is trying to push his changes to your local repo, it sounds like it's not working because you currently have master checked out, which would make sense as a safety feature...you wouldn't want to have Git silently swap out your working copy from right under your nose without warning. You can allow Git to move your master branch/reference/label/pointer by checking out the commit it's on directly:

$ git checkout head

Then your friend would probably be able to push his changes to your repo. After he's done, be sure to checkout the master branch again in your local repo (if you want to work on it):

$ git checkout master

However, people don't usually push changes to other people's personal/private repos. They usually send each other pull requests instead, and fetch each other's changes. So you could add your friend's remote and fetch his changes and then merge them in (or pull to do both):

$ git add <friend name> <url or path to friend remote>

$ git fetch <friend remote>
$ git merge <friend>/master

# Or combine fetch and merge using pull
$ git pull <friend> master

Alternatively, instead of fetching and merging (or pulling) your friend's changes into your local master branch, you can rebase your local changes on top of his changes:

$ git fetch <friend remote>
$ git rebase <friend>/master master

Otros consejos

Just don't use push. Add each other as remote and use git fetch/merge, cherry-pick, rebase or whatever you find appropriate to pull in each others contributions.

@cupcake's answer is fine, but there is nothing overkill about setting up a bare repo on your machine which acts as a central remote repository for both you and your friend. It's actually a lot simpler and makes life easier once its setup:

Prepare your new bare repository

mkdir newrepo
cd newrepo
git init --bare

Back to your existing repo:

git remote add new_repo_alias /path/to/newrepo
git push new_repo_alias something

Your friend does the same using ssh://..., but you have already figured out that part.

easy!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top