In the previous post on Git, changes have been made directly on the main branch*.
*(The “master” branch was the default branch name for any git's repository. This has been changed to “main” to promote a more inclusive language).
Let’s see how to set up new branches.
(Thanks to Kevin Stratvert for the tutorial 'Git and GitHub for Beginners Tutorial', available here).
Before we do that, what is a new branch?
It is a copy of the main branch in which we want to modify single aspects of our code without affecting the main branch. Once all the necessary changes have been made, we can merge it back into the main branch.
In other words, you can create a new branch for a new feature or for a bug fix etc. without affecting "main".
Source |
To create a new branch, type this on your terminal:
git branch NameOfTheChange
"NameOfTheChange" was made up. It's just the name of the new branch.
PS: make sure to be in the right directory where the repository is! E.g.: cd C:\Users\etc.
git branch -> to check how many branches have been made. The * sign tells us in which branch we are currently operating.
For the command above, I used the PowerShell terminal in Visual Studio Code. I discovered that my "main" branch is called “master” by my terminal, despite having specifically named it "main" when I previously set it up. If this is the case, if you are in the "master" branch, use git branch -m main
(-m stands for "move/rename").
git switch NameOfTheChange -> to switch branch.
Let’s see the branch in action. I am expecting to make a change to my branch without affecting any other branch.
I have a text file (.txt) in my repository, so I modify and commit it. I proceed in this order:
1) I modify the file manually while being in "NameOfTheChange". Save it.
2) git status -> to verify the modification.
3) git commit -a -m “description of what I am doing” -> to commit the change.
Remember that -a is to force commit for a change we haven’t staged. -m is used to add comments to the commit.
If I now switch the branch, from “NameOfTheChange” to “main”, I expect the text file in the repository to not show my last change. The repository should change according to which branch I am currently working with. I type the switch command:
git switch main
Note: Watch out if your “main” is called “master” like in my case!
I created a new file in my repository called text.txt with random words in it. I am in the “NameOfTheChange” branch. I commit it in that branch but, again, both branches display this file. I was expecting only “NameOfTheChange” to display it.
I tried the same process, but I accidentally hit some wrong button on my terminal during the commit. Now any further commit makes me display the following error:
'Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier: remove the file manually to continue.'
1) I accessed the hidden folder of Git (.git) in the repository.
2) I delete the "index.lock" file*.
3) I checked back my git status.
4) I properly committed the previous changes (adding a comment too!)
*(‘When you perform a Git command that edits the index, Git creates a new index.lock file, writes the changes, and then renames the file. The index.lock file indicates to other Git processes that the repository is locked for editing.’ Source).
I noticed that changes on .htm files in my repository are identified by Git, while changes on .txt files are not (in my case, I simply opened a .htm file with Notepad and modified it). I am still looking for an explanation for this behaviour. In conclusion, I tested the branches by modifying a .htm file in one branch and committing the change in that branch only. When I switched the branch I noticed that, as expected, in this last branch the same .htm file had no changes. In this way, I demonstrated that branches are independent: code can be modified in one branch without affecting the same code in other branches.
git merge -m “Type a comment here” NameOfTheChange
“NameOfTheChange” is the branch to merge with “main” (you must be in “main” to do it, use git switch).
I can delete now the “NameOfTheChange” branch:
git branch -d NameOfTheChange
-d stands for "delete".
git switch -c NameOfABranch -> create a new branch and automatically switch to it (-c stands for “create”).
When you merge a branch with “main” and both have the same file with different text or code, Git will report a CONFLICT, the type of conflict (e.g., content), and for which file. We must resolve the conflict (e.g., manually) before the merge can take place.