Switch git branch without checking files

Is it possible in git to switch to another branch without checking all the files? After switching the branches, I need to delete all the files, restore them, commit and return them back. Therefore, checking files is just a waste of time (and about 14,000 files is a lengthy operation).

To make everything clear:

I need all this to upload documentation to github.

I have a repo with gh-pages branch. When I rebuild the documentation locally, I copy it to the repo directory, commit and click on github. But I was unhappy because I had two copies of the documentation at the local level. And I decided to create an empty branch even after I switched to empty and deleted files. But switching is a lengthy operation, so I asked this question.

I know that I can just leave gh-pages on the branch and delete the files, but I don’t like dirty working trees)

+62
git git-checkout branch
Aug 15 '09 at 19:32
source share
9 answers

Yes you can do it.

git symbolic-ref HEAD refs/heads/otherbranch 

If you need to commit to this branch, you will need to reset the index, otherwise you will end up doing something based on the last posted branch.

 git reset 
+65
Aug 15 '09 at 21:29
source share

Using only the basic git commands:

This answer is a bit longer than Charles, but it consists only of the basic git commands that I can understand and thus remember, eliminating the need to continue searching.

Mark your current location (fix first):

 git checkout -b temp 

Reset (moves) the marker to another branch without changing the working directory:

 git reset <branch where you want to go> 

now temp and the other branch point to the same commit, and your working directory is not touched.

 git checkout <branch where you want to go> 

since your HEAD already points to the same commit, the working directory is not affected

 git branch -d temp 

Please note that these commands are also available to any graphical client.

+20
Jul 01 2018-12-12T00:
source share

Wouldn't it be a better solution to have two working directories (two workspaces) with one repository or even with two repositories?

contrib/ has a git-new-workdir tool to help you with this.

+14
Aug 16 '09 at 8:54
source share

You can overwrite your HEAD file with a different branch name:

echo "ref: refs / heads / MyOtherBranch"> .git / HEAD

+8
Aug 15 '09 at 19:55
source share

I think you are looking for the git read-tree plumbing team. This will update the index, but will not update the files in your working directory. For example, assuming branch is the name of the branch to read:

  git read-tree branch 

If you want to then commit the branch you just read, you will also need:

  git symbolic-ref HEAD refs / heads / branch 
+7
Aug 15 '09 at 21:31
source share

In the interest of the reader:

Although I believe Charles Bailey's solution is correct, this solution needs to be tuned when switching to something that is not a local branch. There must also be some way how to do this with regular commands that are easy to understand. Here is what I came up with:

 git checkout --detach git reset --soft commitish git checkout commitish 

Explanations:

  • git checkout --detach same as git checkout HEAD^{} , which leaves the current branch behind and goes into the "state of a separate head". Therefore, the following HEAD modification no longer affects any branch. Disabling HEAD does not affect the operating line or index.
  • git reset --soft commitish then moves the HEAD to the SHA of the given commitish . If you want to update the index, open --soft , but I do not recommend it. This, again, does not apply to the working line, and ( --soft ) is not an index.
  • git checkout commitish then attaches the HEAD to the given commitish (branches) again. (If commitish is a SHA, nothing happens.) This also does not affect the index and worktree.

This decision accepts everything related to commit, so it is perfect for some git alias. rev-parse below is just a test to make sure nothing in the chain breaks, so typos do not accidentally switch to the disconnected head state (error recovery would be more difficult).

This results in the following git switch treeish alias:

 git config --global alias.switch '!f() { git rev-parse --verify "$*" && git checkout "HEAD^{}" && git reset --soft "$*" && git checkout "$*"; }; f' 

FYI, you can find it in my list of git aliases .

+1
Jul 12 '17 at 14:10
source share

With so many files, it might be best for you to keep two repositories, one for each branch. You can pull changes back and forth as needed. This will be less surprising than trying to play tricky tricks with git.

0
Aug 15 '09 at 10:15
source share

If you are just trying to change the location of the remote branch, you can do this with git push without touching your local copy.

http://kernel.org/pub/software/scm/git/docs/git-push.html

The format of the <refspec> parameter is an optional plus +, followed by the source code ref <src>, followed by a colon :, and then the destination ref <dst>. It is used to indicate which <src> object should update the <dst> ref in the remote repository.

for example, to update foo to commit c5f7eba, follow these steps:

 git push origin c5f7eba:foo 

Not sure what it was after or not.

0
Feb 04 '10 at 16:16
source share

you can use

  1. git checkout -f <new-branch> 2. git cherry-pick -x <previous-branch-commit-id> 

previous-branch-commit-id is the commit from which you want to copy the old data.

0
Feb 22 '17 at 5:22
source share



All Articles