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. That's because it has syntax highlighting for everything – making it easier to work with Git.Branches are one of the core concepts in Git. So, running the git branch -d command would only remove the branch locally.Īnd if you have issues working with Git, I suggest you switch your terminal to Git bash. ![]() That’s because you’ve pushed the branch already. If you check GitHub again, it won’t be there:īear in mind that to completely remove a Git branch from your project, you need to use the git push origin command. You can see the remote branch, test-branch1, is not listed anymore. To verify that the remote branch has been deleted, run git branch -a again. So the syntax representing the command for removing a remote branch looks like this: git push origin -d branch-name.įor instance, to remove the test-branch1 branch, I will run git push origin –d test-branch1: To completely remove a remote branch, you need to use the git push origin command with a -d flag, then specify the name of the remote branch. But if you run git branch -a, the branch will still be listed.Īnd if you check GitHub, the branch will still be there: If you try to delete a remote branch with the same command used for deleting a local branch, you will get a message that the branch has been deleted. If you type in the command correctly, you will get a response that the branch has been deleted. To delete a local branch, run git branch -d branch-name. ![]() In this situation, test-branch2 is a branch I’m yet to push, so it’s a local branch. This is a feature I've seen only in Git bash. If you run git branch -a in particular, it will make the remote branches distinct. Run git branch or git branch -a to see the branches you’ve created for your project. But it's okay if you use another terminal. I’ll use Git bash in this article because it makes working with Git easier than any other terminal. But firstly, let’s look at how to delete a local branch. In this article, I will show you how to delete a remote branch in Git. When you're working with Git, you might want to delete remote branches pushed to a platform like GitHub for various reasons.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |