Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commit a file to a Different Branch Without Checkout

Is it possible to commit a file in a git branch with out checking out that branch? If so how?

Essentially I want to be able to save a file in my github pages branch without switching branches all the time. Any thoughts?

Update: It's not possible to do what I want (see comments below for use case). What I ended up doing is programmatically cloning my current directory to a tmp directory, then checking out my branch in that tmp directory (doesn't affect my working directory) and committing my files to the tmp directory clone. When I'm done, I push back to my working directory and delete the tmp directory. Sucks, but it's the only way to commit files to another branch without changing the current working branch of the working directory. If anyone has a better solution, please feel free to add it below. If it's better than 'it cannot be done', I'll accept yours.

like image 551
Schneems Avatar asked Oct 28 '11 18:10

Schneems


People also ask

How we can git push the changes to other branch without checkout to that branch?

Sometimes, you cannot merge a branch B into branch A without checking out A first if it would result in a non-fast-forward merge. This is because a working copy is needed to resolve any potential conflicts. git fetch with a refspec. This will help you to push the changes without any checkouts.

How do I commit and push to a different branch?

Create a new branch with the branch, switch or checkout commands. Perform a git push with the –set-upstream option to set the remote repo for the new branch. Continue to perform Git commits locally on the new branch. Simply use a git push origin command on subsequent pushes of the new branch to the remote repo.

How do I commit to a different remote branch?

In some cases, you may want to push your changes to another branch on the remote repository. In order to push your branch to another remote branch, use the “git push” command and specify the remote name, the name of your local branch as the name of the remote branch.


2 Answers

It's not possible.

The changes you commit are related to the current working copy. If you want to commit to another branch it means that you could commit changes from your working copy, but base them from another copy state.

This is not a natural way of versioning your work, and this is why you need to make different steps (stash changes, checkout the branch, pop stash and commit) to accomplish it.

As for your specific use case, a simple way is to keep two copies of your work, one checked out at master branch, and the other at pages branch.

In the pages working copy, add the master copy as a remote repo.

  • You commit pages on master
  • Pull from master on the pages copy
  • push to GitHub
  • reset the master branch at its previous state.
like image 176
CharlesB Avatar answered Sep 20 '22 10:09

CharlesB


It can be done by reimplementing git commit.

This can be done with various call to git hash-object

But this is hard to achieve.

Please read progit chapter 9 for more details and a full example of how to simulate a commit.

like image 42
p'tit fred Avatar answered Sep 20 '22 10:09

p'tit fred