"Mastering Git: Essential Commands and DevOps Applications"
Photo by Praveen Thirumurugan on Unsplash
1. git init
Starts a new Git repository in the current folder.
bashCopy codegit init
2. git clone
Clones a remote repository to your local machine.
bashCopy codegit clone https://github.com/Pratiklahamge/MyResume.git
3. git status
Displays the state of the working directory and the staging area.
bashCopy codegit status
4. git add
Adds files to the staging area.
bashCopy codegit add filename.txt
git add .
5. git commit
Commits the changes in the staging area with a message.
bashCopy codegit commit -m "Initial commit"
6. git push
Pushes the committed changes to a remote repository.
bashCopy codegit push origin main
7. git pull
Fetches and merges changes from the remote repository into the local branch.
bashCopy codegit pull origin main
8. git branch
Lists, creates, or deletes branches.
bashCopy codegit branch # List all branches
git branch new-branch # Create new branch
git branch -d branch-name # Delete branch
9. git checkout
Switches to another branch or restores files.
bashCopy codegit checkout main # Switch to the main branch
git checkout -b feature-branch # Create and switch to a new branch
10. git merge
Merges a branch into the current branch.
bashCopy codegit merge feature-branch
11. git log
Displays the commit history.
bashCopy codegit log
12. git reset
Resets the index and working directory to a previous commit.
bashCopy codegit reset --hard HEAD~1 # Reset to the previous commit
13. git rebase
Reapplies commits on top of another base commit.
bashCopy codegit rebase main
14. git stash
Stashes changes in a dirty working directory.
bashCopy codegit stash
15. git stash apply
Re-applies the latest stashed changes.
bashCopy codegit stash apply
16. git fetch
Fetches changes from a remote repository without merging them.
bashCopy codegit fetch origin
17. git diff
Shows changes between commits, branches, or your working directory.
bashCopy codegit diff
18. git remote
Manages remote connections to repositories.
bashCopy codegit remote add origin https://github.com/Pratiklahamge/MyResume.git
19. git tag
Creates, lists, or deletes tags.
bashCopy codegit tag v1.0 # Create a new tag
git tag # List all tags
git tag -d v1.0 # Delete a tag
20. git rm
Removes files from the working directory and staging area.
bashCopy codegit rm filename.txt
21. git mv
Moves or renames files in the repository.
bashCopy codegit mv oldname.txt newname.txt
22. git cherry-pick
Applies changes from a specific commit.
bashCopy codegit cherry-pick <commit-hash>
23. git bisect
Uses binary search to find the commit that introduced a bug.
bashCopy codegit bisect start
git bisect bad
git bisect good <commit-hash>
24. git blame
Shows who made the last change to each line of a file.
bashCopy codegit blame filename.txt
25. git show
Shows details about a commit.
bashCopy codegit show <commit-hash>
26. git revert
Creates a new commit that undoes changes from a previous commit.
bashCopy codegit revert <commit-hash>
27. git config
Configures Git settings like username and email.
bashCopy codegit config --global user.name "Pratik Lahamge"
git config --global user.email "plahamge340@gmail.com"
28. git submodule
Manages submodules within a repository.
bashCopy codegit submodule add https://github.com/example/repo.git
29. git clean
Removes untracked files from the working directory.
bashCopy codegit clean -f
30. git archive
Creates a tar or zip archive of the repository.
bashCopy codegit archive --format=tar HEAD | tar -x -C /path/to/extract
Commands use by devops Enginersโโ
1. git clone
Use: DevOps engineers often clone repositories to set up environments, deploy applications, or configure infrastructure.
bashCopy codegit clone https://github.com/Pratiklahamge/devops-repo.git
Example: Cloning the configuration files for infrastructure as code (IaC) setup using Terraform scripts.
2. git pull
Use: Pulls the latest code from the remote repository to ensure the environment or deployment is up-to-date.
bashCopy codegit pull origin main
Example: Before deploying an application, a DevOps engineer pulls the latest changes to avoid deploying outdated code.
3. git push
Use: After automating a build or adding scripts, a DevOps engineer pushes the changes to the repository.
bashCopy codegit push origin feature-branch
Example: Pushing a new Dockerfile or a Kubernetes YAML configuration after modifying it for CI/CD pipelines.
4. git branch
Use: Creating branches for different environments (e.g., development, staging, production).
bashCopy codegit branch production
git checkout production
Example: A DevOps engineer creates a "production" branch for the final deployment environment.
5. git merge
Use: Merges feature or bug fix branches into the main branch for continuous delivery.
bashCopy codegit merge feature-branch
Example: After a feature is fully tested, a DevOps engineer merges it into the production branch for deployment.
6. git commit
Use: Commits changes in configuration, scripts, or infrastructure templates with detailed messages.
bashCopy codegit commit -m "Updated Jenkinsfile for new build pipeline"
Example: Committing changes to Jenkins pipeline scripts that automate testing and deployment.
7. git fetch
Use: Fetches updates from remote repositories without merging them into the current branch. Useful for monitoring remote changes.
bashCopy codegit fetch origin
Example: Fetching changes from the infrastructure repository to review updates without directly applying them.
8. git rebase
Use: Reapplies commits from one branch onto another. Often used to ensure branches are up-to-date.
bashCopy codegit rebase main
Example: Rebase the feature branch with the latest main branch before deploying to ensure no conflicts.
9. git reset
Use: Resets changes in case something goes wrong during deployment or configuration setup.
bashCopy codegit reset --hard HEAD~1
Example: A DevOps engineer resets a branch to the previous commit when a configuration file update caused an issue in production.
10. git stash
Use: Temporarily stashes local changes, useful when switching between different tasks or branches without losing work.
bashCopy codegit stash
git stash apply
Example: Stashing changes during an ongoing deployment, then switching to another branch to fix a bug.
11. git tag
Use: Creates tags for specific releases or deployments.
bashCopy codegit tag v1.0
git push origin v1.0
Example: Tagging a commit with v1.0 before deploying the first version of the application to production.
12. git log
Use: Reviews the commit history, often used during audits, troubleshooting, or post-deployment checks.
bashCopy codegit log --oneline
Example: Checking the commit history to verify when a particular feature or bug fix was deployed.
13. git cherry-pick
Use: Picks a specific commit from one branch and applies it to another branch.
bashCopy codegit cherry-pick <commit-hash>
Example: A specific bug fix from a development branch is cherry-picked to the production branch for immediate deployment.
14. git config
Use: Configures Git settings for global or repository-specific use, often used to set up credentials.
bashCopy codegit config --global user.name "Pratik Lahamge"
git config --global user.email "plahamge340@gmail.com"
Example: Configuring Git to use a CI/CD pipeline user for automated commits and deployments.
15. git diff
Use: Shows the changes between commits, branches, or files. Useful for code reviews and troubleshooting.
bashCopy codegit diff main feature-branch
Example: A DevOps engineer compares the production branch with the feature branch to ensure only necessary changes will be deployed.
16. git rm
Use: Removes files from the repository and staging area.
bashCopy codegit rm obsolete-file.txt
Example: Removing old Kubernetes or Terraform files that are no longer needed in the project.
17. git stash pop
Use: Applies and removes the latest stashed changes.
bashCopy codegit stash pop
Example: After switching back to a branch, the engineer applies the stashed configuration changes to resume their work.
18. git show
Use: Displays information about a commit, often used in troubleshooting or post-deployment audits.
bashCopy codegit show <commit-hash>
Example: Inspecting a commit to understand which files were changed before deployment.
19. git bisect
Use: Finds the commit that introduced a bug using binary search.
bashCopy codegit bisect start
git bisect bad
git bisect good <commit-hash>
Example: Identifying the commit that caused a failure in the deployment pipeline.
20. git archive
Use: Creates a zip or tar archive of the project, useful for releases or backup.
bashCopy codegit archive --format=zip HEAD -o release.zip
Example: Creating an archive of the latest version of the repository for deploying to a specific environment.
21. git submodule
Use: Manages external dependencies stored in other Git repositories.
bashCopy codegit submodule add https://github.com/example/config-repo.git
Example: Adding a submodule that contains shared configuration files or Terraform modules used across multiple environments.
22. git clean
Use: Cleans untracked files from the working directory, useful before deployments to avoid leftover artifacts.
bashCopy codegit clean -f
Example: Cleaning up old log files and artifacts before a new deployment.
23. git remote -v
Use: Verifies the URLs for all remote repositories.
bashCopy codegit remote -v
Example: Checking the remote repository URLs to ensure you're pushing changes to the correct environment (staging, production, etc.).
24. git ls-files
Use: Shows information about files in the index and the working directory.
bashCopy codegit ls-files
Example: Listing all tracked files during troubleshooting to verify which files are included in the deployment.
25. git revert
Use: Creates a new commit that reverses the changes from a previous commit, often used to undo a bad deployment.
bashCopy codegit revert <commit-hash>
Example: Reverting a commit that introduced a bug into production without modifying the history.
26. git submodule update
Use: Updates submodules to the latest commit from their remote repository.
bashCopy codegit submodule update --remote
Example: Ensuring all submodules, such as Terraform modules, are up-to-date before deploying infrastructure changes.
Getting & Creating Projects
Command | Description | Example |
git init | Initialize a local Git repository | git init |
git clone https://github.com/Pratiklahamge/[repository-name].git | Create a local copy of a remote repository | git clone https://github.com/Pratiklahamge/MyRepo.git |
Basic Snapshotting
Command | Description | Example |
git status | Check the status of the working directory and staging area | git status |
git add [file-name.txt] | Add a file to the staging area | git add README.md |
git add -A | Add all new and changed files to the staging area | git add -A |
git commit -m "[commit message]" | Commit changes with a message | git commit -m "Updated README and added new feature" |
git rm -r [file-name.txt] | Remove a file or folder from the repository | git rm old_file.txt |
git remote -v | View the remote repository URL(s) | git remote -v |
Branching & Merging
Command | Description | Example |
git branch | List all branches (current branch marked with * ) | git branch |
git branch -a | List all local and remote branches | git branch -a |
git branch [branch-name] | Create a new branch | git branch feature-branch |
git branch -d [branch-name] | Delete a local branch | git branch -d feature-branch |
git push origin --delete [branch-name] | Delete a remote branch | git push origin --delete feature-branch |
git checkout -b [branch-name] | Create and switch to a new branch | git checkout -b new-feature |
git branch -m [old-name] [new-name] | Rename a local branch | git branch -m old-branch new-branch |
git checkout [branch-name] | Switch to a branch | git checkout main |
git checkout -- [file-name.txt] | Discard changes to a file | git checkout -- file.txt |
git merge [branch-name] | Merge a branch into the current active branch | git merge feature-branch |
git stash | Stash changes in a dirty working directory | git stash |
git stash pop | Apply the latest stashed changes | git stash pop |
git stash clear | Remove all stashed entries | git stash clear |
Sharing & Updating Projects
Command | Description | Example |
git push origin [branch-name] | Push a branch to the remote repository | git push origin main |
git push -u origin [branch-name] | Push and set the upstream tracking branch | git push -u origin dev |
git pull | Update local repository to the newest commit | git pull |
git pull origin [branch-name] | Pull changes from a specific branch | git pull origin main |
git remote add origin https://github.com/Pratiklahamge/[repository-name].git | Add a remote repository | git remote add origin https://github.com/Pratiklahamge/MyRepo.git |
git remote set-url origin https://github.com/Pratiklahamge/[repository-name].git | Change the remote repository URL | git remote set-url origin https://github.com/Pratiklahamge/MyRepo.git |
Inspection & Comparison
Command | Description | Example |
git log | View commit history | git log |
git log --summary | View detailed commit history | git log --summary |
git log --oneline | View a brief commit history | git log --oneline |
git diff [source-branch] [target-branch] | Preview changes between branches | git diff dev main |