Day-6 | Git & Github Advanced
Welcome to Day 6 of my DevOps learning journey! Today, we're exploring advanced Git and GitHub concepts that are essential for effective version control and collaboration. These skills are crucial for developers and teams to manage codebases, streamline workflows, and maintain clean project histories. Let's dive into these advanced topics!
Agenda
Branching Strategies
Revert & Reset
Merge & Rebase
Cherry-Pick
Stash
Squash
Merge Conflicts
Branching Strategies
Branching in Git allows you to create separate lines of development for different features, bug fixes, or experiments. Here are some common branching strategies:
Feature Branching: Each feature is developed in its own branch. Once complete, it’s merged back into the main branch, often through a pull request. This keeps the main branch stable.
Gitflow: A more structured approach with separate branches for development (
develop
), production (master
), features, releases, and hotfixes. This strategy is useful for projects with strict release cycles.Trunk-Based Development: Developers work on the main branch (
trunk
) and create short-lived branches for features or fixes. This approach encourages frequent integration and reduces merge conflicts.
Revert & Reset
Revert:
Purpose: Undo changes introduced by a specific commit by creating a new commit.
Usage:
git revert <commit>
creates a new commit that reverses the changes of the specified commit. This is safe for shared branches as it doesn't alter the commit history.git revert abc1234
Reset:
Purpose: Move the HEAD and branch pointer to a specific commit, altering the commit history.
Types:
git reset --soft <commit>
: Resets the HEAD to the specified commit and keeps the changes staged.git reset --hard <commit>
: Resets the HEAD and working directory to the specified commit, discarding all changes.git reset --soft HEAD~1 git reset --hard HEAD~1
Merge & Rebase
Merge:
Purpose: Combine changes from one branch into another.
Types:
Fast-forward Merge: Moves the branch pointer forward if there are no divergent commits.
Three-way Merge: Creates a new merge commit when there are divergent histories.
git checkout main git merge feature-branch
Rebase:
Purpose: Reapply commits on top of another base branch, effectively "replaying" changes.
Usage:
git rebase <branch>
replays the commits of the current branch onto<branch>
. It’s useful for maintaining a linear commit history but should be used with caution on shared branches to avoid rewriting history.git checkout feature-branch git rebase main
Cherry-Pick
Purpose: Apply changes from specific commits to another branch.
Usage:
git cherry-pick <commit>
applies the changes from the specified commit onto the current branch. This is useful for selectively applying bug fixes or features.git cherry-pick 123abc
Stash
Purpose: Temporarily save changes that are not yet ready to be committed.
Usage:
git stash save "message"
saves the changes with a message.git stash pop
applies the stashed changes and removes them from the stash list.git stash list
shows all stashed changes.git stash git stash pop git stash show
Squash
Purpose: Combine multiple commits into a single commit, useful for cleaning up commit history before merging.
Usage:
git rebase -i HEAD~n
allows interactive rebase, wheren
is the number of commits to squash.git checkout main git merge --squash feature-branch git commit -m "Merged feature-branch with squash"
Merge Conflicts
Purpose: Occur when Git cannot automatically merge changes.
Resolution:
Git marks conflicting areas in files, which must be manually resolved.
After resolving, use
git add <file>
to mark the conflict as resolved, followed bygit commit
to finalize the merge.git add resolved-file git commit -m "Resolved merge conflict in resolved-file"
Conclusion
Today’s deep dive into advanced Git and GitHub topics has enhanced our understanding of sophisticated version control techniques. These skills are vital for managing complex codebases and ensuring smooth collaboration in development teams. As we continue our journey, mastering these advanced tools will prepare us for more efficient and effective software development practices.
Stay tuned for more updates and happy coding!
Subscribe to my newsletter
Read articles from Dhruv Rajvanshi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by