15 ‐ Git - CloudScope/DevOpsWithCloudScope GitHub Wiki
Git Notes for DevOps Engineer
1. Introduction to Git
Git is a distributed version control system (VCS) that tracks changes in source code during software development.
Git allows multiple developers to work on a project simultaneously without interfering with each other’s work.
Git is widely used in DevOps workflows for managing code repositories, collaboration, and integration with Continuous Integration/Continuous Deployment (CI/CD) pipelines.
2. Git Basics
Repository: A directory that contains all the files and the history of changes to those files.
Commit: A snapshot of the project at a particular point in time.
Branch: A pointer to a specific commit, used for parallel development.
Clone: Copying a repository from a remote server to your local machine.
Remote: A version of the repository hosted on a server, such as GitHub, GitLab, or Bitbucket.
Merge: Combining changes from different branches into a single branch.
Pull Request (PR): A request to merge changes from one branch to another, often used in collaboration platforms.
3. Basic Git Commands
git init: Initialize a new Git repository.
git clone <repo_url>: Clone an existing remote repository to your local machine.
git status: Show the status of changes in your working directory.
git add : Stage a file (or files) to be committed.
git commit -m "message": Commit changes to the local repository with a message describing the change.
git push: Push local commits to a remote repository.
git pull: Fetch and merge changes from a remote repository into the local branch.
git fetch: Download objects and refs from another repository without merging.
git log: View the commit history of a repository.
git diff: Show changes between commits, branches, or the working directory and the index.
4. Branching & Merging
Creating a Branch: git branch <branch_name> creates a new branch.
Switching Branches: git checkout <branch_name> switches to an existing branch. Or use git switch <branch_name>.
Merge: git merge <branch_name> merges changes from another branch into the current branch.
Rebase: git rebase <branch_name> moves or combines commits from one branch to another, typically used to maintain a clean commit history.
View Remotes: git remote -v displays the remotes associated with the repository.
Push to Remote: git push origin <branch_name> pushes changes to a remote branch.
Pull from Remote: git pull origin <branch_name> fetches and merges changes from a remote branch.
6. Git Workflow in DevOps
Feature Branch Workflow: Developers create branches for each new feature or bug fix, and merge them back into the main branch (e.g., master or main) when complete.
Forking Workflow: A developer forks a repository, works on a feature in their fork, and submits a pull request to the main repository.
Gitflow Workflow: A branching model that defines specific branches for features, releases, and hotfixes. It uses branches like develop, feature, release, hotfix, and master.
7. Git in CI/CD
Git plays a central role in CI/CD pipelines by integrating with build and deployment automation tools (e.g., Jenkins, GitLab CI/CD, CircleCI, etc.).
Continuous Integration: Developers frequently commit code to the repository to integrate changes regularly. Tools automatically build, test, and validate the changes.
Continuous Deployment: After successful integration and testing, changes are automatically deployed to production or other environments.
Triggers: Git can trigger CI/CD pipelines on events like pushing to a branch or creating a pull request.
8. Git and Automation
Webhooks: Webhooks allow Git repositories to trigger automated actions in CI/CD tools. For example, a webhook can notify Jenkins to start building after a commit is pushed.
Hooks: Git hooks are scripts that run automatically on certain events. Common examples include:
pre-commit: Runs before a commit is made (can be used to check coding standards or run linters).
post-commit: Runs after a commit is made (can be used to notify a system or trigger other actions).
pre-push: Runs before pushing changes to a remote repository.
9. Git Best Practices for DevOps Engineers
Commit Frequently: Commit small, meaningful changes often rather than large, infrequent updates.
Write Descriptive Commit Messages: Follow a consistent format for commit messages. Example: [TYPE] Short description of the change.
Branching Strategy: Adopt a branching model (like GitFlow or Feature Branch Workflow) to manage development and releases.
Use Pull Requests: Encourage code reviews through PRs. This helps maintain quality, documentation, and approval workflows.
Avoid Force Pushes: Be cautious with git push --force, as it can overwrite changes in a shared repository.
Tag Releases: Use Git tags to mark releases or milestones (e.g., git tag v1.0.0).
Keep the Master/Main Branch Clean: Ensure that the master or main branch always contains deployable code.
10. Advanced Git Concepts
Git Submodules: Use submodules to manage external repositories as part of your project. This can be useful for managing third-party libraries or dependencies.
Git Cherry-Pick: git cherry-pick <commit> allows you to apply a specific commit from another branch.
Stashing Changes: git stash saves uncommitted changes to be applied later without committing them.
11. Troubleshooting Git
Revert a Commit: git revert <commit_hash> creates a new commit that undoes the changes from a specified commit.
Reset: git reset <commit_hash> moves the HEAD pointer to a previous commit, optionally discarding changes (use with caution).
Resolve Merge Conflicts: When merging branches, conflicts may arise. Git marks the conflicts in the files. Use a merge tool or manually resolve them, then commit the changes.
Recover Lost Commits: If you lose a commit, you can usually recover it with git reflog.
12. Git Security and Access Management
SSH Keys: Use SSH keys instead of passwords for more secure authentication when interacting with remote repositories.
Access Controls: Limit who can push to certain branches (e.g., using branch protection rules in GitHub/GitLab).
Two-Factor Authentication: Enable 2FA for remote Git repositories to enhance security.