How to create a Branch from a Tag in Git ?
In this sneppet you will learn how to create a Branch from Tag in Git using the “git checkout” command with -b option followed by new branch name and tag name. You will also learn how to create a Git branch from a tag with current local changes.
create a Branch from Tag in Git
1. First, you need to fetch tags from your remote repository using “git fetch” command followed by “–all” and “–tags” options.
$ git fetch --all --tags Fetching origin remote: Enumerating objects: 1, done. remote: Counting objects: 100% (1/1), done. remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (1/1), 149 bytes | 18.00 KiB/s, done. From https://github.com/sneppets/gitdemo * [new tag] v2.0 -> v2.0
2. After that, you fetch tags list using the “git tag” command followed by “-I”. Then check if the tag exists, from which you wanted to create branch.
$ git tag -l v1.0 v2.0
3. Finally, after confirming v2.0 tag is available, now you can create a Git Branch from a Tag using the “git checkout” command as shown below.
$ git checkout -b branch_new tags/v2.0 Switched to a new branch 'branch_new'
Create a new branch with current local changes
Let’s say you wanted to create a new branch from a tag without loosing the current local repository changes. For that, before you run “git checkout” command, you need to record the current state of working directory using “git stash” command.
$ git stash Saved working directory and index state WIP on main: 5204209 third line
After you saved the current state of your working directory, you can run “git checkout” command.
$ git checkout -b branch_new tags/v2.0 Switched to a new branch 'branch_new'
Now, try the “git branch” command to check the existing branch.
$ git branch * branch_new main
You need to run the following “git push” command followed by “–set-upstream” option, origin and the new branch name. So that the current branch is pushed to the expected upstream branch.
Please note, as a safety measure, the push will be aborted if the upstream branch does not match with the local one.
$ git push --set-upstream origin branch_new Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 remote: remote: To create a merge request for branch_a, visit: remote: https://github.com/sneppets/gitdemo/-/merge_requests/new?merge_request%5Bsource_branch%5D=branch_new remote: To https://github.com/sneppets/gitdemo.git * [new branch] branch_new -> branch_new Branch 'branch_new' set up to track remote branch 'branch_new' from 'origin'.
After this, you need to run the “git stash” command followed by “apply” option to restore the stashed changes to local.
$ git stash apply On branch branch_new Your branch is up to date with 'origin/branch_new'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: file1.txt no changes added to commit (use "git add" and/or "git commit -a")
Now, your current branch is the new one that you created from a Git tag and you also restored the stashed changes. Finally, you can run “git add”, “git commit” and “git push” commands to commit and push the changes to remote repository.
Hope you find this sneppet useful 🙂
You’ll Also Like
- Throw away the local commits from your branch in Git ?
- Your branch and ‘origin/main’ have diverged, how to fix this ?
- How to connect to Github using SSH ?
- How do you do undo git reset –hard HEAD^ and revert to previous commit ?
- Git – How to compare files in two different branches
- Git not working with a proxy server – gnutls_handshake() failed
- npm ERR! enoent spawn git ENOENT
- GitHub unable to access HTTPS and SSL routines error
- Remove http and https proxy settings of npm and git ?
- Fix Nodejs Error: ENOENT: no such file or directory
- Cache https credentials for pushing commits to git repository ?