Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6764809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:38:14+00:00 2026-05-26T14:38:14+00:00

Is it possible to commit a file in a git branch without checking out

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:38:15+00:00Added an answer on May 26, 2026 at 2:38 pm

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How can I commit only part of a file in git I
Possible Duplicate: How can I commit only part of a file in git How
Possible Duplicate: git: can i commit a file and ignore the content changes? I
Subversion: is it possible to commit local revisions without pushing them, and push them
I want to know whether it is possible to commit to a previous version.
Is it possible to clone only one branch (or from a given commit) in
I know that it's possible to get SVN to send emails after a commit
I accidentally pushed a commit to GitHub. Is it possible to remove this commit?
I have a local branch that is tracking of a remote branch on git.
Possible Duplicate: How to purge a huge file from commits history in Git? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.