Skip to main content

Command Palette

Search for a command to run...

Title: Essential Git Commands Daily Used by DevOps Engineers

Published
•6 min read•View as Markdown
Title: Essential Git Commands Daily Used by DevOps Engineers
C

Proficient in Devops ,cloud and SDLC including analysis, design, coding, scripting ,testing, automation ,version control, documentation, support.

Introduction:

Git is a widely used version control system that has become an integral part of the DevOps workflow. DevOps engineers rely heavily on Git to manage source code, collaborate with team members, and automate deployments. In this blog, we will explore some essential Git commands that are commonly used by DevOps engineers in their day-to-day work. Understanding these commands will help you become more proficient in version control and streamline your DevOps processes.

Below is a comprehensive list of commonly used Git commands:

  1. Configuration:

    • git config --global user.name "Your Name": Sets your global username.

    • git config --global user.email "youremail@example.com": Sets your global email.

    • git config --list: Lists all Git configurations.

  2. Creating and Cloning Repositories:

    • git init: Initializes a new Git repository in the current directory.

    • git clone <repository_url>: Clones an existing repository to your local machine.

  3. Basic Workflow:

    • git status: Shows the status of your working directory and staged files.

    • git add <file_name>: Stages a file for the next commit.

    • git add .: Stages all modified files for the next commit.

    • git commit -m "Your commit message here": Commits the staged changes with a message.

    • git commit -a -m "Your commit message here": Stages and commits all modified files in one step.

    • git push origin <branch_name>: Pushes committed changes to a remote repository.

    • git pull: Fetches and merges changes from the remote repository into the current branch.

    • git fetch: Fetches changes from the remote repository without merging.

  4. Branches:

    • git branch: Lists all local branches and shows the current branch with an asterisk.

    • git branch <branch_name>: Creates a new branch.

    • git checkout <branch_name>: Switches to the specified branch.

    • git checkout -b <branch_name>: Creates and switches to a new branch in one step.

    • git merge <branch_name>: Merges the specified branch into the current branch.

    • git branch -d <branch_name>: Deletes the specified branch.

  5. Stashing Changes:

    • git stash: Temporarily saves changes in a "stash" to work on something else.

    • git stash list: Lists all stashes.

    • git stash apply: Applies the most recent stash to the working directory.

    • git stash pop: Applies and removes the most recent stash from the stash list.

  6. Undoing Changes:

    • git reset <file_name>: Unstages a file but preserves its changes.

    • git reset --hard HEAD: Discards all local changes and resets to the last commit.

    • git revert <commit_hash>: Reverts a specific commit and creates a new commit to undo its changes.

    • git clean -f: Removes untracked files from the working directory.

  7. History and Logs:

    • git log: Displays the commit history.

    • git log --oneline: Displays the commit history in a simplified format.

    • git log --author="Author Name": Filters the commit history by the specified author.

    • git diff: Shows the changes between the working directory and the last commit.

    • git diff <commit1> <commit2>: Shows the changes between two specific commits.

  8. Remote Repositories:

    • git remote add <remote_name> <repository_url>: Adds a remote repository.

    • git remote -v: Lists all remote repositories and their URLs.

    • git push <remote_name> <branch_name>: Pushes changes to a specific branch in a remote repository.

    • git pull <remote_name> <branch_name>: Fetches and merges changes from a remote repository.

  9. Ignoring Files:

    • Create a file named .gitignore in the root directory and add the names of files and directories to be ignored.
  1. Rebase:
  • git rebase <branch_name>: Reapplies commits from the current branch on top of another branch, effectively moving the branch's base to the latest commit of <branch_name>.
  1. Cherry-Pick:
  • git cherry-pick <commit_hash>: Applies the changes of a specific commit onto the current branch.
  1. Tagging:
  • git tag: Lists all tags in the repository.

  • git tag <tag_name>: Creates a lightweight tag at the current commit.

  • git tag -a <tag_name> -m "Tag message": Creates an annotated tag with a message.

  1. Working with Remote Repositories:
  • git remote: Lists all remote repositories.

  • git remote show <remote_name>: Shows information about a specific remote repository.

  • git remote rename <old_name> <new_name>: Renames a remote repository.

  • git remote remove <remote_name>: Removes a remote repository.

  1. Fetching and Pulling:
  • git fetch <remote_name>: Fetches changes from a remote repository without merging.

  • git pull --rebase <remote_name> <branch_name>: Fetches and rebases changes from a remote repository.

  1. Reflogs:
  • git reflog: Lists the history of all references, including changes to branches and HEAD.
  1. Git Log Formatting:
  • git log --graph: Displays the commit history as a graph.

  • git log --oneline --graph --all: Shows a condensed graph view of all branches.

  1. Interactive Rebase:
  • git rebase -i <commit_hash>: Allows interactive reordering, editing, or squashing of commits.
  1. Git Bisect:
  • git bisect start: Initiates the binary search for the commit that introduced a bug.

  • git bisect good <commit_hash>: Marks a commit as good (bug-free).

  • git bisect bad <commit_hash>: Marks a commit as bad (contains a bug).

  • git bisect reset: Exits the bisect mode.

  1. Submodules:
  • git submodule add <repository_url>: Adds a new submodule to the repository.

  • git submodule update: Initializes and updates submodules in the repository.

  1. Cleaning and Maintenance:
  • git clean -fd: Removes untracked files and directories forcefully.

  • git gc: Cleans and optimizes the local repository.

  1. Git Hooks:
  • Git hooks are scripts that can be triggered at certain points during Git's execution. They reside in .git/hooks directory and have names like pre-commit, post-merge, etc.Git Revert Range:
  • git revert <start_commit>^..<end_commit>: Reverts a range of commits, starting from start_commit (exclusive) up to and including end_commit.
  1. Cherry-Pick Range:
  • git cherry-pick <start_commit>^..<end_commit>: Applies a range of commits, starting from start_commit (exclusive) up to and including end_commit, onto the current branch.
  1. Recovering Lost Commits:
  • git reflog: Lists the history of all references, including changes to branches and HEAD. Use it to recover lost commits.
  1. Squashing Commits:
  • git reset --soft HEAD~<number_of_commits>: Squashes the last <number_of_commits> commits into one, preserving changes in the working directory.
  1. Amending Commits:
  • git commit --amend: Adds staged changes to the previous commit, effectively editing the last commit message.
  1. Stashing with Untracked Files:
  • git stash -u: Stashes both modified and untracked files.
  1. Stashing Specific Files:
  • git stash push <file_name>: Stashes specific files, not just all changes.
  1. Working with Remotes:
  • git fetch --prune: Fetches changes from remote, removing any remote branches that no longer exist.

  • git push --tags: Pushes only tags to the remote repository.

  1. Git Blame:
  • git blame <file_name>: Shows the author and last modification details of each line in a file.
  1. Ignoring Tracked Files:
  • git update-index --assume-unchanged <file_name>: Tells Git to ignore changes to a tracked file.
  1. Showing File Changes Between Branches:
  • git diff <branch1>..<branch2> -- <file_name>: Shows changes for a specific file between two branches.
  1. Using Patch Files:
  • git diff > changes.patch: Exports the changes as a patch file.

  • git apply changes.patch: Applies a patch file to the repository.

  1. Stashing with Untracked Files:
  • git stash -u: Stashes both modified and untracked files.
  1. Custom Difftool and Mergetool:
  • git difftool: Configures and runs a custom tool for visualizing differences between commits.

  • git mergetool: Configures and runs a custom tool for resolving merge conflicts.

  1. Bisect Reset:
  • git bisect reset: Exits the bisect mode and returns to the original branch.

More from this blog

Untitled Publication

25 posts