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!
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:
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.
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]
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
After that, let's type the already known command and make sure that the right Git branch and commit is selected:
git show
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
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
In order to create a remote Git branch, you must run the push command against the selected commit:
git push origin new_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
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
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.