Search code examples
gitversion-controlbranchgit-branch

Commit a file to a Different Branch Without Checkout


Is it possible to commit a file in a git branch without 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?

I needed to do this atomically without changing the current directory, so multiple commands won't work, even if they can be used as a one liner. Otherwise, you can end up with race conditions.

Update: It's impossible to do what I want (see comments below for use case). I ended up 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. I'll accept yours if it's better than 'it cannot be done'.

Update update: For full details of why I asked to be able to do this 11 years ago and why I wanted it to be atomic see this repo https://github.com/schneems/git_test. Back in the day we didn't have CI services. Developers at the company I worked for were expected to run tests locally, but not everyone did it. The idea behind that project was to track test run history. This would allow you to see which commit tests (or if they weren't run at all).

CI now handles this functionality but since I asked an "impossible question", I'm always curious if there is an answer.


Solution

  • 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.