To create a new branch that is based on your currently checked out (HEAD) branch, simply use "git branch" with the name of the new branch as the only parameter: $ git branch How do I create a new branch based on some existing one? How do I create a new branch based on the current HEAD? There are a couple of different use cases when creating branches in Git. In fact, the power and flexibility of its branching model is one of the biggest advantages of Git! There are some other patterns too, of course - there always are - if you’re interested in checking those out too, read this Stack Overflow answer.Git makes creating and managing branches very easy. Read about how it works in a blog post by its author, Vincent Driessen. Git flow is a methodology for creating, naming, and merging branches that keeps your codebase in a “clean” state at all times, so that you can merge pull requests and create releases in a smooth, bug-free fashion. Unless you’re a solo developer working on a side project, it’s probably worth using. Now that you’ve gotten your head around the basics of branch creation, it’s a good time to advocate for using some best practices - generally speaking, you should stick to what’s called “git flow” for managing your repository. Best practices for creating and naming branch Interestingly, that relationship is not established automatically by Git - this is because a local branch can actually be linked to a branch of an entirely different name on the server. The `-set-upstream` flag creates a relationship between the branch on the server, and this branch locally. This command tells the remote server that a new branch has been created locally, so that it can recreate the same branch. > testing-repo git:(main) $ git push -set-upstream origin my-branch-name Now that we have a fundamental understanding of how branches and commits are stored and operated on, it’s time to explore the checkout command, which is where the magic in this shortcut happens. Then, it operates on an entire line of commits together. The same thing happens when you run any commands relating to a branch: Git checks the commit that the branch is pointing to, and then finds all of its parents by looking at those pointers. When you run `git log` or some other utility, or view commits on GitHub, those tools are examining the relationships between commits to present them in order.
![git create branch on commit git create branch on commit](https://www.testingdocs.com/wp-content/uploads/Git-Branching-.png)
Not only do branches point to commits, commits themselves can point to other commits. and zero or more pointers to the commit or commits that were the direct parents of this commit: zero parents for the first commit, one parent for a normal commit, and multiple parents for a commit that results from a merge of two or more branches. When you commit in Git, Git stores a commit object that contains a pointer to the snapshot of the content. That begs another question, then: how are commits ordered sequentially? That is, how can you tell what the other commits in a branch are, if a branch is just a pointer to one single commit? Clearly we aren’t dealing with folders here. And, think about it - you’d never have a branch stored inside of another branch. Branches being pointers implies that Git stores its data in a flat structure, rather than as a directory - which is actually true! In fact, all commits live next to each other at the same level. However, if a branch is just a “pointer” or reference to a specific commit, then that mental model doesn’t work at all. If that were the case, then it would make sense to want to create a branch as if you were creating a folder, using some kind of “create” command.
![git create branch on commit git create branch on commit](http://www.marinamele.com/wp-content/uploads/2014/07/Git_new_ideas.png)
This is a pretty interesting conceptual model, because if you’re anything like me, you initially imagined a branch as being more of a folder than anything else, with commits stored inside of it somehow. Every time you commit, it moves forward automatically. As you initially make commits, you’re given a master branch that points to the last commit you made. The default branch name in Git is master. Here’s what they have to say:Ī branch in Git is simply a lightweight movable pointer to. In order to understand what a branch is, let’s dive into the Git docs on branching. In this article, we’ll dive deep on Git branches to understand why they chose to express this behavior in such a non-obvious manner. However, if you understand how git branches work below the surface, you’ll realize why this makes sense, and is ultimately the better way of describing what happens when you create a branch. This is the fastest and easiest of creating a branch from the command line, but at first glance, this command is much less intuitive or easy to remember.
![git create branch on commit git create branch on commit](https://cdn-images-1.medium.com/fit/t/1600/480/0*RiQZEGNoU9HmyjFP.png)
> testing-repo git:(main) $ git checkout -b my-branch