Skip to main content

Command Palette

Search for a command to run...

Essential Git Commands for DevOps

Updated
•7 min read•View as Markdown
Essential Git Commands for DevOps
P

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

  1. 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_project

  2. Clone 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.git

  3. Add 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.html

  4. Commit 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"

  5. 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 main

  6. Pull Changes from a Remote Repository
    This command fetches updates from a remote repository and merges them into your current branch.
    Command: git pull origin main

  7. Show 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 status

  8. Show Commit History
    This command lists all the commits made in the repository, showing the commit messages and authors.
    Command: git log

  9. Show 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 --oneline

  10. Show 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

  1. List Branches
    This command shows all local branches in your repository. It highlights the branch you are currently on.
    Command: git branch

  2. Create a New Branch
    This command creates a new branch for development. You can work on features separately from the main branch.
    Command: git branch feature1

  3. Switch 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 feature1

  4. Merge a Branch into the Current Branch
    This command combines the changes from another branch into your current branch.
    Command: git merge feature1

  5. Delete a Branch
    This command removes a branch that is no longer needed from your local repository.
    Command: git branch -d feature1

  6. Rename a Branch
    This command allows you to rename the current branch you are on.
    Command: git branch -m new_branch_name

  7. Check Out a New Branch
    This command creates a new branch and switches to it in one step.
    Command: git checkout -b new_branch_name

  8. Show the Last Commit on a Branch
    This command shows the latest commit on the specified branch.
    Command: git show branch_name

3. Remote Repositories

  1. List Remote Repositories
    This command shows all remote connections linked to your local repository, including their URLs.
    Command: git remote -v

  2. Add 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.git

  3. Remove a Remote Repository
    This command removes a remote repository link from your local Git settings.
    Command: git remote remove origin

  4. Fetch Changes from a Remote Repository
    This command downloads changes from the remote repository but does not merge them.
    Command: git fetch origin

  5. Push All Branches to a Remote Repository
    This command sends all local branches to the specified remote repository.
    Command: git push --all origin

  6. Show Remote Branches
    This command lists all branches from the remote repository.
    Command: git branch -r

4. Other Useful Commands

  1. 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~1

  2. Reverse a Commit
    Use this command to create a new commit that undoes changes from a previous commit.
    Command: git revert HEAD~1

  3. Save 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 stash

  4. Apply a Stashed Change
    Use this command to bring back the most recent stashed changes into your working directory.
    Command: git stash apply

  5. List Stashes
    This command shows all the stashed changes that you have saved in your repository.
    Command: git stash list

  6. Drop a Stash
    This command permanently removes a specific stash entry from your stash list.
    Command: git stash drop stash@{0}

  7. Clear All Stashes
    This command deletes all stashed changes from the stash list.
    Command: git stash clear

5. Advanced Commands

  1. 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 feature1

  2. Apply 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>

  3. 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 start

  4. Interactive Rebase
    This command allows you to rebase commits interactively, giving you control over how commits are applied.
    Command: git rebase -i HEAD~n

  5. Show 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

  1. Initialize Git Flow
    This command sets up a Git flow workflow in your repository, organizing feature and release branches.
    Command: git flow init

  2. Start 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-feature

  3. Finish a Feature Branch
    This command merges your completed feature branch back into the main branch.
    Command: git flow feature finish new-feature

  4. Start 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.0

  5. Finish 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.0

  6. Start a Hotfix Branch
    This command creates a branch for quickly addressing a production issue.
    Command: git flow hotfix start hotfix-name

  7. Finish 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

  1. Show Git Configuration
    This command displays your current Git configuration settings.
    Command: git config --list

  2. Set 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"

  3. 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

  1. View Changes in a Specific File
    This command shows changes made to a specific file since the last commit.
    Command: git diff filename

  2. Check the Current Branch
    This command displays the branch you are currently on.
    Command: git branch --show-current

  3. Create 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"

  4. 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 😎

More from this blog

DevOps

27 posts