Git Commands and Concepts for DevOps Engineers

Pankaj  RoyPankaj Roy
37 min read

📚 Getting Started with Git and GitHub

Step 1: Create a GitHub Account

Go to GitHub, sign up, and create your profile. GitHub is a cloud-based platform where you can store and manage Git repositories.

Setting Up Git on AWS /Azure Linux VMs

Create two Linux virtual machines in AWS:

  • One in Mumbai

  • One in Singapore

Make sure to allow SSH and HTTP in the security group.

🛠Install Git and Configure User Info

Note: Use sudo before the command if you're not logged in as the root user.

  • Use sudo su to switch to root user.
azureuser@mumbai-vm:~$ sudo su
root@mumbai-vm:/home/azureuser#
  • Run apt update to update package lists.
root@mumbai-vm:/home/azureuser# apt update
Hit:1 http://azure.archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://azure.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:3 http://azure.archive.ubuntu.com/ubuntu jammy-backports InRelease
Hit:4 http://azure.archive.ubuntu.com/ubuntu jammy-security InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.
  • Run apt upgrade -y to upgrade packages.
root@mumbai-vm:/home/azureuser# apt upgrade -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
  • Install Git using apt install git -y.
root@mumbai-vm:/home/azureuser# apt install git -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
git is already the newest version (1:2.34.1-1ubuntu1.15).
git set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
  • Check Git version using git --version.
root@mumbai-vm:/home/azureuser# git --version
git version 2.34.1
  • Set your Git username using git config --global user.name "Pankaj"
azureuser@mumbai-vm:~/mumbaiDir$ git config --global user.name "Pankaj"
root@mumbai-vm:/home/azureuser/mumbaiDir# git config --global user.email "Pankaj@gmail.com"
  • View Git configuration using git config --list
root@mumbai-vm:/home/azureuser/mumbaiDir# git config --list
user.name=Pankaj
user.email=pankaj@gmail.com

🔗Working with Local and Remote Repositories

🌏Mumbai VM Workflow

  • Login to Mumbai VM.

  • Create a directory and go inside it.

root@mumbai-vm:/home/azureuser# mkdir mumbaiDir
root@mumbai-vm:/home/azureuser# ls
mumbaiDir
  • Initialize Git using git init
root@mumbai-vm:/home/azureuser/mumbaiDir# 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:   git config --global init.defaultBranch <name>
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:   git branch -m <name>
Initialized empty Git repository in /home/azureuser/.git/
  • Create a file using touch myfile and write “My name is Pankaj”.
root@mumbai-vm:/home/azureuser/mumbaiDir# touch myfile
root@mumbai-vm:/home/azureuser/mumbaiDir# ls
myfile
  • Check repository status using git status
root@mumbai-vm:/home/azureuser/mumbaiDir# git status
On branch master
No commits yet
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        myfile
nothing added to commit but untracked files present (use "git add" to track)
  • Add changes to staging area using git add .
root@mumbai-vm:/home/azureuser/mumbaiDir# git add .
  • Commit changes to local repository using git commit -m "First commit from Mumbai VM".
root@mumbai-vm:/home/azureuser/mumbaiDir# git commit -m "First commit from Mumbai VM"
[master (root-commit) 6b8533c] First commit from Mumbai VM
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 myfile
  • Check status again using git status
root@mumbai-vm:/home/azureuser/mumbaiDir# git status
On branch master
nothing to commit, working tree clean
  • View commit history using git log
root@mumbai-vm:/home/azureuser/mumbaiDir# git log
commit 6b8533c5488c6c4def635b167f6b0c08596f60a0 (HEAD -> master)
Author: Pankaj <pankaj@gmail.com>
Date:   Tue Aug 12 12:49:02 2025 +0000

    First commit from Mumbai VM
  • View specific commit using git show <commit-id>

    • You can provide the first 6 alphanumeric characters of the commit ID, or you can use the entire commit ID.
root@mumbai-vm:/home/azureuser/mumbaiDir# git show  6b8533c5488c6c4def635b167f6b0c08596f60a0
commit 6b8533c5488c6c4def635b167f6b0c08596f60a0 (HEAD -> master)
Author: Pankaj <pankaj@gmail.com>
Date:   Tue Aug 12 12:49:02 2025 +0000
    First commit from Mumbai VM
diff --git a/myfile b/myfile
new file mode 100644
index 0000000..e69de29
  • Add remote repository using git remote add origin <remote-repo-url>

    • Create a GitHub repository on GitHub.

Screenshot of a GitHub repository page titled "Central-repo-git-practice." It shows options for starting with Codespaces, adding collaborators, and quick setup instructions for the repository. The repository is marked as public.

root@mumbai-vm:/home/azureuser/mumbaiDir# git remote add origin https://github.com/pan9123/git_hub_practice.git

🔍🗑Checking and Removing Remote Repository

  • Use git remote -v to check if remote repository is added.
root@mumbai-vm:/home/azureuser/mumbaiDir# git remote -v
origin  https://github.com/pan9123/git_hub_practice.git (fetch)
origin  https://github.com/pan9123/git_hub_practice.git (push)
  • Use git remote remove origin to remove the remote repository.

🔗Connecting GitHub with Local Machine

Option 1: SSH Key (Optional)

  • Generate SSH key using ssh-keygen -t ed25519 -c "roypankaj121@gmail.com".

  • View public key using cat ~/.ssh/id_ed25519.pub.

  • Go to GitHub → Profile → Settings → SSH and GPG Keys → New SSH Key → Paste the key.

Screenshot of a GitHub settings page for adding a new SSH key. The form includes fields for the key's title ("ssh-git-login"), the key type (Authentication Key), and an input area for the key itself. The left sidebar displays various account settings options.

I will choose option 2.

🔐Option 2: Personal Access Token (PAT)

  • Go to GitHub → Profile → Settings → Developer Settings → Personal Access Token → Tokens (classic) → New personal access token (classic) → Generate Token.

  • Select required checkboxes and generate token.

  • Copy and save the token securely.

  • Use this token as password when pushing code.

After generating the token, please make sure to note it down somewhere, as it will only be displayed once.

Push changes using git push -u origin master

  • Use Personal Access Token (PAT) token as password when pushing code.
root@mumbai-vm:/home/azureuser/mumbaiDir# git push origin master
Username for 'https://github.com': pan9123
Password for 'https://pan9123@github.com':
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 215 bytes | 215.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/pan9123/Central-repo-git-practice.git
 * [new branch]      master -> master
  • Code pushed successfully to GitHub account


🖥Singapore VM Workflow

  • Login to Singapore VM.

  • Set your Git username using git config --global user.name "Rohan"

azureuser@singapore-vm:~/singaporeDir$ git config --global user.name "Rohan"
azureuser@singapore-vm:~/singaporeDir$ git config --global user.email "rohan@gmail.com"
  • View Git configuration using git config --list
azureuser@singapore-vm:~/singaporeDir$ git config --list
user.name=Rohan
user.email=rohan@gmail.com
  • Create a directory and go inside it.
azureuser@singapore-vm:~$ mkdir singaporeDir
azureuser@singapore-vm:~$ ls
singaporeDir
azureuser@singapore-vm:~$ cd singaporeDir
azureuser@singapore-vm:~/singaporeDir$
  • Initialize Git using git init
azureuser@singapore-vm:~/singaporeDir$ 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:   git config --global init.defaultBranch <name>
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:   git branch -m <name>
Initialized empty Git repository in /home/azureuser/singaporeDir/.git/
  • Add remote repository using git remote add origin <repo-url>
azureuser@singapore-vm:~/singaporeDir$ git remote add origin https://github.com/pan9123/Central-repo-git-practice.git
  • Pull latest code using git pull origin master
azureuser@singapore-vm:~/singaporeDir$ git pull origin master
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), 195 bytes | 195.00 KiB/s, done.
From https://github.com/pan9123/Central-repo-git-practice
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
  • View commit history using git log and git show <commit-id>.
azureuser@singapore-vm:~/singaporeDir$ git log
commit 6b8533c5488c6c4def635b167f6b0c08596f60a0 (HEAD -> master, origin/master)
Author: Pankaj <pankaj@gmail.com>
Date:   Tue Aug 12 12:49:02 2025 +0000

    First commit from Mumbai VM
azureuser@singapore-vm:~/singaporeDir$ git show 6b8533c5488
commit 6b8533c5488c6c4def635b167f6b0c08596f60a0 (HEAD -> master, origin/master)
Author: Pankaj <pankaj@gmail.com>
Date:   Tue Aug 12 12:49:02 2025 +0000

    First commit from Mumbai VM

diff --git a/myfile b/myfile
new file mode 100644
index 0000000..e69de29
  • Add new code to the file.
azureuser@singapore-vm:~/singaporeDir$ cat >myfile
update the file from singapore VM
azureuser@singapore-vm:~/singaporeDir$ cat myfile
update the file from singapore VM
  • Check status using git status
azureuser@singapore-vm:~/singaporeDir$ git status
On branch master
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:   myfile

no changes added to commit (use "git add" and/or "git commit -a")
  • Add changes using git add .
azureuser@singapore-vm:~/singaporeDir$ git add .
  • Commit changes using git commit -m "Code updated from Singapore VM"
azureuser@singapore-vm:~/singaporeDir$ git commit -m "Code updated from Singapore VM"
[master 33db4db] Code updated from Singapore VM
 1 file changed, 1 insertion(+), 1 deletion(-)
  • Push changes using git push origin master

    • Use Personal Access Token (PAT) token as password when pushing code.
azureuser@singapore-vm:~/singaporeDir$ git push origin master
Username for 'https://github.com': pan9123
Password for 'https://pan9123@github.com':
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (6/6), 576 bytes | 576.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/pan9123/Central-repo-git-practice.git
   6b8533c..33db4db  master -> master

Code successfully updated to the GitHub repository.

Why This Is Important

Suppose you're working on a project and complete some part. Your organization wants another developer in Singapore to complete the remaining part. This setup allows seamless collaboration.


🙈Using .gitignore to Exclude Files

To ignore specific files during commit:

  • Create a hidden file named .gitignore
azureuser@singapore-vm:~/singaporeDir$ touch .gitignore
azureuser@singapore-vm:~/singaporeDir$ ls -la
total 2
drwxrwxr-x 3 azureuser azureuser 4096 Aug 12 14:26 .
-rw-rw-r-- 1 azureuser azureuser    0 Aug 12 14:26 .gitignore
  • Add patterns like *.css and *.java to ignore all CSS and Java files.
azureuser@singapore-vm:~/singaporeDir$ vi .gitignore 

// To ignore specific file extensions, add the following content to the .gitignore file:
# Ignore all  
*.css
*.java
  • Add .gitignore using git add .gitignore
azureuser@singapore-vm:~/singaporeDir$ git add .gitignore
  • Commit using git commit -m "Latest update exclude CSS and Java"
azureuser@singapore-vm:~/singaporeDir$ git commit -m "Latest update exclude CSS and Java"
[master 33d7a36] Latest update exclude CSS and Java
 1 file changed, 4 insertions(+)
 create mode 100644 .gitignore
  • Create files like file1.txt, file2.java, file3.css, file4.txt
azureuser@singapore-vm:~/singaporeDir$ touch file1.txt file2.java file3.css file4.txt
  • Use ls to list files
azureuser@singapore-vm:~/singaporeDir$ ls
file1.txt  file2.java  file3.css  file4.txt  myfile
  • Use git status to check tracked/untracked files

    • Only file1.txt and file4.txt are tracked by Git, while file3.css and file2.java are ignored.
azureuser@singapore-vm:~/singaporeDir$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file1.txt
        file4.txt

nothing added to commit but untracked files present (use "git add" to track)
  • Add and commit only text files.
azureuser@singapore-vm:~/singaporeDir$ git add .
azureuser@singapore-vm:~/singaporeDir$ git commit -m "file3.css and file2.java exclude successfully"
[master 00815c5] file3.css and file2.java exclude successfully
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1.txt
 create mode 100644 file4.txt
  • Pushing the .gitignore file to the remote repository
azureuser@singapore-vm:~/singaporeDir$ git push origin master
Username for 'https://github.com': pan9123
Password for 'https://pan9123@github.com':
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 2 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 545 bytes | 545.00 KiB/s, done.
Total 6 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/pan9123/Central-repo-git-practice.git
   33db4db..00815c5  master -> master

📜Understanding Git Log Commands

Git provides powerful commands to view the history of commits in your repository. These commands help you track changes, identify who made them, and understand the evolution of your codebase.

  • git log -1 : This command shows the latest (most recent) commit in your repository.
    It is useful when you want to quickly check the last change made, including the commit message, author, date, and commit ID.

    • Use Case:
      You just pushed a change and want to verify what was committed last.

Example Output:

azureuser@singapore-vm:~/singaporeDir$ git log -1
commit 00815c543e8d490502822aa51a549ec910c23b51 (HEAD -> master, origin/master)
Author: Rohan <rohan@gmail.com>
Date:   Tue Aug 12 14:42:53 2025 +0000

    file3.css and file2.java exclude successfully
  • git log -2 : This command displays the two most recent commits.
    It helps you compare the last two changes made to the repository.

    • Use Case:
      You want to see what was changed in the last two commits, maybe to verify a bug fix or a feature addition.

Example Output:

azureuser@singapore-vm:~/singaporeDir$ git log -2
commit 00815c543e8d490502822aa51a549ec910c23b51 (HEAD -> master, origin/master)
Author: Rohan <rohan@gmail.com>
Date:   Tue Aug 12 14:42:53 2025 +0000

    file3.css and file2.java exclude successfully

commit 33d7a364a6099213ab546db706ffba21257c611e
Author: Rohan <rohan@gmail.com>
Date:   Tue Aug 12 14:36:04 2025 +0000

    Latest update exclude CSS and Java
  • git log --oneline : This command shows the entire commit history in a compact format, where each commit is displayed in a single line.
    It includes the short commit ID and the commit message.

    • Use Case:
      You want a quick overview of all commits without detailed information like author or timestamp.

Example Output:

azureuser@singapore-vm:~/singaporeDir$ git log --oneline
00815c5 (HEAD -> master, origin/master) file3.css and file2.java exclude successfully
33d7a36 Latest update exclude CSS and Java
33db4db Code updated from Singapore VM
9574b88 Code updated from Singapore VM
6b8533c First commit from Mumbai VM
  • git log --grep "exclude" : This command filters the commit history to show only those commits whose messages contain the word "ignore".
    It is useful when you're searching for a specific change related to ignored files or .gitignore updates.

    • Use Case:
      You want to find the commit where you added or modified the .gitignore file or excluded certain file types.

Example Output:

azureuser@singapore-vm:~/singaporeDir$ git log --grep "exclude"
commit 00815c543e8d490502822aa51a549ec910c23b51 (HEAD -> master, origin/master)
Author: Rohan <rohan@gmail.com>
Date:   Tue Aug 12 14:42:53 2025 +0000

    file3.css and file2.java exclude successfully

commit 33d7a364a6099213ab546db706ffba21257c611e
Author: Rohan <rohan@gmail.com>
Date:   Tue Aug 12 14:36:04 2025 +0000

    Latest update exclude CSS and Java

🌿Branching Strategy in Git

Branching in Git allows developers to work on multiple features or fixes simultaneously without affecting the main codebase. The below diagram visualizes a repository with two isolated lines of development—one for a small feature and another for a long-running feature. This setup is essential for parallel development and maintaining a clean, error-free master branch.

🔑Key Concepts of Git Branching

  • Each task or feature should have its own sub-branch.

  • Once development is complete, the code is merged into the master branch.

  • This strategy supports parallel development across teams.

  • You can create any number of sub-branches or even sub-sub-branches.

  • Changes made in a branch are isolated and personal to that branch.

  • The default branch in Git is usually named master.

  • Files created in your workspace are visible across branches until you commit them. Once committed, the files belong to the specific branch where the commit was made.

  • When you create a new branch, it copies the data from the branch it was created from.

💻Real-Life Analogy: Laptop and Windows Versions

Imagine your laptop is running Windows 10. One day, your friend wants to test Windows 16. You can't risk your main laptop, which contains all your work and personal files. So, you give your friend an extra laptop for testing.

If the test fails and the system crashes, your main laptop remains safe. But if the test succeeds, you can decide to install Windows 16 on your main laptop later.

This is exactly how Git branching works:

  • You create a sub-branch to experiment with new features.

  • If the experiment works, you merge it into the master branch.

  • If it fails, the master branch remains unaffected.

🔧Branch Creation and Data Flow in Git

In Git, when you create a new branch, it inherits all the data from the branch it was created from. This allows the new branch to work independently while maintaining the base structure and code of the original branch.

Diagram showing a linear sequence labeled A1, A2, and A3 forming the master branch, with a branch off to B1 labeled as the sub-branch.

📌Example Explained

Let’s say you have a branch named A2, and from this branch, you create a new sub-branch called B1.

  • The B1 branch will copy all the data from A2 at the time of creation.

  • Any changes or commits made in B1 will be isolated to B1 only.

  • These changes will not be visible in A1, A2, or A3 unless you explicitly merge B1 into one of those branches.

Once the development in B1 is complete and approved, you can merge it into the master branch.
This ensures that only tested and verified code reaches the main branch, keeping it stable and clean.

Best Practice: Work in Sub-Branches

It is always recommended to work in sub-branches rather than directly in the master branch.
This approach allows:

  • Safe experimentation

  • Isolated development

  • Easy rollback if something goes wrong

Once the code is developed and tested in the sub-branch, it can be merged into the master branch confidently.

🔀Parallel Development Strategy

In large-scale projects, different teams often work on different features simultaneously. Each team creates its own branch to work independently.

Real-World Example: Facebook Development

Imagine the development of Facebook involves multiple teams:

  • Login page team

  • Signup page team

  • UI team

  • Backend team

Each team works in its own branch:

  • login-feature

  • signup-feature

  • ui-enhancements

  • backend-updates

Once all features are completed and tested, they are merged into the master branch.
This strategy ensures that:

  • Teams don’t interfere with each other’s work

  • Code integration is smooth

  • The master branch remains stable

🧑‍💻Git Branching Commands and Practice Guide

Branching is a fundamental concept in Git that allows developers to work on different features or fixes independently. Below are the essential commands and a practical example to help you understand how branching works.

Git Branching Commands

  • git branch : This command shows the list of all available branches in your repository. It helps you see which branches exist and which one you're currently on.

    * → This symbol indicates the pointer to the master branch.

azureuser@singapore-vm:~/singaporeDir$ git branch
* master
  • git branch <branch-name> : This command creates a new branch with the name you specify. It copies the current branch's data into the new branch, allowing you to work independently.
azureuser@singapore-vm:~/singaporeDir$ git branch newBranch
azureuser@singapore-vm:~/singaporeDir$ git branch
* master
  newBranch
  • git checkout <branch-name> : This command switches your working directory to the specified branch. It updates your files to match the state of that branch.
azureuser@singapore-vm:~/singaporeDir$ git checkout newBranch
Switched to branch 'newBranch'

* → This symbol shows the pointer to the newly created branch 'newBranch'.

azureuser@singapore-vm:~/singaporeDir$ git branch
  master
* newBranch
  • git branch -d <branch-name> : This command deletes a branch if its work is completed and merged. The -d stands for delete. Use this when you're sure the branch has been merged or is no longer needed

Output Error:

azureuser@singapore-vm:~/singaporeDir$ git branch -d newBranch
error: Cannot delete branch 'newBranch' checked out at '/home/azureuser/singaporeDir'

Note: First, we need to switch to a different branch (e.g., master) before we can delete the branch.

azureuser@singapore-vm:~/singaporeDir$ git checkout master
Switched to branch 'master'
azureuser@singapore-vm:~/singaporeDir$ git branch
* master
  newBranch

//Deleting the branch
azureuser@singapore-vm:~/singaporeDir$ git branch -d newBranch
Deleted branch newBranch (was 00815c5).
azureuser@singapore-vm:~/singaporeDir$ git branch
* master
  • git branch -D <branch-name> : This command forcefully deletes a branch, even if it hasn’t been merged. Use this with caution. Use this with caution — you might lose work that hasn’t been merged.
azureuser@singapore-vm:~/singaporeDir$ git branch branch1
azureuser@singapore-vm:~/singaporeDir$ git branch
  branch1
* master
azureuser@singapore-vm:~/singaporeDir$ git branch -D branch1
Deleted branch branch1 (was 00815c5).

📝Practice Steps for Branching

Let’s walk through a practical example using a directory named mumbaiDir

  • Switch to root user using sudo su to ensure you have full access

  • Navigate to your Git project folder using cd mumbaiDir

root@mumbai-vm:/home/azureuser# cd mumbaiDir
root@mumbai-vm:/home/azureuser/mumbaiDir#
  • View commit history using git log --oneline to see previous commits.

    • The latest code has not yet been pulled from the central repository.
root@mumbai-vm:/home/azureuser/mumbaiDir# git log --oneline
6b8533c (HEAD -> master, origin/master) First commit from Mumbai VM
  • Check existing branches using git branch
root@mumbai-vm:/home/azureuser/mumbaiDir# git branch
* master
  • Create a new branch named branch1 using git branch branch1
root@mumbai-vm:/home/azureuser/mumbaiDir# git branch branch1
  • Verify branch creation again using git branch
root@mumbai-vm:/home/azureuser/mumbaiDir# git branch
  branch1
* master
  • Switch to the new branch using git checkout branch1
root@mumbai-vm:/home/azureuser/mumbaiDir# git checkout branch1
Switched to branch 'branch1'
  • Create a new file named filebranch1 using touch filebranch1
root@mumbai-vm:/home/azureuser/mumbaiDir# touch filebranch1
  • Switch back to master branch using git checkout master
root@mumbai-vm:/home/azureuser/mumbaiDir# git checkout master
Switched to branch 'master'
  • List files using ls
    Note: You will still see filebranch1 in all branches because it hasn't been committed yet.
root@mumbai-vm:/home/azureuser/mumbaiDir# ls
filebranch1  myfile
  • Switch back to branch1 using git checkout branch1
root@mumbai-vm:/home/azureuser/mumbaiDir# git checkout branch1
Switched to branch 'branch1'
  • Stage the file using git add . to prepare it for commit
root@mumbai-vm:/home/azureuser/mumbaiDir# git add .
  • Commit the file using git commit -m "First commit from branch1"
root@mumbai-vm:/home/azureuser/mumbaiDir# git commit -m "First commit from branch1"
[branch1 f30f7aa] First commit from branch1
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 filebranch1
  • Switch to master again using git checkout master

  • List files using ls.
    Note: Now filebranch1 will not be visible because it was committed to branch1, not to master.

root@mumbai-vm:/home/azureuser/mumbaiDir# git checkout master
Switched to branch 'master'
root@mumbai-vm:/home/azureuser/mumbaiDir# ls
myfile

📚Understanding Git Merge and Repository Rules

Merging in Git is the process of combining changes from one branch into another. It’s commonly used when a feature branch is ready to be integrated into the main branch (usually master).

Important Rule: Same Repository Only

You cannot merge branches from different repositories.
For example, if you have a feature1 branch in your Mumbai VM and a feature2 branch in your Singapore VM, these branches belong to different repositories and cannot be merged directly.

However, you can merge branches within the same repository. This is the standard practice in collaborative development.

How Git Merge Works

To merge a branch, you use the pulling mechanism.
The command used is:

  • git merge <branch-name> : This command merges the specified branch into your current branch.

    • Current branch → master

    • Specified branch → branch1

root@mumbai-vm:/home/azureuser/mumbaiDir# git branch
  branch1
* master

// Now merge the code from branch1 into master

root@mumbai-vm:/home/azureuser/mumbaiDir# git merge branch1
Updating 6b8533c..f30f7aa
Fast-forward
 filebranch1 | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 filebranch1

Note: Only the new code changes are merged, not the entire branch data.
Think of it like copy + paste, not cut + paste. The original branch remains intact.

🔍Verifying the Merge

After merging, you can use:

  • git log --oneline
root@mumbai-vm:/home/azureuser/mumbaiDir# git log --oneline
f30f7aa (HEAD -> master, branch1) First commit from branch1
6b8533c (origin/master) First commit from Mumbai VM

ℹ️ GitHub Background Info

GitHub is a cloud-based storage platform for Git repositories.
It was originally known as “Logical Awesome”, and its logo is a cat with tentacles (called the Octocat).
Interestingly, the same designer also created the Twitter bird icon.

GitHub was acquired by Microsoft for $7.5 billion, which led to the rise of GitLab—an alternative platform that offers more open-source and self-hosted options.


⚠️Git Merge Conflicts

A Git conflict occurs when Git is unable to automatically merge changes between two branches. This usually happens when the same file is modified differently in both branches.

When Do Conflicts Occur

Conflicts typically happen during a merge operation.
For example, if you try to merge branch1 into master, and both branches have changes in the same file, Git will not know which version to keep.

Why Do Conflicts Occur

Let’s understand with an example:

  • In the master branch, you created a file named file1 and wrote:
    "My name is Pankaj"
root@mumbai-vm:/home/azureuser/mumbaiDir# git branch
  branch1
* master
root@mumbai-vm:/home/azureuser/mumbaiDir# cat > file1
My name is Pankaj
root@mumbai-vm:/home/azureuser/mumbaiDir# cat file1
My name is Pankaj
root@mumbai-vm:/home/azureuser/mumbaiDir# git add .
root@mumbai-vm:/home/azureuser/mumbaiDir# git commit -m "for git merge testing"
[master 0aae0d3] for git merge testing
 1 file changed, 1 insertion(+)
 create mode 100644 file1
  • In branch1, you also created file1 but wrote:
    "My name is Rohan"
root@mumbai-vm:/home/azureuser/mumbaiDir# git branch
* branch1
  master
root@mumbai-vm:/home/azureuser/mumbaiDir# touch file1
root@mumbai-vm:/home/azureuser/mumbaiDir# cat >file1
My name is Rohan
root@mumbai-vm:/home/azureuser/mumbaiDir# ls
file1  filebranch1  myfile
root@mumbai-vm:/home/azureuser/mumbaiDir# cat file1
My name is Rohan
root@mumbai-vm:/home/azureuser/mumbaiDir# git add .
root@mumbai-vm:/home/azureuser/mumbaiDir# git commit -m"for git merge testing from branch1"
[branch1 6563e70] for git merge testing from branch1
 1 file changed, 1 insertion(+)
 create mode 100644 file1

Since both files have the same name but different content, Git gets confused and cannot decide which version to keep.
This results in a merge conflict.

Let's merge the code into the master branch.

root@mumbai-vm:/home/azureuser/mumbaiDir# git merge branch1
Auto-merging file1
CONFLICT (add/add): Merge conflict in file1
Automatic merge failed; fix conflicts and then commit the result.

How to Resolve Git Conflicts

When a conflict occurs, Git will mark the conflicting sections in the file like this:

Screenshot of a text editor displaying a Git merge conflict. The conflict markers indicate a difference between two branches: "HEAD" with the line "My name is Pankaj" and "branch1" with the line "My name is Rohan."

To resolve the conflict:

  • Open the file manually using a text editor like vi file1.

  • Delete the conflict markers:

    • <<<<<<< HEAD

    • =======

    • >>>>>>> branch1

  • Arrange the content as needed

  • Save the file.

  • Stage the resolved file using git add file1

root@mumbai-vm:/home/azureuser/mumbaiDir# git add file1
  • Commit the changes using git commit -m "Resolved merge conflict in file1"
root@mumbai-vm:/home/azureuser/mumbaiDir# git commit -m "Resolved merge conflict in file1"
[master 0ece4f9] Resolved merge conflict in file1

Git will then assume the conflict is resolved and continue the merge process.

Example Output:

root@mumbai-vm:/home/azureuser/mumbaiDir# cat file1
My name is Pankaj
My name is Rohan

Note: You must commit the changes; otherwise, you won't be able to switch to another branch.

root@mumbai-vm:/home/azureuser/mumbaiDir# git switch branch1
error: Your local changes to the following files would be overwritten by checkout:
        file1
Please commit your changes or stash them before you switch branches.
Aborting

Committing the changes from the merge code

root@mumbai-vm:/home/azureuser/mumbaiDir# git add .
root@mumbai-vm:/home/azureuser/mumbaiDir# git commit -m "merge conflict resolved"

Note: While pushing the code, I encountered an error. Below, I have outlined how I resolved the issue.

🔍 Scenario Overview

Imagine you're working on a feature in the master branch. You've made some local commits and are ready to push your changes. But—another developer has already pushed new updates to the remote master branch.

If you try to push without syncing first, Git will reject your push.

root@mumbai-vm:/home/azureuser/mumbaiDir# git push origin master
Username for 'https://github.com': pan9123
Password for 'https://pan9123@github.com':
To https://github.com/pan9123/Central-repo-git-practice.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/pan9123/Central-repo-git-practice.git'

So, what’s the cleanest way to handle this?

root@mumbai-vm:/home/azureuser/mumbaiDir# git pull --rebase origin master

What Does This Command Do❓

  • git pull: Fetches changes from the remote repository.

  • --rebase: Replays your local commits on top of the latest remote commits.

  • origin main: Specifies the remote (origin) and branch (main) you're syncing with.

Why Use Rebase Instead of Merge

  • Keeps your commit history linear and clean.

  • Avoids unnecessary merge commits.

  • Makes collaboration smoother, especially in CI/CD pipelines.

azureuser@mumbai-vm:~/mumbaiDir$ git push origin master
Username for 'https://github.com': pan9123
Password for 'https://pan9123@github.com':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 364 bytes | 364.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/pan9123/Central-repo-git-practice.git
   416db76..5cf7cfb  master -> master

When Conflicts Do Not Occur

If the content is partially the same, Git can intelligently merge the changes.

Example:

  • In the master branch, file1 contains:
    My name is Pankaj

  • In branch1, file1 contains:
    My name is Pankaj

    I am a student

In this case, Git sees that the beginning of the content is the same and assumes the additional part is new.
So, it merges the content without any conflict.

// switching to branch1
azureuser@mumbai-vm:~/mumbaiDir$ git checkout branch1
Switched to branch 'branch1'
azureuser@mumbai-vm:~/mumbaiDir$ vi file4.txt
azureuser@mumbai-vm:~/mumbaiDir$ git add .
azureuser@mumbai-vm:~/mumbaiDir$ git commit -m " Added content into the  file4 2nd time from branch1"
[branch1 f4bf05e]  Added content into the  file4 2nd time from branch1
 1 file changed, 1 insertion(+)

//switching to master branch and merge the changes from branch1
azureuser@mumbai-vm:~/mumbaiDir$ git checkout master
Switched to branch 'master'
azureuser@mumbai-vm:~/mumbaiDir$ git merge branch1
Updating 929b70f..f4bf05e
Fast-forward
 file4.txt | 1 +
 1 file changed, 1 insertion(+)

📥Git Stashing

Git stashing is a powerful feature that allows you to temporarily save your work-in-progress changes without committing them. This is especially useful when you're working on a new feature and suddenly need to switch tasks—like handling a customer escalation or fixing a critical bug.

Diagram illustrating a Git workflow within a virtual machine. It shows the working directory, staging area, and local repository, with an arrow indicating git stashing to a separate stash store.

Why Use Git Stash

Imagine you're developing a new feature, and your code is halfway done. Suddenly, a customer issue comes up that needs immediate attention. You can't commit your partial code because it's incomplete, and you can't push it either. In such cases, Git stash acts like a temporary storage where you can safely park your changes and come back to them later.

🗒️Important Notes

  • Git stash only works on modified files, not newly created files that haven’t been tracked yet.

  • You can stash multiple files at once.

  • Once your urgent task is done, you can restore your stashed changes and continue working.

🛠️Best Practice for Stashing

Before starting your day:

  1. Add and commit your clean workspace.

  2. Begin writing your feature code (e.g., xyz.txt).

  3. If an escalation comes, stash xyz.txt.

  4. Work on the escalated issue.

  5. Once resolved, apply the stash and continue your feature development.

This ensures your workspace remains organized and recoverable.

📦Git Stash Commands Explained

  • git stash : This command stashes your current modified files. It temporarily removes them from your working directory and stores them safely.

    • Note: Please move the file to the staging area before you stash it.
azureuser@mumbai-vm:~/mumbaiDir$ cat > xyz.txt
Stash Checking
azureuser@mumbai-vm:~/mumbaiDir$ cat xyz.txt
Stash Checking

/stashing the file
azureuser@mumbai-vm:~/mumbaiDir$ git stash
Saved working directory and index state WIP on master: f4bf05e  Added content into the  file4 2nd time from branch1
  • git stash list : Shows the list of all stashed items. Each stash is indexed like stash@{0}, stash@{1}, etc.
azureuser@mumbai-vm:~/mumbaiDir$ git stash list
stash@{0}: WIP on master: f4bf05e Added content into the file4 2nd time from branch1
  • git stash apply stash@{0} : Applies the stash with index 0 back to your working directory. You can continue working from where you left off.
azureuser@mumbai-vm:~/mumbaiDir$ git stash apply stash@{0}
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   xyz.txt
  • git stash clear : Clears all stashed items after moving the file back to the working directory. The stash item will still be there. Use this command when you're sure you no longer need the stashed changes.
azureuser@mumbai-vm:~/mumbaiDir$ git stash clear
//no output after clear the stash
azureuser@mumbai-vm:~/mumbaiDir$ git stash list

🧪Practice Example

  • Create a file named demo using touch demo.
azureuser@mumbai-vm:~/mumbaiDir$ touch demo
  • Stage and commit it using git add . and git commit -m "First file stashing".
azureuser@mumbai-vm:~/mumbaiDir$ git add .
azureuser@mumbai-vm:~/mumbaiDir$ git commit -m "for stash practical"
[master ff6896d] for stash practical
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 demo
  • Open the file using vi demo and write some code (e.g., "First code")

    • Add the "second code"
azureuser@mumbai-vm:~/mumbaiDir$ vi demo
azureuser@mumbai-vm:~/mumbaiDir$ cat demo
First code
Second code
  • Stage and commit again using git add .
azureuser@mumbai-vm:~/mumbaiDir$ git add demo
  • Run git stash to stash the changes.
azureuser@mumbai-vm:~/mumbaiDir$ git stash
Saved working directory and index state WIP on master: fece901 Git stash demo
  • Use cat demo to check the file—your code will not show because it has been moved to stash

    • Note that I committed the "First code," which is why it appears. The "Second code" does not show because it has been stashed.
azureuser@mumbai-vm:~/mumbaiDir$ cat demo
First code
  • Modified the changes in the demo file.
azureuser@mumbai-vm:~/mumbaiDir$ vi demo
azureuser@mumbai-vm:~/mumbaiDir$ cat demo
First code
Third code
  • Stashing the "Third code"
azureuser@mumbai-vm:~/mumbaiDir$ git add .
azureuser@mumbai-vm:~/mumbaiDir$ git stash
Saved working directory and index state WIP on master: fece901 Git stash demo
  • Let's see how many stash items there are.

    🧠 What fece901 Represents

    • fece901 is the commit hash of the HEAD of the master branch at the time you created the stash.

    • It means: "This stash was made while I was working on top of commit fece901."

azureuser@mumbai-vm:~/mumbaiDir$ git stash list
stash@{0}: WIP on master: fece901 Git stash demo
stash@{1}: WIP on master: fece901 Git stash demo
  • If you stash two different sets of changes and apply the second one, a merge conflict may occur.
azureuser@mumbai-vm:~/mumbaiDir$ git stash apply stash@{1}
On branch master
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:   demo

no changes added to commit (use "git add" and/or "git commit -a")
  • To resolve the conflict, open the file using vi demo, delete the conflict markers, arrange the content, and commit the resolved version.

  • Finally, run git stash clear to remove all stash entries.

azureuser@mumbai-vm:~/mumbaiDir$ git stash clear
azureuser@mumbai-vm:~/mumbaiDir$ git stash list

💡Understanding Git Reset, Git Revert, Removing Untracked Files, Tags, and GitHub Clone

Git provides several commands to manage your repository's state. These commands help you undo changes, clean up your workspace, label versions, and clone repositories. Let’s understand each one in detail.

⏪Git Reset

git reset is a powerful command used to undo local changes in your Git repository. It works in your private area, meaning it affects only your local machine and not the central repository.

  • git reset <filename> : This command removes the specified file from the staging area. It’s the opposite of git add.

  • Example:
    git reset . : This resets all files from the staging area. It’s useful when you’ve added files by mistake and want to unstage them.

    • adding content to the demo file
    azureuser@mumbai-vm:~/mumbaiDir$ cat demo
    First code
    Third code
    git reset
  • adding the demo file to the staging area
    azureuser@mumbai-vm:~/mumbaiDir$ git add .
    azureuser@mumbai-vm:~/mumbaiDir$ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            modified:   demo
  • moving the file from the staging area back to the working area
    azureuser@mumbai-vm:~/mumbaiDir$ git reset .
    Unstaged changes after reset:
    demo
    azureuser@mumbai-vm:~/mumbaiDir$ git status
    On branch master
    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:   demo
  • git reset --hard : This command resets changes from both the staging area and the working directory. Use it with caution, as it will discard all local changes permanently.
azureuser@mumbai-vm:~/mumbaiDir$ git add demo
azureuser@mumbai-vm:~/mumbaiDir$ git reset --hard
HEAD is now at ce8f645 git stash apply demo
azureuser@mumbai-vm:~/mumbaiDir$ git status
On branch master
nothing to commit, working tree clean

↩️Git Revert

git revert is used to undo a commit that has already been made. Unlike reset, it works in the public area, meaning it creates a new commit that reverses the changes of a previous commit.

  • It does not delete any data. Instead, it creates a new commit that restores the files to their previous state.

  • This keeps your version history intact and moving forward, while the file content moves backward.

⚖️Reset vs Revert

  • Reset: Used before commit, affects local/staging area.

  • Revert: Used after commit, affects central repository and creates a new commit.

Practice Example for Git Revert

  • Switch to root using sudo su

  • Navigate to your Git project folder using cd mumbaigit

  • Check files using ls

  • View repository status using git status

  • Create a new file using cat > newfile and write:
    "Hi, final code for app"

azureuser@mumbai-vm:~/mumbaiDir$ cat > newfile
Hi, final code for app
azureuser@mumbai-vm:~/mumbaiDir$
azureuser@mumbai-vm:~/mumbaiDir$ cat newfile
Hi, final code for app
  • Stage and commit the file using git add . and git commit -m "git revert demo"
azureuser@mumbai-vm:~/mumbaiDir$ git add .
azureuser@mumbai-vm:~/mumbaiDir$ git commit -m "git revert demo"
[master 409ca7a] git revert demo
 1 file changed, 1 insertion(+)
 create mode 100644 newfile
  • View commit history using git log --oneline
azureuser@mumbai-vm:~/mumbaiDir$ git log --oneline
409ca7a (HEAD -> master) git revert demo
  • Revert the commit using git revert <commit-id>

    Note:

    • When you run git revert, Git will open the vim editor to enter a commit message for the revert.

    • After reverting, a new commit ID will be generated.

Screenshot of a terminal displaying a GNU nano editor window. The message at the top reads "Reverting the demo commit due to incorrect file changes." Below, it shows details about a git revert command for a specific commit.

azureuser@mumbai-vm:~/mumbaiDir$ git revert 409ca7a
[master a68212b] Reverting the demo commit due to incorrect file changes
 1 file changed, 1 deletion(-)
 delete mode 100644 newfile

🗑️Removing Untracked Files

Sometimes your working directory may contain files that are not tracked by Git. You can remove them using the git clean command.

  • git clean -n : This is a ‘n → dry run’. It shows which untracked files will be deleted, giving you a warning before actual deletion.
azureuser@mumbai-vm:~/mumbaiDir$ cat > demoUntrack
for Removing Untracked Files demo

azureuser@mumbai-vm:~/mumbaiDir$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        demoUntrack

// giving warning
azureuser@mumbai-vm:~/mumbaiDir$ git clean -n
Would remove demoUntrack
  • git clean -f
    This deletes the untracked files forcefully without any warning.
azureuser@mumbai-vm:~/mumbaiDir$ git clean -f
Removing demoUntrack

Best Practice:
Always run git clean -n first to review the files, then use git clean -f if you're sure.

🏷️Git Tags

Git tags allow you to assign a meaningful name to a specific version of your code.
Think of it like labeling spice jars in your kitchen—you write the name on each jar to identify the spice inside. Similarly, in Git, you use tags to label important versions of your code, such as releases or milestones.

Why Use Tags

  • To mark stable versions of your project

  • To identify release points like v1.0, v2.0, etc.

  • To simplify rollback or reference to specific commits

Common Tag Commands

  • To create a tag:
    Use git tag -a <tagname> -m <message> <commit-id> : This creates an annotated tag with a message on a specific commit.
azureuser@mumbai-vm:~/mumbaiDir$ git tag -a tagFile4 -m "to track the file4 changes" 929b70f
//result
azureuser@mumbai-vm:~/mumbaiDir$  git log --oneline
929b70f (tag: tagFile4)  Added content into the  file4 2nd time
  • To list all tags:
    Use git tag : This shows all tags created in the repository.
azureuser@mumbai-vm:~/mumbaiDir$ git tag
tagFile4
  • To view tag details:
    Use git show <tagname> : This displays the commit and message associated with the tag.
azureuser@mumbai-vm:~/mumbaiDir$ git show tagFile4
tag tagFile4
Tagger: Pankaj <pankaj@gmail.com>
Date:   Wed Aug 13 11:18:35 2025 +0000
to track the file4 changes
commit 929b70fd92c1b7f4c7193cd287b777f0a832dc73 (tag: tagFile4)
-------
  • To delete a tag:
    Use git tag -d <tagname> : This removes the tag from your local repository
azureuser@mumbai-vm:~/mumbaiDir$ git tag -d tagFile4
Deleted tag 'tagFile4' (was 7059034)

🔀Git Rebase: Streamlining Commit History

Git rebase is used to move or combine a sequence of commits to a new base commit.
It helps keep your commit history clean and linear, especially when working with feature branches.

Why Use Rebase

  • To avoid unnecessary merge commits

  • To make history easier to read

  • To integrate changes from one branch into another smoothly

Example Scenario

Suppose you're working on a feature branch and the master branch has received new updates.
Instead of merging master into your feature branch, you can rebase your branch onto master.

How Rebase Works

  • First, switch to your feature branch using git checkout <banch>

  • Then run git rebase master : This will replay your feature branch commits on top of the latest master branch

azureuser@mumbai-vm:~/mumbaiDir$ git rebase master
Successfully rebased and updated refs/heads/branch1.
azureuser@mumbai-vm:~/mumbaiDir$ ls
demo  file1.txt  file4.txt  myfile  xyz.txt

⚔️Merge vs Rebase

  • Merge: Keeps all history and creates a merge commit

  • Rebase: Rewrites history and avoids merge commits

Note: Rebase is best used before pushing your branch to a shared repository.

🧬GitHub Clone: Copying Repositories Locally

Diagram illustrating a virtual machine containing a working directory, staging area, and central repository. An arrow labeled "Git clone" points from an external central repository, such as GitHub, to the virtual machine's central repository.

Cloning is the process of copying a GitHub repository to your local machine.
It’s the easiest way to start working on an existing project.

Steps to Clone a Repository

  1. Open GitHub, log in, and navigate to the repository you want to clone.

  2. Click on the Code button and copy the repository URL.

  3. On your Linux machine, open the terminal and run:
    git clone <URL of GitHub repo>

azureuser@mumbai-vm:~$ git clone https://github.com/pan9123/Central-repo-git-practice.git
Cloning into 'Central-repo-git-practice'...
remote: Enumerating objects: 53, done.
------

What Happens After Cloning

  • A new folder is created with the same name as the GitHub repository.
azureuser@mumbai-vm:~$ ls
Central-repo-git-practice
  • The folder contains all files, branches, and commit history.
azureuser@mumbai-vm:~/Central-repo-git-practice$ ls
demo  file1.txt  file4.txt  myfile  xyz.txt

// commit history
azureuser@mumbai-vm:~/Central-repo-git-practice$ git log --oneline
a68212b (HEAD -> master, origin/master, origin/HEAD) Reverting the demo commit due to incorrect file changes
409ca7a git revert demo
ce8f645 git stash apply demo
fece901 Git stash demo
ff6896d for stash practical
----
  • You don’t need to manually create a directory or initialize Git—it’s all done automatically.

This is a quick and efficient way to start working on any GitHub-hosted project.

⚔️Fork vs Clone

Clone

Copies a repository to your local machine using git clone <repo-url>.

Fork

Creates a copy of someone else's repository under your GitHub account. Useful for open-source contributions.

  • The original owner's name will be visible.

🍒Git Cherry-Pick: Selective Commit Transfer

Why is it useful

  • You want only one or a few commits from a feature branch, not the whole branch.

  • Useful for hotfixes: Apply a bug fix from develop to main without merging all in-progress features.

  • Helps maintain clean history by avoiding unnecessary merges.

  • Use git cherry-pick <commit-id> to apply a specific commit from one branch to another

//Creating first file and commiting
azureuser@mumbai-vm:~/mumbaiDir$ touch fileCP
azureuser@mumbai-vm:~/mumbaiDir$ git add .
azureuser@mumbai-vm:~/mumbaiDir$ git commit -m " cherry-pick demo fileCP"

//Creating second file and commiting
azureuser@mumbai-vm:~/mumbaiDir$ touch fileCP2
azureuser@mumbai-vm:~/mumbaiDir$ git add .
azureuser@mumbai-vm:~/mumbaiDir$ git commit -m " cherry-pick demo fileCP2"

// git log
azureuser@mumbai-vm:~/mumbaiDir$ git log --oneline
ca6f9b8 (HEAD -> branch1)  cherry-pick demo fileCP2
0a69f06  cherry-pick demo fileCP
  • Switch to the master branch and merge only fileCP2.
azureuser@mumbai-vm:~/mumbaiDir$ git cherry-pick ca6f9b8
[master d072e10]  cherry-pick demo fileCP2
 Date: Wed Aug 13 11:44:16 2025 +0000
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 fileCP2

// Only the fileCP2 is added 
azureuser@mumbai-vm:~/mumbaiDir$ ls
demo  file1.txt  file4.txt  fileCP2  myfile  xyz.txt

🪝Git Hooks: Automate Git Tasks

Hooks are scripts that run during Git events.

Examples:

  • pre-commit: Runs before a commit is created.
    Use case: Run tests, lint code, or format files.

  • commit-msg: Runs after you enter a commit message.
    Use case: Validate commit message format.

  • post-commit: Runs after a commit is made.
    Use case: Send notifications or update logs.

  • pre-push: Runs before pushing to remote.
    Use case: Run CI checks or prevent pushing broken code

Hooks are stored in .git/hooks/.

🗄️Git Archive: Export Repository as Zip

  • Use git archive -o latest.zip HEAD to create a zip file of the latest commit.

  • git archive → Command to create an archive of files from a specific commit, branch, or tag.

  • -o latest.zip → Output file name will be latest.zip.

  • HEAD → Refers to the current commit you are on.

So, this will package all tracked files from the current commit into latest.zip, without including:

In simple terms, it will package all the tracked files from the commit.

azureuser@mumbai-vm:~/mumbaiDir$ ls
 latest.zip

✍️Best Practices for Git Commit Messages

Use clear and descriptive messages.

Format

  • type(scope): message

Common Types:

  • feat: New feature

  • fix: Bug fix

  • docs: Documentation

  • style: Code formatting

  • refactor: Code restructuring

Example: feat(login): add user authentication


🖱️GitHub GUI Operations

You can perform most Git operations directly from GitHub’s web interface:

  • Create repositories

    • Navigate to the repository → Click on New button

Screenshot of GitHub's "Create a new repository" page. The repository name is set to "git-playground" and is available. The description mentions "creating repository from GitHub GUI". The visibility is set to public, and options for adding README, .gitignore, and license are shown.

  • Create and switch branches

    • Click on "Create New Branch" (replace "New Branch" with the branch name) from the main branch.

  • Create a File in a New Branch

    • Click on the ‘add file’ button, name the file (e.g., file1), and then click on 'Commit changes'.

  • Merge branches

    • Go to the main branch and click on Compare & pull request. After that, click on Create pull request.

Screenshot of a GitHub repository named "git-playground" with a notification indicating recent pushes to "NewBranch". A green button labeled "Compare & pull request" is highlighted. The main branch has one commit with a file named "README.md" and the initial commit message is "Initial commit".

A GitHub interface showing a message that there are no conflicts with the base branch, and a green button for merging the pull request.

  • Delete branches

    • Click on "Delete branch."

Notification message indicating that a pull request has been successfully merged and closed, with text stating the branch has been merged.

  • Revert commits

    • commits the changes

Screenshot of a GitHub interface showing a file named "file1" in the "git-playground" repository. The file contains two lines of text: "Hi creating file from the GITHUB GUI" and "This line will be reverted." There are buttons for "Cancel changes" and "Commit changes" on the right.

  • Edit the file again and delete the line you want to revert.

  • View commit history

    • Click on "Commits" in the lower right side to view the commit history.

Screenshot of a GitHub repository named "git-playground," showing it is public. It has one branch and zero tags. There is a recent commit titled "Update file1 is reverted" by the user "pan9123," and the repository has a total of five commits.


🧠 Final Thoughts

Git is more than just a version control system—it's a collaboration tool, a safety net, and a productivity booster. Whether you're working solo or in a team across regions, mastering Git will make your DevOps journey smoother and more efficient.

---

- Written by Pankaj Roy | DevOps & Cloud Enthusiast

0
Subscribe to my newsletter

Read articles from Pankaj Roy directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pankaj  Roy
Pankaj Roy