Go to the git repository, (A Git repository is just a normal folder that contains a hidden subdirectory named .git.) .
[root@devopsvm01 html]# ls -lart
total 12
drwxr-xr-x. 4 root root 33 Nov 2 09:13 ..
-rw-r--r--. 1 root root 3561 Nov 2 09:20 about.html
-rw-r--r--. 1 root root 3081 Nov 2 09:40 index.html
-rw-r--r--. 1 root root 2455 Nov 2 10:46 services.html
drwxr-xr-x. 3 root root 75 Nov 2 10:46 .
drwxr-xr-x. 8 root root 166 Nov 2 10:49 .git
[root@devopsvm01 html]#
The git branch command, which is used to list, create, or delete branches, can only run inside a folder that has a .git directory.
[root@devopsvm01 html]# git branch
* main
[root@devopsvm01 html]#
The asterisk (*) next to main indicates that you are currently on the main branch.
Since only one branch name is listed, the repository currently only has the main branch.
The Two-Step Approach to create new Branch
This is useful if you want to create the branch but stay on your current branch for a moment.
Create the branch:
Switch to the new branch:
Let's do it.
[root@devopsvm01 html]# git branch
* main
[root@devopsvm01 html]# git branch feature/branch-1
[root@devopsvm01 html]#
[root@devopsvm01 html]# git branch
feature/branch-1
* main
[root@devopsvm01 html]#
In Git: The / (forward slash) in a branch name is only used for organization and convention, not for creating a physical directory in your file system.
When you use git checkout -b feature/my-feature, Git creates that branch pointer in its hidden internal .git/refs/heads/ directory, but your actual working directory (/var/www/html/) remains the same. The slash does not create a subfolder in /var/www/html/.
[root@devopsvm01 html]# git checkout feature/branch-1
Switched to branch 'feature/branch-1'
[root@devopsvm01 html]#
[root@devopsvm01 html]# git branch
* feature/branch-1
main
[root@devopsvm01 html]#
The one-step approach to create new branch
git checkout -b <branch-name> - Creates a new branch and immediately switches you to it.
git checkout -b feature/branch-2
[root@devopsvm01 html]# git checkout -b feature/branch-2
Switched to a new branch 'feature/branch-2'
[root@devopsvm01 html]#
[root@devopsvm01 html]# git branch
feature/branch-1
* feature/branch-2
main
[root@devopsvm01 html]#
1. Deleting a Local Branch
You must be checked out to a different branch (e.g., main) before attempting to delete a branch.
Example,
[root@devopsvm01 html]# git branch -d feature/branch-2
error: cannot delete branch 'feature/branch-2' used by worktree at '/var/www/html'
[root@devopsvm01 html]#
[root@devopsvm01 html]# git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
[root@devopsvm01 html]#
[root@devopsvm01 html]# git branch -d feature/branch-2
Deleted branch feature/branch-2 (was 08b4859).
[root@devopsvm01 html]#
[root@devopsvm01 html]# git branch
feature/branch-1
* main
[root@devopsvm01 html]#
2. Deleting a Remote Branch
Deleting a local branch does not delete the branch on your remote repository (like GitHub or GitLab). You need a separate command for that:
No comments:
Post a Comment