Mastering Essential Git Commands: A DevOps Engineer’s Guide with Real Examples
Introduction
In the life of a DevOps engineer, Git is a cornerstone tool that ensures smooth version control and collaboration across teams. Knowing essential Git commands helps in managing codebases effectively, tracking changes, and troubleshooting issues quickly. This article covers the most used Git commands, with real-world examples to help you get the most out of Git in your DevOps role.
1. git clone
Purpose: Cloning repositories to your local environment is often the first step in working with any project.
Example:
git clone
https://github.com/username/project.git
- Clones the entire repository from GitHub, allowing you to work on the project locally. This is essential for setting up a local development environment.
2. git status
Purpose: Shows the current state of the working directory and staging area.
Example:
git status
- Displays modified files, new files, and staged changes. It’s a quick way to review what’s ready to be committed and any untracked changes, ensuring that nothing is missed before committing.
3. git add
Purpose: Stages changes for the next commit.
Example:
git add .
- Adds all modified and new files in the current directory to the staging area. Use this before committing to ensure that all intended changes are included in the next commit.
Selective Staging:
git add filename.txt
- Only stages
filename.txt
, which is useful when you want to commit changes selectively.
- Only stages
4. git commit
Purpose: Commits staged changes to the repository with a descriptive message.
Example:
git commit -m "Fix issue with load balancer configuration"
- Commits staged changes with a message describing the update. This is crucial for maintaining a clear history of changes, especially in collaborative projects.
Amending the Last Commit:
git commit --amend -m "Updated load balancer configuration and fixed typos"
- Use this to modify the last commit’s message or content, often to fix small mistakes or clarify messages.
5. git pull
Purpose: Fetches updates from the remote repository and integrates them with the current branch.
Example:
git pull origin main
- Updates the local
main
branch with the latest changes from the remotemain
branch. This is essential for staying in sync with the latest code changes from teammates.
- Updates the local
6. git push
Purpose: Uploads local changes to the remote repository.
Example:
git push origin feature-branch
- Pushes commits from the local
feature-branch
to the remote repository. This command is vital for sharing changes and enabling collaboration.
- Pushes commits from the local
7. git log
Purpose: Shows the commit history.
Example:
git log --oneline --graph
- Displays a condensed view of commit history in a graphical format, making it easier to track changes and branches.
8. git branch
Purpose: Lists, creates, or deletes branches.
Listing Branches:
git branch
- Lists all branches in the local repository, helping you see what branches are available and which one you’re currently on.
Creating a Branch:
git branch feature-xyz
- Creates a new branch named
feature-xyz
, often used to start a new feature without affecting the main branch.
- Creates a new branch named
Deleting a Branch:
git branch -d feature-xyz
- Deletes the specified branch locally. Use this to clean up branches no longer in use.
9. git checkout
Purpose: Switches branches or restores files.
Switching Branches:
git checkout feature-xyz
- Switches to the
feature-xyz
branch, often used when working on multiple features in parallel.
- Switches to the
Restoring Files:
git checkout filename.txt
- Reverts
filename.txt
to its last committed state, helpful when you want to discard changes in specific files.
- Reverts
10. git merge
Purpose: Merges changes from one branch into another.
Example:
git merge feature-xyz
- Merges changes from
feature-xyz
into the current branch, commonly used after completing feature development and preparing to integrate it with the main branch.
- Merges changes from
11. git rebase
Purpose: Reapplies commits on top of another base tip, often used to keep a cleaner commit history.
Example:
git rebase main
- Rebases the current branch onto the
main
branch, effectively moving all local commits on top of the latestmain
branch. This is particularly helpful to reduce merge conflicts.
- Rebases the current branch onto the
12. git stash
Purpose: Temporarily saves changes without committing them.
Example:
git stash save "WIP: partial configuration update"
- Stashes uncommitted changes, letting you work on another task without losing progress.
Retrieve Stashed Changes:
git stash pop
- Applies the last stashed changes back to the working directory, restoring your work when you’re ready to resume.
13. git reset
Purpose: Undoes changes by resetting the current branch to a specific commit.
Example:
git reset --soft HEAD~1
- Moves the HEAD back by one commit but keeps the changes staged, allowing you to adjust a commit message or other details.
Hard Reset:
git reset --hard HEAD~1
- Completely removes the last commit and its changes, often used for irrecoverable changes.
Conclusion
Mastering these Git commands enables DevOps engineers to efficiently manage code changes, collaborate seamlessly with teams, and troubleshoot code issues. By practicing these commands and understanding their use cases, you can enhance productivity and streamline DevOps workflows.
Subscribe to my newsletter
Read articles from Shaik Yams directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Shaik Yams
Shaik Yams
Cloud & DevOps Engineer | Kubernetes | AWS | Ansible | GIT | Terraform | Gitlab | Docker | Python