user_12
user_12

Reputation: 2129

How to duplicate a branch and all the data in it?

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

Answers (3)

Daemon Painter
Daemon Painter

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:

  • As long as the referenced commit exists, both branches will show the same history, regardless of the changes you make
    • If one branch is moved forward to a new commit, child of the original reference commit, this branch will "include" the other branch up to the point where they diverge;
    • If you rebase one of the branches, that is if you rebase the reference commit, things might get fuzzy and you might break others people work. Check out the pictures on git-scm: they use backwards arrows to better represent how to read a branch;
    • If you'd like to freeze the duplicate branch in time, you should consider a tag. A tag is not meant to be moved around, unlike a branch, but in the end it is a reference to a specific commit. Just like a branch, knowing the SHA of the reference commit will allow you to re-trace your history and get the "duplicated" content, but unlike a branch it is not meant for active development.
  • 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

Tim Biegeleisen
Tim Biegeleisen

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

Sal
Sal

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

Related Questions