This guide provides a complete list of essential, advanced, and DevOps-specific Git commands. It’s tailored for professionals working in environments where Git is essential for version control, collaboration, and continuous integration/delivery.
- Command:
git init
- Usage: Initializes a new Git repository in your current directory to begin version control.
- Command:
git clone <repository-url>
- Usage: Creates a copy of an existing repository to your local machine.
- Command:
git status
- Usage: Displays the state of the working directory, showing untracked files, staged changes, and modifications.
- Command:
git add <file>
orgit add .
- Usage: Stages changes for the next commit.
- Command:
git add -p <file>
- Usage: Interactively stages specific portions (hunks) of a file, ideal for partial commits.
- Command:
git commit -m "Commit message"
- Usage: Records changes to the local repository.
- Command:
git log
- Usage: Shows the commit history.
- Command:
git branch <branch-name>
- Usage: Creates a new branch for development.
- Command:
git checkout <branch>
- Usage: Switches to another branch.
- Command:
git push origin <branch>
- Usage: Pushes local commits to a remote repository.
- Command:
git pull origin <branch>
- Usage: Fetches and merges changes from a remote repository.
- Command:
git merge <branch>
- Usage: Merges the changes from one branch into another.
- Command:
git commit --amend --no-edit
- Usage: Modifies the last commit without changing the commit message.
- Command:
git rebase -i HEAD~5
- Usage: Clean up commit history by reordering or squashing commits.
- Command:
git stash
orgit stash save "message"
- Usage: Temporarily saves your changes without committing.
- Command:
git stash push -p
- Usage: Stash specific changes in a file interactively.
- Command:
git cherry-pick <commit-hash>
- Usage: Apply a specific commit from another branch.
- Command:
git reflog
- Usage: Tracks actions in Git, even those not in history, useful for recovering lost commits.
- Command:
git bisect start
- Usage: Perform a binary search to find the exact commit that introduced a bug.
- Command:
git sparse-checkout init --cone && git sparse-checkout set <folder>
- Usage: Only check out a portion of a large repository.
- Command:
git commit -S -m "Commit message"
- Usage: Sign your commits with a GPG key for secure verification.
- Command:
git push --force-with-lease
- Usage: Force push your changes while preventing overwriting someone else’s work.
- Command:
git config merge.tool <tool>
- Usage: Specify a custom merge tool to visually resolve conflicts.
- Command:
git tag -a v1.0.0 -m "Version 1.0"
- Usage: Create an annotated tag for versioning your releases in CI/CD pipelines.
- Command:
git push origin v1.0.0
- Usage: Push the tag to the remote repository.
- Command:
git push origin <branch> --tags
- Usage: Push changes and tags together for triggering deployments in CI/CD systems like Jenkins, GitLab CI, or GitHub Actions.
- Command:
vi .git/hooks/pre-commit
- Usage: Automate tasks like running tests or linting code before committing by configuring Git hooks.
- Command:
git rev-list --objects --all | sort -k 2 | cut -f1 | xargs git cat-file -s | sort -n -r | head -n 10
- Usage: Lists the top 10 largest files in your Git history to optimize repository size.
- Command:
git submodule add <repository-url>
- Usage: Use Git submodules to manage external dependencies in infrastructure-as-code repositories (e.g., Terraform, Ansible).
- Command:
.gitlab-ci.yml
- Usage: Define a
.gitlab-ci.yml
configuration file to automate testing, building, and deploying code in a GitLab CI/CD pipeline.
- Command:
git worktree add <path> <branch>
- Usage: Use Git worktrees to work on multiple branches simultaneously, useful in multi-environment deployments (e.g., development, staging, production).
- Command:
.github/workflows/deploy.yml
- Usage: Use GitHub Actions by defining deployment workflows in YAML, automating the deployment process for your infrastructure.
- Command:
git cherry-pick <commit-hash>
- Usage: Apply hotfixes to production branches without merging an entire feature branch.
- Command:
git branch -m development
- Usage: Maintain different branches for environments (e.g., development, staging, production) and switch between them as needed.
- Command:
docker build -t <image> . && docker run <image>
- Usage: Use GitLab CI and Docker to automate building and deploying Docker containers in CI/CD pipelines.
- Command:
git push origin master
- Usage: Apply GitOps principles by pushing changes to a repository that triggers infrastructure automation and deployment.
- Command:
git config --global alias.co checkout
- Usage: Create aliases to speed up repetitive tasks.
- Command:
git config --global alias.deploy '!git push origin master --tags && ssh user@server "cd /path/to/project && git pull && restart-service"'
- Usage: Automate deployments with a single alias.