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
If you accidently modified things in the wrong branch, here's some simple steps :
Everything should be fine after this. You can also merge, reset and cleanup your modifications selectively.
So long as you don't have anything in your current index that differs from your HEAD that you want to keep you can so something like this. (If you do want to keep your index you could temporarily export the GIT_INDEX_FILE environment variable to point at a temporary file for the duration of these commands.)
# Reset index and HEAD to otherbranch
git reset otherbranch
# make commit for otherbranch
git add file-to-commit
git commit "edited file"
# force recreate otherbranch to here
git branch -f otherbranch
# Go back to where we were before
# (two commits ago, the reset and the commit)
git reset HEAD@{2}
We've never actually checked out otherbranch and our working tree files haven't been touched.
As several others have said, it is literally possible, but impractical.
However, as of Git 2.5 (with some important fixes in 2.6 and minor ones since then), there is a practical method for doing this using git worktree add.
Let's say, for instance, that you want to work on branches main and doc "at the same time", or branches develop and test "at the same time", but the two branches in question deliberately contain different things. (For instance, the doc branch has documentation that exists outside or alongside the code, or the test branch has tests that will be run against the code, but not distributed, or which are expected to have failures for which tests are deliberately skipped on the develop side, or whatever.)
Instead of just:
git clone -b develop <source> theclone
followed by working in theclone with constant switching back and forth between the two branches, you would:
git clone -b develop <source> theclone
but then:
cd theclone
git worktree add ../ct test # check out branch test in ../ct
or just:
git worktree add ../test # check out branch test in ../test
Now you can run your tests in ../test while developing in theclone. You can merge and/or rebase changes from one branch to the other in the usual way: the underlying repository is already shared, so no git push or git fetch is required. You simply have both branches checked out, into two separate work-trees, named theclone and test from the top level.
This is how I do it:
if I've acidentally committed, roll back one commit:
git reset --soft 'HEAD^'
add the files you want to add
git add .
create a new temporary branch:
git checkout -b oops-temp
commit your changes:
git commit -m "message about this commit"
checkout the branch you meant to check out:
git checkout realbranch
merge the old branch:
git merge oops-temp
delete the old branch:
git branch -D oops-temp
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.
mastermaster on the pages copyI cannot agree it is not possible. By mixing git stash push, git stash pop, git checkout, git checkout, git add and git commit this is possible.
How I understand problem:
You are on branch master and you made some modifications to file patched.txt and you would like to commit this file to other branch.
What you would like to do is:
file.txt from stashed stackpatched (and only this file) to new branchfile.txtThis can be achieved by executing following commands:
destBranch=patch
thisBranch=master
FileToPutToOtherBranch="file1.txt file2.txt 'file with space in name.txt'"
message="patched files $FileToPutToOtherBranch"
#assumption: we are on master to which modifications to file.txt should not belong
git stash &&\ #at this point we have clean repository to $thisBranch
git checkout -b $destBranch &&\
git checkout stash@{0} -- $FileToPutToOtherBranch && #if there are many files, repeat this step #create branch if does not exist (param -b)
git add $FileToPutToOtherBranch &&\ # at this point this is equal to git add . --update
git commit -m "$message" &&\
git checkout $thisBranch &&\
git stash apply &&\ # or pop if want to loose backup
git checkout $thisBranch -- $FileToPutToOtherBranch # get unpatched files from previous branch
The reason why I am using "&&" and the end is if somebody will copy&paste this snippet into terminal, even if one error occurs, next commands will be executed, which is not good. \ is for informing shell that command is continued in next line.
To proove this works I provide testing environment for this snippet
mkdir -p /tmp/gitcommitToAnyBranch && cd /tmp/gitcommitToAnyBranch &&\
git init
echo 'this is master file' > file1.txt
echo 'this is file we do not want to have modified in patch branch because it does not patches any feature' > docs.txt
git add file1.txt && git commit -m "initial commit"
echo 'now this file gets patched' > file1.txt
git status
Now, if you run my script with parameters
destBranch=patch
thisBranch=`git rev-parse --abbrev-ref HEAD`
FileToPutToOtherBranch="file1.txt"
message="patched file $FileToPutToOtherBranch"
You will have file1.txt modified only in patch branch, for more see gitk --all