Reputation: 2129
I am bit confused on how to create a duplicate new branch.
I have a github branch called abc
now I want to create a exact duplicate of that branch with a different name (i.e., lets say def
), All the data in def
should be exactly similar to abc
branch the only difference should be the name of the branch.
I have cloned the repo and checkout the branch using,
git clone https://...url
git checkout abc
What else I have to do?
Upvotes: 1
Views: 3258
Reputation: 3500
I am bit confused on how to create a duplicate new branch.
I believe much of the confusion comes from the a lack of understanding of what a branch is.
Because a branch in Git is actually a simple file that contains the 40 character SHA-1 checksum of the commit it points to, branches are cheap to create and destroy. Creating a new branch is as quick and simple as writing 41 bytes to a file (40 characters and a newline). source
So, a branch is just a pointer to a commit. If you create a new pointer to the same commit, you'll have a duplicate of that branch. As other have already pointed out, go to the commit pointed by your branch-to-be-cloned and run git switch -c newBranch
or git checkout -b newBranch
or git branch newBranch
, and you'll create a new pointer to the same commit.
From here, the two branches may diverge, but as long as the history that leads to that commit is intact, they'll show the same content.
Your content is defined by tracing back the previous edits from the commit the branch is pointing to.
Some consequences:
I have a github branch called abc now I want to create a exact duplicate of that branch with a different name (i.e., lets say def), All the data in def should be exactly similar to abc branch the only difference should be the name of the branch.
You don't, in the end. From the same source as above,
in sharp contrast to the way most older VCS tools branch, which involves copying all of the project’s files into a second directory
in git you are not copying or duplicating, just creating a new, floatable, reference to a point in time. So, you just duplicate the pointer, not the content.
Upvotes: 2
Reputation: 522141
Just create a new branch from abc
:
git branch def
If you want to also switch to that new branch while creating it, then use:
git checkout -b def
Note that a branch is really just a pointer to a commit. So, to create a "duplicate" of abc
not very much has to move, just a new reference to the HEAD commit of abc
needs to be created.
To push the def
branch to GitHub, you may use something like:
git push origin def
Upvotes: 4
Reputation: 1697
git checkout -b def
This will tell git
to checkout a new local branch (that's what the -b does) called def
Upvotes: 0