Advance Git & GitHub for DevOps Engineers: Part-2

Advance Git & GitHub for DevOps Engineers: Part-2

1. Git Stash: Temporarily Storing Uncommitted Changes

Git stash provides a convenient way to temporarily store uncommitted changes, allowing you to switch branches or perform other tasks without losing your work in progress.

Command:

git stash
git stash
git checkout main
# Work on the main branch
git stash pop

Explanation:

This command stashes the uncommitted changes in the current branch. The git checkout main command switches to the main branch, allowing you to work on it without losing the stashed changes. The git stash pop command retrieves the stashed changes and applies them to the current branch.

2. Git Cherry-Pick: Selecting Specific Commits

Git cherry-pick allows you to select and apply individual commits from one branch to another, providing a more granular approach to merging changes.

git cherry-pick <commit_hash>
git checkout <branch_name>
git cherry-pick 1234567

Explanation:

This command selects and applies the commit with the hash 1234567 from the <branch_name> branch to the current branch.

3. Understanding Merging and Rebasing

Merging and rebasing are two fundamental techniques for combining changes from one branch into another. While both achieve the goal of integration, they differ in their approach and impact on the repository history.

  • Merging: Merging creates a new merge commit, representing the point where the changes from two or more branches are combined. This results in a non-linear history, with multiple branches merged into the main line.

  • Rebasing: Rebasing rewrites the history of a branch by applying its commits on top of another branch's history. This creates a linear history where all changes appear as if they were made to the target branch.

Conflicts and Resolution

Conflicts arise when changes made to different branches affect the same file, causing Git to halt the merging or rebasing process. To proceed, these conflicts need to be manually resolved.

  1. Identifying Conflicts: The git status command reveals the files containing conflicts.
git status
  1. Examining Differences: The git diff command shows the differences between the conflicting versions, allowing you to identify the conflicting lines.
git diff <filename>
  1. Resolving Conflicts: Manually edit the affected file, choosing the desired changes from each conflicting version.
vim <filename>
  1. Adding Resolved Files: Once conflicts are resolved, use the git add command to mark the modified files as staged for committing.
git add <filename>

Choosing Merging or Rebasing

The choice between merging and rebasing depends on the desired outcome and the state of the branches.

  • Merging: Merging is suitable when preserving the complete history of the branches is important. It's also useful when integrating changes from multiple branches.

  • Rebasing: Rebasing is preferred when maintaining a linear history is crucial. It's also appropriate when rebasing a feature branch onto the main branch before merging.

Consider two branches, feature1 and feature2, which have diverged. Merging feature2 into feature1 may result in conflicts if both branches have modified the same file.

git checkout feature1
git merge feature2

If conflicts arise, follow the conflict resolution steps to manually resolve the conflicting lines. Once resolved, add the modified files and proceed with the merge.

git add <filename>
git commit -m "Merged feature2 into feature1"

Takeaway

Git stash, cherry-pick, merging, and rebasing are powerful tools for managing branches and integrating changes in Git repositories. Understanding their distinct functionalities and applications enables developers to navigate complex codebases, maintain a clean history, and ensure seamless collaboration.