Apache HTTP Server is one of the world's oldest, most popular, and most widely used web server programs. The /var/www/html is the default document root directory for the Apache HTTP Web Server on Linux distributions like Oracle Linux, Red Hat, Fedora, and CentOS. This is the directory where you place all the web files (HTML pages, images, etc.) for your main website. If a file named index.html or index.php exists directly inside /var/www/html, that is the file that is automatically shown to the visitor. I will use this default document root as my git repository and it will be pushed to github.
We can install apache software using
#dnf install httpd -y
After installing httpd ,follow below process , so that we can access the webpage.
# Start the httpd service and enable it to run at boot
sudo systemctl enable --now httpd
#To check the status of the service:
sudo systemctl status httpd
# Allow HTTP (port 80) traffic permanently
sudo firewall-cmd --permanent --add-service=http
# Allow HTTPS (port 443) traffic permanently
sudo firewall-cmd --permanent --add-service=https
# Reload the firewall to apply the changes instantly
sudo firewall-cmd --reload
For learning , I've created a sample website with below html files.
[root@devopsvm01 html]# pwd
/var/www/html
[root@devopsvm01 html]# ls -lrt
total 8
-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
[root@devopsvm01 html]#
Let's access the website.
Initialize a new Git repository using git init.
[root@devopsvm01 html]# pwd
/var/www/html
[root@devopsvm01 html]# git status
fatal: not a git repository (or any of the parent directories): .git
[root@devopsvm01 html]#
We are trying to use Git in the /var/www/html directory, but the directory has not been initialized as a Git repository.
The error message fatal: Not a git repository (or any of the parent directories): .git means that Git cannot find the hidden configuration directory (.git) that marks a location as a repository.
[root@devopsvm01 html]# git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in /var/www/html/.git/
[root@devopsvm01 html]# ls -lart
total 8
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
drwxr-xr-x. 3 root root 54 Nov 2 09:44 .
drwxr-xr-x. 7 root root 119 Nov 2 09:44 .git
[root@devopsvm01 html]#
Inside .git folder we can see the config file.
[root@devopsvm01 html]# cd .git/
[root@devopsvm01 .git]# ls -lart
total 16
drwxr-xr-x. 2 root root 21 Nov 2 09:44 info
drwxr-xr-x. 2 root root 4096 Nov 2 09:44 hooks
-rw-r--r--. 1 root root 73 Nov 2 09:44 description
drwxr-xr-x. 2 root root 6 Nov 2 09:44 branches
drwxr-xr-x. 3 root root 54 Nov 2 09:44 ..
drwxr-xr-x. 4 root root 31 Nov 2 09:44 refs
-rw-r--r--. 1 root root 92 Nov 2 09:44 config
drwxr-xr-x. 4 root root 30 Nov 2 09:44 objects
-rw-r--r--. 1 root root 23 Nov 2 09:44 HEAD
drwxr-xr-x. 7 root root 119 Nov 2 09:44 .
[root@devopsvm01 .git]# cat config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[root@devopsvm01 .git]#
Note:- Any file that you don't want to track by git can be included in a special config file called .gitignore. The .gitignore file is typically found in the root directory of your Git repository.
[root@devopsvm01 html]# git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
about.html
index.html
nothing added to commit but untracked files present (use "git add" to track)
[root@devopsvm01 html]#
Main is the modern and widely adopted default branch name used by most major hosting platforms and development teams. Let's adopt that naming standard.
[root@devopsvm01 html]# git config --global init.defaultBranch main
[root@devopsvm01 html]# git config --global init.defaultBranch
main
[root@devopsvm01 html]#
#Run the following command while you are still inside /var/www/html:
git branch -m main --> -m is to rename a branch
[root@devopsvm01 html]# git branch -m main
[root@devopsvm01 html]# git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
about.html
index.html
nothing added to commit but untracked files present (use "git add" to track)
[root@devopsvm01 html]#
Add and Push the files to github
1. First add git remote end point .
[root@devopsvm01 html]# git remote add origin git@github.com:mahekarthya/devops-demo-pvt.git
[root@devopsvm01 html]# git remote -v
origin git@github.com:mahekarthya/devops-demo-pvt.git (fetch)
origin git@github.com:mahekarthya/devops-demo-pvt.git (push)
[root@devopsvm01 html]#
Origin - This is the nickname you assign to the remote repository's URL. By convention, the primary remote repository that a local repository tracks is always named origin. You could technically name it anything (e.g., github-main), but using origin is the standard practice and is expected by many tools and scripts.
Now the config file has been updated.
[root@devopsvm01 .git]# cat config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = git@github.com:mahekarthya/devops-demo-pvt.git
fetch = +refs/heads/*:refs/remotes/origin/*
[root@devopsvm01 .git]#
[root@devopsvm01 .git]#
2. Now add all the files using git add command
[root@devopsvm01 html]# git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
about.html
index.html
nothing added to commit but untracked files present (use "git add" to track)
[root@devopsvm01 html]#
[root@devopsvm01 html]# git add .
[root@devopsvm01 html]# git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: about.html
new file: index.html
[root@devopsvm01 html]#
The git add . command moves the current versions of all modified and untracked files in your working directory to the staging area which prepares your changes for the next commit.. When you run git add ., Git takes the content of the staged files, compresses them, and stores them as a blob object in the hidden .git/objects directory. Git doesn't commit files directly from your working directory; it commits the files that are currently in the staging area.
3. Now do the git commit.
The git commit command is the fundamental action in Git that permanently records staged changes into your local repository's history. The command updates the current branch's pointer (like main) to point to this new commit object.
When you run git commit, you are required to provide a commit message to explain why the changes were made.
For example, git commit -m "Add index.html and update README"
[root@devopsvm01 html]# git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: about.html
new file: index.html
[root@devopsvm01 html]#
[root@devopsvm01 html]# git commit -m "initial commit"
[main (root-commit) c5f1294] initial commit
2 files changed, 182 insertions(+)
create mode 100644 about.html
create mode 100644 index.html
[root@devopsvm01 html]# git status
On branch main
nothing to commit, working tree clean
[root@devopsvm01 html]#
4. Push the changes to github using git push -u origin main
The term main (as in the command git push -u origin main) refers to a branch on your local repository called main.
When you push main to origin, you are saying: “Take my local branch main and push it to the remote repository origin as its branch named main.”
On the remote side, after you push it, there will be a branch main under origin if it didn’t already exist
-u (or --set-upstream) links your local main branch with the remote main branch, and remember this connection for future pushes/pulls
After you run this once, you can simply use:
#git push and git pull
without specifying branch names, because Git already knows which remote branch your local branch is connected to.
[root@devopsvm01 html]# git push -u origin main
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 2.48 KiB | 1.24 MiB/s, done.
Total 4 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:mahekarthya/devops-demo-pvt.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
[root@devopsvm01 html]#
Now we can see the files in github unser main branch.
Now I've added another files called services.html
[root@devopsvm01 html]# ls -lrt
total 12
-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
[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)
services.html
nothing added to commit but untracked files present (use "git add" to track)
[root@devopsvm01 html]#
[root@devopsvm01 html]# git add .
[root@devopsvm01 html]# git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: services.html
[root@devopsvm01 html]# git commit -m "Added services.html"
[main 08b4859] Added services.html
1 file changed, 75 insertions(+)
create mode 100644 services.html
[root@devopsvm01 html]# git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
[root@devopsvm01 html]# git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 1.38 KiB | 234.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To github.com:mahekarthya/devops-demo-pvt.git
c5f1294..08b4859 main -> 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]#
Once you create and commit new files, your local branch becomes one commit ahead of the remote origin/main branch until you push the changes.
No comments:
Post a Comment