

For example, a remote branch is often something like the "counterpart" of a local one. They are stored and managed as independent objects in Git.īut in real life, of course, local and remote branches often do have a relationship with each other.

GIT CREATE BRANCH TRACKING REMOTE HOW TO
How to Track Branches in Gitīy default, local and remote branches have nothing to do with each other. But one parameter, the -u flag, is worth explaining – which I'll do in the next section.īut to give you the short version here: it tells Git to establish a "tracking connection" which will make pushing and pulling much easier in the future. The command, overall, is probably not a big surprise for you. We can "upload" what we have locally to the remote and thereby share it with our team: $ git push -u origin What we can do, however, is publish an existing local branch on a remote repository. How to Publish a Branch in GitĪs I already said in the section about "creating branches" above, it's not possible to create a new branch on a remote repository. I think it makes a lot of sense to move away from the checkout command – because it's used to perform so many different actions – and instead move towards the new switch command, which is absolutely unambiguous about what it does. However, because the git checkout command has so many different duties, the Git community (fairly recently) introduced a new command you can now also use to change the current HEAD branch: $ git switch Having said that, it makes sense that switching the currently active branch is one of the most-used actions any developer performs when working with branches.Īnd since switching branches is also referred to as "checking out" branches, you won't be surprised to learn the command that's used to make this happen: $ git checkout Or in other words: the current HEAD branch is where new commits will be created. The current branch (also referred to as the HEAD branch) defines the context you're working in at the moment. If you're using a Git desktop GUI like Tower, you won't be bothered with these details: you can simply rename both local and remote branches from a contextual menu (no need to delete and re-push anything): How to Switch Branches in Git

# Then, simply push the new local branch with the correct name: In practice, renaming a remote branch can be done by deleting the old one and then pushing up the new one from your local repository: # First, delete the current / old branch: If you'd like to rename a remote branch, things are a little bit more complicated - because Git doesn't allow you to rename remote branches. These commands, again, are used to work with local branches. In case you'd like to rename a different local branch (which is NOT currently checked out), you'll have to provide the old and the new name: $ git branch -m If you want to rename your current HEAD branch, you can use the following command: $ git branch -m That's why Git makes it pretty easy to rename a local branch. Mistyping a branch's name or simply changing your mind after the fact is all too easy. "Creating" branches in a remote repository happens by publishing an existing local branch - which we'll talk about later. It goes without saying that you can only create new branches in your local repository. If you'd like your new branch to start at a specific revision, you can simply add the revision's SHA-1 hash: $ git branch 89a2faad When providing just a name to the git branch command, Git will assume that you want to start your new branch based on your currently checked out revision. So let's start by talking about how to create branches: $ git branch Here's an overview of what we're going to cover:īefore you can work with branches, you need to have some in your repository. But I will provide links if you want to learn more. I didn't want to produce a book-length article, so I won't go into detail for all of the actions. My intention with this post is to create a comprehensive overview of the things you can do with branches in Git. You can create and delete them, rename and publish them, switch and compare them. And there's an endless amount of things you can do with them. Branches are one of the core concepts in Git.
