If you have just done git init in a new repository (as opposed to re-init-ing an existing repository), and have not yet run git commit, you are currently on an unborn branch (normally master-as-unborn-branch). What this means is that HEAD contains the name master, while the branch named master does not actually exist.

If you have furthermore not run any git add commands, your index is currently empty.

The state that you would like to have is to have the name master point to the same commit as foo/master, without changing the contents of the work-tree. But first you need to obtain all the commits from remote foo:

$ git fetch foo

Now you can create the branch itself:

$ git branch master foo/master

Since you're already on master—it was merely "unborn"—you are now still on master and master points to the same commit as foo/master.

Your index is still empty, so git status will show you as having deleted every file from the HEAD commit, with every file in the work-tree as an untracked file. If you would like to populate your index from the current commit, you can now do:

$ git read-tree HEAD

and then git reset various files to restore them. If not—if you just want to use the work-tree—you can simply git add . (though you may want to set up a .gitignore first).

Answer from torek on Stack Overflow
🌐
GitHub
github.com › jesseduffield › lazygit › issues › 5067
Create new local branch from remote but do not checkout · Issue #5067 · jesseduffield/lazygit
November 25, 2025 - Is your feature request related to a problem? Please describe. I've always want to favorite some remote branch, watch it's update and cherry-pick from it. So I create a new local branch from it. However, when I search it in the remote ta...
Author   jesseduffield
Discussions

How to make a branch exactly as another without making any changes to existing commits?
Checkout the feature branch. Do a soft reset of the feature branch to master. This will move feature to master but leave the working copy unchanged. The differences between master and what was on feature will be staged but not committed. Commit the changes. Now feature is a single commit on top of master. Checkout master and merge feature into master. This will be a FF merge. Note that this moves the feature branch, which you may not want. In that case create a new branch where feature is and use that one instead (deleting it when finished). More on reddit.com
🌐 r/git
10
0
September 13, 2024
github - Using git push to create remote branch on the fly without creating local branch - Unix & Linux Stack Exchange
When I clone a repository from Github and checkout some stable branch, and make some local changes in it, then I don't want to create a local feature branch nor do I want the changes to remain in the local stable branch that I have checked out. I just want to make changes and push it to remote repo such that a feature branch is created there on remote, and my local stable branch remains clean. How can this be done without ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
July 20, 2016
Switch Git branch without files checkout - Stack Overflow
In v2.24 git switch is something like a safe git checkout. Hence I renamed the alias below to git hop for "hop on the branch without changing worktree" More on stackoverflow.com
🌐 stackoverflow.com
git - Commit a file to a Different Branch Without Checkout - Stack Overflow
It let's you edit specific files ... other branch completely (just the files you want to edit) and commit them. All this without ever affecting your working copy or staging area. Behind the scenes it uses a combination of git worktree and sparse checkout. The source is fairly small, so you can read through. ... While there is currently no single command to do this, there are at least two other options. You could use the github api to create the ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Super User
superuser.com › questions › 1474038 › how-to-create-local-branch-from-remote-branch-not-checkouting-this-branch
git - How to create local branch from remote branch not checkouting this branch? - Super User
August 22, 2019 - Let's say I have remote branch origin/foo. I want to create local branch foo pointing the same commit remote branch origin/foo is pointing now. I can do git checkout foo and this is what I need exc...
🌐
CloudBees
cloudbees.com › blog › git-create-branch
Git Create Branch: 4 Ways to Do It
October 2, 2018 - With Git this is possible...at least in regard to the files in our repository. You can, at any time, check out a commit if you know its hash: ... LANG:code You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout...
🌐
Reddit
reddit.com › r/git › how to make a branch exactly as another without making any changes to existing commits?
r/git on Reddit: How to make a branch exactly as another without making any changes to existing commits?
September 13, 2024 -

I have two branches, master and feature. Is there a git command equivalent of switching to feature, copying the contents of the entire repo, switching to master, deleting everything in the repo, and pasting the copied contents?

I want the master branch to be exactly the same as feature but the changes needed to achieve this should appear as a single commit in master branch after the latest existing commit in master.

🌐
Samuel Gruetter
samuelgruetter.net › blog › 2018 › 08 › 31 › git-ffwd-without-checkout
Git: Fast-forwarding a branch without checking it out
August 31, 2018 - The other direction sometimes also makes sense: Suppose I’m on master, and made something a bit experimental, did not commit it yet, and want to commit it to branch featureX. If there was no branch called featureX yet, I could just do git checkout -b featureX, but if it already exists (but has been merged into master), I first have to update it to current master.
🌐
Fabian Lee
fabianlee.org › 2023 › 08 › 05 › git-create-a-new-empty-branch-with-no-history-or-commits
Git: create a new empty branch with no history or commits | Fabian Lee : Software Engineer
September 17, 2023 - # create fresh branch git switch --orphan my-fresh-branch # validate, no commit history on this branch git log # commit and push git commit -a -m "initial commit of my-fresh-branch" --allow-empty git push -u origin my-fresh-branch · If you are using a git client older than 2.23, use ‘git checkout’ like below.
Find elsewhere
🌐
Git
git-scm.com › docs › git-checkout
Git - git-checkout Documentation
Running git checkout without specifying a branch has no effect except to print out the tracking information for the current branch. ... Create a new branch named <new-branch>, start it at <start-point> (defaults to the current commit), and check out the new branch.
🌐
Atlassian
atlassian.com › git › tutorials › using branches
How to Create a Branch in Git? | Atlassian Git Tutorial
December 15, 2025 - Note that this only creates the new branch. To start adding commits to it, you need to select it with git checkout, and then use the standard git add and git commit commands.
🌐
universejoker
universejoker.weebly.com › blog › git-create-new-branch-without-checkout
Git create new branch without checkout - universejoker
September 22, 2023 - Learn how to clone a specific branch with Git via this article. If we want to add this branch remotely, all we have to do is push it to our Git provider such as GitHub using the command below: $ git...
🌐
Git
git-scm.com › docs › git-switch
Git - git-switch Documentation
Specifying a <start-point> allows you to create a branch based on some other point in history than where HEAD currently points. (Or, in the case of --detach, allows you to inspect and detach from some other point.) You can use the @{-<N>} syntax to refer to the <N>-th last branch/commit switched to using git switch or git checkout ...
Top answer
1 of 11
24

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.

2 of 11
21

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.
🌐
GitHub
github.com › dwyl › gitea › issues › 34
Create local branch without switching to it · Issue #34 · dwyl/gitea
May 26, 2022 - The local_branch_create creates the branch and currently switch to it (using git checkout -b).
Author   dwyl
Top answer
1 of 4
12

The git branch command will let you create a new branch without needing to do any checking out or changes to local files. Then you would need to push the new remote branch.

The specific steps that you would need to do:

  1. git branch master-copy origin/master -- Create a local copy of the branch that you will make into a new remote
  2. git push origin master-copy -- Push the local branch to the remote
  3. git branch -D master-copy -- Delete the local branch copy

All of this is done without doing any checkout of files.

2 of 4
5

What you need to do is to simply create a remote branch pointing to commit 1.

Short answer

git push origin [commit1 hash]:refs/heads/master-copy

Long answer

Lets say this is you repository:

commit 2 (65bc341)   O  <-- master
                     |
     (more commits) ...
                     |
commit 1 (125afe4)   O  <-- origin/master

To achive what you want you use the git push command, you don't need to create a local branch. You need to pass three parameters to the push command:

git push [REMOTE NAME] [WHERE SHOULD THE REMOTE BRANCH POINT TO]:[REMOTE BRANCH NAME]

Assumming you are following standard names, REMOTE NAME willl probably be "origin".

The REMOTE BRANCH NAME is master-copy.

And finally, WHERE SHOULD THE REMOTE BRANCH POINT TO is the hash of the commit1 (125afe4 in the example I used above)

...so the command would be:

git push origin 125afe4:master-copy

Whith this command you are telling git that in the remote named "origin" you want to create a new branch named "master-copy" pointing to commit 125afe4.

If you run this command (and you did not create any local branch called master-copy) you will get this error

error: unable to push to unqualified destination: master-copy
The destination refspec neither matches an existing ref on the remote nor
begins with refs/, and we are unable to guess a prefix based on the source ref.
error: failed to push some refs to 'git@WHATEREVER_YOUR_REPO_URL_IS'

Why you get this response? Because git doesn´t know if you are trying to create a branch or a tag in the remote repository. To resolve the ambiguity you run

    git push origin 125afe4:refs/heads/master-copy

et voila!

commit 2 (65bc341)   O  <-- master
                     |
     (more commits) ...
                     |
commit 1 (125afe4)   O  <-- origin/master origin/master-copy

If you follow the steps in the accepted answer, the local branch "master-copy" that you create and later delete is the one that resolves the ambiguity. I hope I could provide you with some insights about how git-push works and show you that you don't really need this local branch. You can get what you need with one command instead of three!

Remember, if you want to remove the remote branch later you can run

git push --delete origin master-copy

or you can use the older syntax

git push origin :master-copy
🌐
Reddit
reddit.com › r/git › possible to create a new branch without cloning the repo locally?
Possible to create a new branch without cloning the repo locally? : r/git
December 10, 2021 - ... git init git checkout -b <newBranch> git remote add origin <repourl> git commit git push origin <newBranch> ... You may add a "branch" like this to a non-empty remote repo, but the "branch" becomes an orphan without any common history until it is merged into some other branch (may be useful ...
🌐
Quora
quora.com › How-do-you-create-a-new-branch-on-GitHub-using-git
How to create a new branch on GitHub using git - Quora
Answer: Using the command line, you can create and switch to a new branch by running: [code ]git checkout -b new-branch-name[/code] Then push the branch to GitHub with: [code ]git push -u origin new-branch-name[/code] This establishes a separate line of development, allowing you to work independe...
🌐
Atlassian Community
community.atlassian.com › q&a › bitbucket › questions › unable to create new branches: not through the bitbucket cli and not through terminal
unable to create new branches: not through the bitbucket cli and not through terminal
October 26, 2021 - Now, going back to the problem you have reported about not being able to create new branches, you mentioned that after you created the branch on your remote repository, you ran the command "git checkout -b feature/foo". Indeed, it will create a new branch, however without upstream and you won’t be able to push it or create a pull request.