Advance Git and GitHub for DevOps
Branching:
Git branching allows you to diverge from the main line of development and continue work without affecting the main codebase.
Creating a branch creates a pointer to a specific commit, allowing you to work on new features or fixes independently.
Once the work on a branch is complete, it can be merged back into the main branch.
Revert:
git revert
creates a new commit that undoes a previous commit by applying the inverse of the changes. It's a safe way to undo changes without altering the repository's history.It doesn't remove the commit; instead, it adds a new commit with the changes reverted.
Reset:
git reset
is used to reset the current branch to a specific state. It can be a "soft," "mixed," or "hard" reset.Soft reset moves the branch pointer to a previous commit, leaving changes staged.
Mixed reset moves the branch pointer and unstages the changes, but leaves them in the working directory.
Hard reset moves the branch pointer and discards all changes back to the specified commit.
Rebase:
git rebase
integrates changes from one branch into another by moving or combining a sequence of commits to a new base commit.It helps maintain a linear project history by placing the commits on top of the destination branch's tip.
Rebase is useful for a cleaner, more linear history but should be used cautiously when working on shared branches to avoid disrupting other collaborators.
Merge:
git merge
combines changes from different branches into one. It creates a new commit that represents the combined changes of the merged branches.Git can perform different types of merges, such as fast-forward merges or recursive merges, depending on the branch histories.
Absolutely! Here's a breakdown of Git commands and their application using sample code snippets.
1. Branching:
# Create a new branch
git branch new-feature
# Switch to the newly created branch
git checkout new-feature # or "git switch new-feature" in Git versions 2.23+
# Make changes, commit them
git add .
git commit -m "Implementing new feature"
# Switch back to the main branch
git checkout main # or "git switch main" in Git versions 2.23+
# Merge changes from the new feature branch into the main branch
git merge new-feature
2. Revert:
# Identify the commit to revert
git log # Find the commit hash you want to revert
# Revert a specific commit
git revert <commit_hash>
3. Reset:
# Soft reset (moves the HEAD pointer, keeps changes in staging)
git reset --soft <commit_hash>
# Mixed reset (moves the HEAD pointer, unstages changes)
git reset --mixed <commit_hash>
# Hard reset (moves the HEAD pointer, discards changes)
git reset --hard <commit_hash>
4. Rebase:
# Start the rebase
git checkout new-feature
git rebase main
# Resolve any conflicts, if encountered
# Continue the rebase after conflicts are resolved
git rebase --continue
# Finish the rebase
git checkout main
git merge new-feature # Fast-forward merge
5. Merge:
# Merge changes from a branch into the current branch (e.g., main)
git merge new-feature
Subscribe to my newsletter
Read articles from Vivek Chaudhary directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by