Serverspace Black Friday
JT
September 13 2024
Updated September 13 2024

Git branches - how to create and remove?

Git

Teamwork requires tools, and Git, or a repository version control system, is a good example for collaborative development and documentation. It allows team members to maintain their local parts of the project and make changes on a single server with the repository. An important mechanism is the Git branch system, which will be discussed in this article!

What are Git branches?

A branch or branch is a pointer to a series of commits that are united on the basis of their pedigree. They allow you to introduce the functionality of parallel development and data storage relative to each other.

For example, you have a main project and it is stored in the main branch, files and data can be downloaded from it and documentation can be accessed. But bug fixes, new functionality is usually tested and maintained in a separate space, so as not to affect the main project - that's what branches are for!

Schema of work
Screenshot №1 — Schema of work

Since a branch in Git is a pointer, it must reference at least one commit. Usually, when creating a new branch, the first commit is the one the user is currently working with. You can view your current commit with the git show command:

Head
Screenshot №2 — Head

In this case, HEAD points to commit <b782c51...> in main. This will be the first object referenced by the new branch, other changes to the working directory will be child commits. How does this work in practice?

How do I create a local and remote Git branch?

If you don't have sufficient resources than you can perform actions on powerful cloud servers. Serverspace provides isolated VPS / VDS servers for common and virtualize usage.

Create Server
Screenshot №3 — Create Server

It will take some time to deploy server capacity. After that you can connect in any of the convenient ways.

Go to your working directory folder or clone it from GitHub:

git clone [your-repo]
Git clone
Screenshot №4 — Git clone

Or update an existing one using the command below. Note that origin points to the default path, if you need a different repository, specify it manually:

git pull origin
Git pull
Screenshot №5 — Git pull

After that, let's type the already known command and make sure that the right Git branch and commit is selected:

git show
Head
Screenshot №6 — Head in Git

You can only create a branch in a remote repository if there is a local one on the device - this is due to the principle that the branch cannot be empty. To create a local branch, write the command:

git branch new_branch && git branch --list
New Git branch
Screenshot №7 — New Git branch

In order to start working with a branch, you need to switch to it and check the state of the repository:

git checkout new_branch && git show
Switch to branch
Screenshot №8 — Switch to branch

In order to create a remote Git branch, you must run the push command against the selected commit:

git push origin new_branch
Create remote branch
Screenshot №9 — Create remote branch

Ok, so what if a thread became unnecessary or was posted by mistake, how do you delete it?

How do I delete local and remote branches in Git?

There are similar commands for this, to delete a local branch - switch to a neighbouring branch:

git checkout main

After that you can interact with it with the command:

git branch -d new_branch
Delete local branch
Screenshot №10 — Delete local branch

The last line indicates that the branch with id - b782c51 was deleted, you can check the current ones with the command:

git branch --list

To delete a remote branch or a branch on a remote repository, you can use the git push command:

git push -d origin new_branch
Delete remote branch
Screenshot №11 — Delete remote branch

To check for changes, access the repository via the web or update it with git pull, after which the list of branches can be displayed with git branche.

This is a key mechanism when working on team projects using the Git system. Branches allow you to work on files in parallel with the main development pipeline, which speeds up user collaboration.

Vote:
5 out of 5
Аverage rating : 5
Rated by: 1
33145 North Miami, FL 2520 Coral Way apt 2-135
+1 302 425-97-76
700 300
ITGLOBAL.COM CORP
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.