GIT Commands
GIT Commands
Page | 1
git rebase <branch_name>: Applies changes from one
branch onto another branch (instead of merging).
git branch -d <branch_name>: Deletes the specified branch
(locally).
git branch -D <branch_name>: Forcefully deletes a branch
(locally).
git remote set-head origin <branch_name>: Sets the
default branch for the remote repository.
Page | 2
git commit --amend: Modifies the last commit (can change
the commit message or add changes).
git commit --all: Automatically stages tracked files and
commits them.
git commit -a: Stages and commits all modified files.
git commit --no-verify: Skips pre-commit hooks while
committing.
git reset --soft HEAD~1: Moves the HEAD pointer back
one commit but leaves your changes staged.
git reset --hard HEAD~1: Moves the HEAD pointer back
one commit and discards changes in the working directory.
Page | 3
git show <commit_id>:<file_path>: Shows a specific file
at a particular commit.
Page | 4
git stash pop: Applies the latest stash and removes it from
the stash list.
git stash apply: Applies a stash without removing it from the
list.
git stash drop: Removes a specific stash from the stash list.
git stash clear: Clears all stashes.
git stash save "message": Stashes changes with an
optional message for identification.
git stash branch <branch_name>: Creates a new branch
from the stash and applies it.
Page | 5
git config --global user.email
"[email protected]": Sets your global email for
commits.
git config --list: Lists all configuration settings.
git config : Sets a specific configuration option.
git config --global core.editor : Sets the default text
editor for Git.
git config --global color.ui true: Enables colored
output in Git.
git config --global alias.st status: Creates a custom
Git alias (e.g., git st for git status).
Page | 6
12. Git Submodules (Managing external repositories
within a project)
git submodule add <repo_url> : Adds a new submodule to
the repository (downloads an external repo).
git submodule init: Initializes the submodules configured in
the repository (after cloning).
git submodule update: Updates the submodules to the
commit specified in the superproject.
git submodule status: Displays the current commit of the
submodule.
git submodule deinit : Removes a submodule from the
working directory.
git submodule update --remote: Updates the submodule
to the latest commit from the remote repository.
git submodule foreach : Runs a command in each
submodule.
Page | 7
git pull --no-commit: Pulls changes but doesn't
automatically commit them.
git push --force-with-lease: Forces push but checks if
the remote branch has been updated (safer than git push --force).
git push --force: Forces a push to the remote branch,
potentially overwriting changes.
git cherry-pick <commit_id>: Applies the changes from a
specific commit onto the current branch.
Page | 8
16. Git Hooks for Automation (Pre-commit and post-
commit hooks)
git commit-msg: A hook script that runs before the commit
message is finalized (you can use this to enforce rules, like
requiring certain keywords).
git pre-commit: A hook that can be used to run checks on files
before the commit is finalized, such as linters or tests.
git post-commit: A hook that is triggered after a commit has
been completed, useful for triggering actions like notifications or
build processes.
git pre-push: A hook that runs before a push to the remote
repository is initiated.
git post-merge: A hook that runs after a merge, ideal for
cleanup or setup tasks.
Page | 9
git archive --format=tar --
output=<output_file>.tar <branch_name>: Creates a
tarball archive of the repository at a specific branch.
git archive --format=zip --
output=<output_file>.zip <branch_name>: Creates a zip
archive of the repository at a specific branch.
Page | 10
Git for DevOps
1. Git for Continuous Integration/Continuous
Deployment (CI/CD)
git fetch --all: Fetches all branches from all remotes, often used
before triggering a build or deployment in a CI/CD pipeline.
git merge origin/: Merges the latest changes from a remote
branch into the local branch to ensure that the CI/CD pipeline
tests the most up-to-date version.
git push --force-with-lease: When integrated with CI/CD, this
command can be used to push changes forcefully while ensuring
that the remote branch hasn’t changed unexpectedly (prevents
accidental overwrites in collaborative environments).
git rebase origin/: Rebasing is often part of CI/CD to ensure your
branch is up to date with the base branch before merging or
deploying.
git tag <version_number>: Tagging is essential in DevOps for
versioning releases, allowing the CI/CD system to deploy specific
versions of the code (e.g., for staging or production).
7. GitOps Workflow
GitOps is an approach to Continuous Deployment where Git
repositories are the source of truth for infrastructure and application
configuration.
Page | 13
git push : In GitOps, pushing changes to a Git repository
automatically triggers deployment pipelines. For instance, when
code or configuration files are pushed to Git, the repository can
trigger an automation system (like ArgoCD or Flux) to apply those
changes to a Kubernetes cluster or other environments.
git clone <repo_url>: Developers clone GitOps repositories to
inspect or modify deployment configurations stored as code.
Page | 15
git rebase --skip: Skips the current commit while rebasing, useful
if there’s a conflict or you don’t want to include a specific commit
in the rebase.
git rebase --continue: Continues the rebase process after
resolving any conflicts.
Page | 16
git log --show-signature: Displays the GPG signature of commits in
the log to verify their authenticity.
Page | 17
git log --oneline: Use this command to find the commit hash for
the state you want to roll back to.
git checkout <commit_id>: Checks out an older commit, allowing
you to inspect the repository at that point in time or even deploy
an older version of the app.
Page | 18
git fetch origin <feature_branch>: Fetches a feature branch from
a remote repository so that team members can review it or test it
locally.
git pull origin <feature_branch>: Pulls the latest changes from a
feature branch into your local repository, often used when testing
code changes before merging.
git diff <branch_name>: Compare different branches (e.g.,
comparing feature-xyz with main branch) before merging them.
Page | 19
Git is tightly integrated with CI/CD systems, where it triggers build and
deployment processes based on repository changes. Here are key Git
commands related to build automation:
Page | 20