Thursday, 6 November 2025

Git - create dev branch and merge it to main

/var/www/html is my git repository.

[root@devopsvm01 html]# pwd

/var/www/html

 [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 13:52 .git
[root@devopsvm01 html]#
[root@devopsvm01 html]# git branch
* main
[root@devopsvm01 html]#
[root@devopsvm01 html]# git branch feature-1/branch-1
[root@devopsvm01 html]#
[root@devopsvm01 html]# git branch
feature-1/branch-1
* main
[root@devopsvm01 html]# git checkout feature-1/branch-1
Switched to branch 'feature-1/branch-1'
[root@devopsvm01 html]# git branch
* feature-1/branch-1
main
[root@devopsvm01 html]#


When you create a new branch in Git, such as "feature-1/branch-1" from the "main" branch, this new branch initially contains all the files and the code state of the branch it was created from (in this case, "main"). This is because a branch in Git is essentially a lightweight movable pointer to a specific commit, so the new branch points to the same commit as "main" at creation. The branch pointers are stored internally in the Git repository's metadata, not as physical folders.


I've added another file called "offer.html" from my feature-1/branch-1


[root@devopsvm01 html]# git status

On branch feature-1/branch-1

nothing to commit, working tree clean

[root@devopsvm01 html]#


[root@devopsvm01 html]# vi offer.html

[root@devopsvm01 html]# ls -lrt

total 16

-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

-rw-r--r--. 1 root root 1314 Nov  2 14:18 offer.html

[root@devopsvm01 html]#

[root@devopsvm01 html]# git status

On branch feature-1/branch-1

Untracked files:

  (use "git add <file>..." to include in what will be committed)

        offer.html

nothing added to commit but untracked files present (use "git add" to track)

[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 status

On branch main

Your branch is up to date with 'origin/main'.

Untracked files:

  (use "git add <file>..." to include in what will be committed)

        offer.html


nothing added to commit but untracked files present (use "git add" to track)

[root@devopsvm01 html]#


Here on main branch also file is showing up as "untracked" , But I did the changes from the feature-1/branch-1 branch, Why ? 

The Golden Rule of Git :

The Working Directory is shared, the Commit History is separate.

Any file you create or edit on your hard drive, before you git add and git commit, remains there, regardless of which branch you switch to. Git only cares about files that are tracked (added and committed) in a branch's history.

Let's checkout  to feature-1/branch-1 branch and modify one file to see how git diff works. 

[root@devopsvm01 html]# git checkout feature-1/branch-1
Switched to branch 'feature-1/branch-1'
[root@devopsvm01 html]#

[root@devopsvm01 html]# vi services.html
[root@devopsvm01 html]# ls -lrt
total 16
-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 1314 Nov  2 14:18 offer.html
-rw-r--r--. 1 root root 2459 Nov  2 14:50 services.html
[root@devopsvm01 html]# git status
On branch feature-1/branch-1
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   services.html

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        offer.html

no changes added to commit (use "git add" and/or "git commit -a")
[root@devopsvm01 html]#
[root@devopsvm01 html]# git diff
diff --git a/services.html b/services.html
index b2c8c3d..2465823 100644
--- a/services.html
+++ b/services.html
@@ -68,7 +68,7 @@
     <hr>

     <footer>
-        <p>&copy; 2025 Tech Solutions. Contact us for a consultation!</p>
+        <p>&copy; 2025 Tech Solutions Ltd. Contact us for a consultation!</p>
     </footer>

 </body>
[root@devopsvm01 html]#

it's doing a comparison of diff --git a/services.html b/services.html

a/ - The version of the file in the index (staging area) or the last committed state of the branch.

b/ - The version of the file in your Working Directory (what you see in /var/www/html/ right now).


When you add the modified/new files using git add . , it tells Git to look at your entire current directory (and all subdirectories) and update the staging area with all changes from your working directory.
[root@devopsvm01 html]# git add .
[root@devopsvm01 html]#
[root@devopsvm01 html]# git status
On branch feature-1/branch-1
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   offer.html
        modified:   services.html

[root@devopsvm01 html]# git diff
[root@devopsvm01 html]#

[root@devopsvm01 html]# git commit -m "Added new files and modified services.html"
[feature-1/branch-1 56dd543] Added new files and modified services.html
 2 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 offer.html
[root@devopsvm01 html]#

So the changes are committed from the feature-1/branch-1 branch.
At this stage, git show command will still shows the changes .

[root@devopsvm01 html]# git show | tail -10
+++ b/services.html
@@ -68,7 +68,7 @@
     <hr>

     <footer>
-        <p>&copy; 2025 Tech Solutions. Contact us for a consultation!</p>
+        <p>&copy; 2025 Tech Solutions Ltd. Contact us for a consultation!</p>
     </footer>

 </body>
[root@devopsvm01 html]#

Note, in certain case you may need to append the previous commit using 

git commit --amend 


Let's checkout to main branch, and see the status. 

[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 status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
[root@devopsvm01 html]#

push the code changes

For that we use git push origin <branch-name> command, and you need to be in the feature-1/branch-1 branch.

Here <branch-name> is the name of the local branch whose commits you want to upload.

[root@devopsvm01 html]# git push origin feature-1/branch-1

Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (11/11), 4.87 KiB | 831.00 KiB/s, done.
Total 11 (delta 3), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (3/3), done.
remote:
remote: Create a pull request for 'feature-1/branch-1' on GitHub by visiting:
remote:      https://github.com/mahekarthya/devops-demo-pvt/pull/new/feature-1/branch-1
remote:
To github.com:mahekarthya/devops-demo-pvt.git
 * [new branch]      feature-1/branch-1 -> feature-1/branch-1
[root@devopsvm01 html]#
[root@devopsvm01 html]#

Now, we can see feature-1/branch-1 branch in my github.




















Now create a Pull request (PR)























Here it compare the new branch that we created with main
























It shows the difference as well.






























Update the reviewer 



























Login as the reviewer account 


















Review the code and merge it 






















SSubmit merger request.




























And finally click on Merge pull request



















finally we can see below detail


























Once pull request is reviewed and merged , you could see the updated code/new files in the main branch.

Get the latest code changes in main branch

[root@devopsvm01 html]# git branch
* feature-1/branch-1
  main
[root@devopsvm01 html]# git status
On branch feature-1/branch-1
nothing to commit, working tree clean
[root@devopsvm01 html]#

[root@devopsvm01 html]# git checkout main
Switched to branch 'main'
Your branch is behind 'origin/main' by 2 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)
[root@devopsvm01 html]#

[root@devopsvm01 html]# git pull
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (1/1), 920 bytes | 920.00 KiB/s, done.
From github.com:mahekarthya/devops-demo-pvt
   34b74e3..b43016d  main       -> origin/main
Updating 08b4859..b43016d
Fast-forward
 main.html     |  1 +
 offer.html    | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 services.html |  2 +-
 3 files changed, 90 insertions(+), 1 deletion(-)
 create mode 100644 main.html
 create mode 100644 offer.html
[root@devopsvm01 html]#


[root@devopsvm01 html]# git status
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
[root@devopsvm01 html]#

By default, in GitHub, the user who opens a Pull Request (the requestor) can usually merge it themselves, and having a reviewer approve it is not mandatory.

To shift from a "trust-based" system to a "required-approval" system, you must implement Branch Protection Rules on the target branch (like main), as we discussed.

Steps to Enforce Mandatory Reviews

  1. Navigate to Repository Settings: Go to the main page of your repository on GitHub (gh.com in your diagram). Click on the Settings tab.

  2. Find Branches: In the left sidebar, click on Branches.

  3. Add a Rule: Under "Branch protection rules," click the Add rule button.

  4. Set the Target Branch: Set the Branch name pattern to main.

  5. Enable Review Requirement: Check the box next to:

    Require a pull request before merging

  6. Enforce Approvals: Under that option, check the box for:

    Require approvals

    • You can then specify the number of required approvals (e.g., 1).

  7. Restrict Commits (Optional but Recommended): You should also check the box to Restrict who can push to matching branches to prevent developers from bypassing the PR process.

Once these rules are set for main, the "Merge pull request" button will be greyed out for everyone until the required number of reviewers have approved the changes.














Click add branch ruleset












Give some name to the ruleset and select all branches.














Enforce Approvals: Under that option, check the box for:
























It looks like a premium feature, we can't avail it on free account. 



No comments:

Post a Comment

Building a Safer PostgreSQL CI/CD Pipeline with GitHub Actions: Dev → PR Review → Test Promotion

In my previous post, we explored a simple push-to-main deployment strategy . While functional, that model is not considered an industry best...