Journey with Git and GitHub

Cloud Engineer | AWS Community Builder
Search for a command to run...

Cloud Engineer | AWS Community Builder
No comments yet. Be the first to comment.
You have a working Amazon EKS cluster and deployed pods. Now, your pods need secure, fine-grained access to AWS services like S3, DynamoDB, or Secrets Manager, but: You don't want to hardcode AWS cre

As your team and projects grow, managing a single EKS cluster can get complicated. You'll likely have different teams like a "Backend Team" and a "Frontend Team" all needing to deploy their applicatio

Serverless computing with AWS Lambda is one of the most powerful ways to run code in the cloud without managing servers. But when a Lambda function is placed inside a VPC (Virtual Private Cloud) to co

Modern developers want fast CI/CD but without storing long-lived AWS keys.By combining Amazon ECS (Fargate), Amazon ECR, GitHub Actions, and OpenID Connect (OIDC) you can deploy securely and automatic

Learn Bash Scripting: A Beginner's Guide Table of Contents Introduction Bash Scripting Basics Variables and User Input Conditional Statements Loops and Iterations Functions Working with Files Scripting Best Practices Conclusion 1. Introdu...

As a DevOps engineer, managing code effectively and ensuring smooth integration, testing, and deployment processes are essential responsibilities. Git and GitHub are the foundation for these workflows, providing the tools necessary for seamless collaboration, automation, and deployment. In this blog, we'll explore a DevOps engineer's journey with Git and GitHub, from creating repositories to deploying code in a production environment.
Q. What is Git?
Git is a distributed version control system that allows developers to track changes, collaborate on code, and manage multiple versions of a project efficiently.
Q. What is a Version Control System (VCS)?
A Version Control System (VCS) is a software tool that helps developers manage changes to source code or other files over time. It enables collaboration, tracks changes, and provides a history of modifications, allowing teams to work on projects efficiently while minimizing conflicts.
Installing Git on Windows
Visit the official Git website and download the latest version for Windows.
Choose the appropriate version for your system (32-bit or 64-bit).
Open the downloaded installer and follow the setup wizard.
Choose your preferred editor for Git (default is Vim, but you can select Notepad++ or VS Code).
Open Command Prompt or PowerShell.
Type the following command to verify the installation:
git --version
You should see the installed Git version.
Set your name and email:
git config --global user.name "Your Name"
git config --global user.email "hello@anupkafle.com.np" // Enter your Email
Installing Git on Linux
Before installing Git, ensure your system is up-to-date.
sudo apt update && sudo apt upgrade
The installation commands vary based on your Linux distribution:
For Ubuntu/Debian:
sudo apt install git
For Fedora/RHEL:
sudo dnf install git
For Arch Linux:
sudo pacman -S git
Check if Git is installed and its version:
git --version
Similar to Windows, set your global username and email:
git config --global user.name "Your Name"
git config --global user.email "hello@anupkafle.com.np" // Enter your Email
After configuring your name and email in Git, it’s important to verify the settings to ensure everything is set up correctly. These details are crucial as they are attached to every commit you make and help identify who made the changes.
Run the following command to view your global Git configuration, including your name and email:
git config --global --list
You should see output similar to this:
msi@anup:~$ git config --global --list
user.name=anupkafle
user.email=hello@anupkafle.com.np
If you want to check the configuration for a specific repository, navigate to the repository folder and use:
git config --list
This will show both global and repository-specific configurations. If you’ve overridden the global configuration for the repository, the repository-specific settings will appear here.
git init
Creates a new Git repository in the current directory by initializing a .git folder.
mkdir my-project
cd my-project
git init
This initializes an empty Git repository in the my-project directory.
git clone <repository-url>
Copies a repository (and its history) from a remote location to your local machine.
git clone https://github.com/anupkafle/testrepo.git
This clones the repository at https://github.com/anupkafle/testrepo.git into a local folder named anupkafle.
git status
Shows the status of your working directory and staging area, including:
Files modified but not staged.
Files staged but not committed.
Untracked files.
git status
Output might look like:
msi@msi:~/Desktop/my-project$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
file1.txt
nothing added to commit but untracked files present (use "git add" to track)
git add <file-name> # Add a specific file
git add . # Add all files
Moves changes from the working directory to the staging area, marking them for the next commit.
git add file1.txt
git add .
This stages the file1.txt file or all files in the directory, respectively.
git commit -m "Your commit message"
Saves the changes in the staging area to the repository with a descriptive message.
git commit -m "Added a new feature to the project"
This commits all staged changes with the message "Added a new feature to the project."
Output might look like:
[master (root-commit) 2e0d35b] Added a new feature to the project
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file1.txt
git log
Shows the commit history for the repository, including commit hashes, author details, dates, and messages.
git log
Output:
commit 2e0d35bf87987f7bdcd8bd9cc6ac6051e8b12ab6 (HEAD -> master)
Author: anupkafle <hello@anupkafle.com.np>
Date: Fri Nov 29 22:15:56 2024 +0100
Added a new feature to the project
git branch <branch-name> # Create a new branch
git checkout <branch-name> # Switch to an existing branch
git checkout -b <branch-name> # Create and switch to a new branch
Branch creation: Creates a new branch to isolate work on a specific feature or bug.
Switching branches: Moves you to another branch in the repository.
git checkout -b feature/new-feature
This creates and switches to a branch named feature/new-feature.
Output:
Switched to a new branch 'feature/new-feature'
git merge <branch-name>
Combines changes from another branch into the current branch.
git checkout master
git merge feature/new-feature
This merges the feature/new-feature branch into the main branch.
git push origin <branch-name>
Uploads local commits from the specified branch to a remote repository.
git push origin master
Pushes the main branch to the remote repository. To proceed, you must first create a repository and link it to a remote URL.
To link your local repository to a remote repository, use the following command:
git remote add origin <remote-repository-url>
git pull
Fetches and merges changes from the remote repository into the current branch.
git pull
Updates the local branch with the latest changes from the remote branch.
git diff
Shows changes between the working directory and the repository (or between branches).
git diff
Displays line-by-line differences for modified files.
Output:
msi@msi:~/Desktop/my-project$ git diff
diff --git a/file1.txt b/file1.txt
index e69de29..537e61e 100644
--- a/file1.txt
+++ b/file1.txt
@@ -0,0 +1 @@
+hi this is test file
The git diff output shows the differences between the current working directory and the latest commit. In this case, the file file1.txt was modified, transitioning from an empty state (indicated by e69de29) to containing a single line, hi this is test file. The + symbol marks the addition of this line in the file.
git checkout -- <file-name> # Reverts changes to a file
git reset <file-name> # Removes a file from staging
Reverting: Discards changes in the working directory.
Resetting: Removes changes from the staging area without deleting them.
msi@msi:~/Desktop/my-project$ cat > file1.txt
Hello
msi@msi:~/Desktop/my-project$ cat file1.txt
Hello
msi@msi:~/Desktop/my-project$ git checkout -- file1.txt
msi@msi:~/Desktop/my-project$ cat file1.txt
msi@msi:~/Desktop/my-project$
git branch -d <branch-name>
Deletes a branch that is no longer needed.
msi@msi:~/Desktop/my-project$ git branch
feature/new-feature
* master
msi@msi:~/Desktop/my-project$ git branch -d feature/new-feature
Deleted branch feature/new-feature (was 2e0d35b).
msi@msi:~/Desktop/my-project$ git branch
* master
msi@msi:~/Desktop/my-project$
Deletes the feature/new-feature branch.
git remote -v
Lists remote repositories linked to your local repository.
git remote -v
Output:
msi@msi:~/Desktop/my-project$ git remote -v
origin https://github.com/anupkafle/testrepo.git (fetch)
origin https://github.com/anupkafle/testrepo.git (push)
git tag -a <tag-name> -m "Tag message"
Marks a specific commit with a version number or release label.
msi@msi:~/Desktop/my-project$ git tag -a v1.0.0 -m "Version 1.0.0"
msi@msi:~/Desktop/my-project$ git log
commit 2e0d35bf87987f7bdcd8bd9cc6ac6051e8b12ab6 (HEAD -> master, tag: v1.0.0)
Author: anupkafle <anupkafle24@gmail.com>
Date: Fri Nov 29 22:15:56 2024 +0100
Added a new feature to the project
Creates and pushes a v1.0.0 tag to the remote repository.
Git stash is a feature that allows you to temporarily save changes in your working directory that you don’t want to commit yet. This is useful when you need to switch branches or perform other tasks without losing your uncommitted changes. The changes are saved in a stack-like structure, and you can reapply them later.
git stash
Temporarily saves changes without committing.
git stash
To apply stashed changes later:
git stash apply
git reset --hard <commit-hash>
Resets the repository to a specific commit, discarding all changes after it.
git reset --hard 1a2b3c4d5e6f7g8h9i0j
Rolls back to the specified commit.
git help <command>
Provides detailed documentation for any Git command.
git help commit
Displays help information for the commit command.
git rebase <branch-name>
Integrates changes from one branch into another by moving the base of your branch to the latest commit on the target branch.
Keeps a linear commit history by replaying your changes on top of the target branch.
git rebase main
Rebases your current branch on top of the main branch.
When you want to synchronize your feature branch with the latest updates from the main branch while avoiding merge commits.
git cherry-pick <commit-hash>
git cherry-pick abc1234
Applies the commit with hash abc1234 onto your current branch.
When you need a specific change from another branch without merging the entire branch.
Git is an indispensable tool for modern software development, enabling teams to collaborate efficiently, maintain code integrity, and streamline project management. Its powerful commands, such as git commit, git branch, git rebase, and git stash, provide flexibility and control over your codebase, making it easier to manage complex projects. By mastering Git’s workflows—whether it’s basic version control, branch management, or advanced rebasing techniques—you can significantly enhance your productivity and contribute to a cleaner, more maintainable project history.
Incorporating Git into your development process not only helps you work better individually but also ensures seamless teamwork, especially when paired with platforms like GitHub or GitLab. Understanding Git’s capabilities and best practices is a crucial step in your journey toward becoming a proficient developer or DevOps engineer. With a strong foundation in Git, you are equipped to tackle challenges in version control, collaborate effectively, and deliver high-quality software.
FAQs
What is the difference between git pull, git fetch, and git clone?
git pull, git fetch, and git clone serve different purposes in Git workflows. git pull combines git fetch and git merge, fetching changes from the remote repository and immediately merging them into your current branch, making it ideal for quick synchronization. git fetch, on the other hand, only downloads changes from the remote repository without merging, allowing you to review updates before integrating them. Finally, git clone is used to create a local copy of an entire remote repository, typically when setting up a repository for the first time. Each command has its unique use case: pull for immediate updates, fetch for careful review, and clone for starting fresh.