Essential Git Commands for DevOps

DevOps Enthusiast | Exploring Cloud & Automation I’m an aspiring DevOps professional passionate about cloud computing, automation, and CI/CD pipelines. I’m currently gaining hands-on experience with tools like AWS, Docker, Kubernetes, Jenkins and GitLab CI/CD, Terraform, Ansible, and more. My focus is on improving development and deployment processes through continuous learning and practical application.
Alongside my learning journey, I share my insights and experiences through blogs to help others who are exploring the same path. I believe in learning together and growing by sharing knowledge, and I’m excited to connect with like-minded individuals in the field.
Let’s collaborate, share ideas, and grow together!
Introduction
In the world of DevOps, Git is a key tool for managing code. Whether you’re working on a big project or just keeping track of your own work, knowing the right Git commands can make your life easier. This blog will share essential Git commands that every DevOps engineer should know. These commands will help you create repositories, manage branches, and keep your code organized. Let’s dive in and explore these useful tools that can boost your productivity
Essential Git Commands for DevOps
Understanding Git is important for managing code and collaborating effectively in DevOps. Here’s a list of essential Git commands you’ll need, organized by category.
1. Basic Commands
Initialize a New Git Repository
This command sets up a new Git repository in your current directory. You need to do this before making any commits.
Command: git init new_projectClone a Remote Repository
Use this command to copy a remote Git repository to your local machine. It includes all files and history.
Command: git clone https://github.com/user/repo.gitAdd Files to the Staging Area
This command prepares files to be committed. You can add all files or specify one file.
Command: git add index.htmlCommit Changes with a Message
This command saves your staged changes. It's important to include a message explaining what you changed.
Command: git commit -m "Add new feature"Push Changes to a Remote Repository
Use this command to send your local changes to the remote repository. Specify which branch you are pushing to.
Command: git push origin mainPull Changes from a Remote Repository
This command fetches updates from a remote repository and merges them into your current branch.
Command: git pull origin mainShow the Current State of the Working Directory
This command displays which files have been modified, added, or deleted, and the current branch you’re on.
Command: git statusShow Commit History
This command lists all the commits made in the repository, showing the commit messages and authors.
Command: git logShow Commit History in One Line
This command displays a simplified list of commits with abbreviated hashes and messages. It’s useful for quickly reviewing the commit history.
Command: git log --onelineShow Differences Between Files
Use this command to see changes made to files that are not staged. It helps you review modifications.
Command: git diff index.html
2. Branching and Merging
List Branches
This command shows all local branches in your repository. It highlights the branch you are currently on.
Command: git branchCreate a New Branch
This command creates a new branch for development. You can work on features separately from the main branch.
Command: git branch feature1Switch to a Different Branch
Use this command to change your working branch. This allows you to work on a different feature or bug fix.
Command: git checkout feature1Merge a Branch into the Current Branch
This command combines the changes from another branch into your current branch.
Command: git merge feature1Delete a Branch
This command removes a branch that is no longer needed from your local repository.
Command: git branch -d feature1Rename a Branch
This command allows you to rename the current branch you are on.
Command: git branch -m new_branch_nameCheck Out a New Branch
This command creates a new branch and switches to it in one step.
Command: git checkout -b new_branch_nameShow the Last Commit on a Branch
This command shows the latest commit on the specified branch.
Command: git show branch_name
3. Remote Repositories
List Remote Repositories
This command shows all remote connections linked to your local repository, including their URLs.
Command: git remote -vAdd a Remote Repository
Use this command to link a remote repository to your local repository, enabling you to push and pull changes.
Command: git remote add origin https://github.com/user/repo.gitRemove a Remote Repository
This command removes a remote repository link from your local Git settings.
Command: git remote remove originFetch Changes from a Remote Repository
This command downloads changes from the remote repository but does not merge them.
Command: git fetch originPush All Branches to a Remote Repository
This command sends all local branches to the specified remote repository.
Command: git push --all originShow Remote Branches
This command lists all branches from the remote repository.
Command: git branch -r
4. Other Useful Commands
Reset to a Specific Commit
This command changes the current branch to a previous commit, discarding any changes made after that commit.
Command: git reset --hard HEAD~1Reverse a Commit
Use this command to create a new commit that undoes changes from a previous commit.
Command: git revert HEAD~1Save Changes to a Stash
This command temporarily hides your changes in the stash, allowing you to work on something else without losing your progress.
Command: git stashApply a Stashed Change
Use this command to bring back the most recent stashed changes into your working directory.
Command: git stash applyList Stashes
This command shows all the stashed changes that you have saved in your repository.
Command: git stash listDrop a Stash
This command permanently removes a specific stash entry from your stash list.
Command: git stash drop stash@{0}Clear All Stashes
This command deletes all stashed changes from the stash list.
Command: git stash clear
5. Advanced Commands
Rebase a Branch onto Another
This command allows you to integrate changes from one branch onto another, keeping a clean commit history.
Command: git rebase main feature1Apply a Commit from Another Branch
Use this command to take a specific commit from a different branch and apply it to your current branch.
Command: git cherry-pick <commit_hash>Find the Commit that Introduced a Bug
This command helps you track down which commit caused a specific problem in your code.
Command: git bisect startInteractive Rebase
This command allows you to rebase commits interactively, giving you control over how commits are applied.
Command: git rebase -i HEAD~nShow Differences Between Two Branches
This command shows changes between two branches, helping you understand differences before merging.
Command: git diff branch1..branch2
6. Git Workflow Commands
Initialize Git Flow
This command sets up a Git flow workflow in your repository, organizing feature and release branches.
Command: git flow initStart a New Feature Branch
Use this command to begin work on a new feature by creating and switching to a new branch.
Command: git flow feature start new-featureFinish a Feature Branch
This command merges your completed feature branch back into the main branch.
Command: git flow feature finish new-featureStart a New Release Branch
This command creates a new branch for preparing a release, allowing you to finalize features.
Command: git flow release start v1.0Finish a Release Branch
Use this command to merge the release branch into the main branch and prepare for deployment.
Command: git flow release finish v1.0Start a Hotfix Branch
This command creates a branch for quickly addressing a production issue.
Command: git flow hotfix start hotfix-nameFinish a Hotfix Branch
This command merges the hotfix branch back into the main branch and prepares for deployment.
Command: git flow hotfix finish hotfix-name
7. Configuration and Info Commands
Show Git Configuration
This command displays your current Git configuration settings.
Command: git config --listSet User Information
Use this command to set your name and email for commits.
Command: git config --global user.name "Your Name"
Command: git config --global user.email "you@example.com"Change the Default Editor
This command sets your preferred text editor for Git commit messages to Vim.
Command: git config --global core.editor vim
8. Miscellaneous Commands
View Changes in a Specific File
This command shows changes made to a specific file since the last commit.
Command: git diff filenameCheck the Current Branch
This command displays the branch you are currently on.
Command: git branch --show-currentCreate a Tag
Use this command to mark a specific commit with a tag for easy reference.
Command: git tag -a v1.0 -m "Version 1.0 release"Push Tags to Remote Repository
This command sends your tags to the remote repository.
Command: git push origin --tags
Thank you for checking out this collection of essential Git commands! These commands are key tools for anyone working in DevOps or software development. If you need practical examples of any commands, just comment, and I will definitely create those for you in the future. To learn more about different topics, check out my other blogs. Happy learning 😎




