Wednesday 13 March 2024

The Git & Github Bootcamp Part 2- Master on essentials and the tricky bits: rebasing, squashing, stashing, reflogs, blobs, trees, & more!


Working With Branches

1. Introducing Branches

Branches in Git allow you to diverge from the main line of development and work independently on different tasks without affecting each other. It’s like working on a different copy of the project which can later be merged back into the main project.

2. The Master Branch (Or Is It Main?)

Traditionally, the default branch in Git repositories was called “master.” However, there’s a shift towards using “main” as the default branch name for new repositories. It’s important to know the name of your default branch, as it’s the base for new branches and often serves as the stable version of your project.


3. What On Earth Is HEAD?

HEAD is a reference to the last commit in the currently checked-out branch. It’s like a pointer telling Git where you currently are in your project’s history. When you switch branches, HEAD moves to the last commit of the checked-out branch.

4. Viewing All Branches With git branch

To see all the branches in your repository:

git branch

This command lists all branches in your repository and marks the currently active branch with an asterisk (*).

5. Creating & Switching Branches

To create a new branch and switch to it:

git switch -c feature-x

This command creates a new branch named “feature-x” and switches to it. The -c flag stands for “create.”

6. More Practice With Branching

Let’s say you want to add a new feature to your project but keep it separate from the main project until it’s ready.

  • Create a new branch for the feature: git switch -c new-feature
  • Make changes in your project.
  • Stage and commit these changes to the “new-feature” branch.

7. Another Option: git checkout Vs. git switch

  • git checkout is the older way to switch branches: git checkout new-feature
  • git switch is a newer, more intuitive command introduced specifically for switching branches: git switch main
  • Both accomplish similar tasks, but git switch is recommended for switching branches because it’s clearer and more focused.

8. Switching Branches With Unstaged Changes?

If you try to switch branches with unstaged changes that conflict with the branch you’re switching to, Git will prevent the switch to avoid overwriting those changes. You can:

  • Stash your changes: git stash
  • Switch branches: git switch other-branch
  • Pop your stash after switching: git stash pop

9. Deleting & Renaming Branches

  • To delete a branch: git branch -d feature-x (if it’s been merged) or git branch -D feature-x (to force delete even if not merged).
  • To rename a branch: First switch to the branch, then git branch -m new-name

10. Branching Exercise

  • Create a branch named “experiment”: git switch -c experiment
  • Add a file named “experiment.txt” and commit it: touch experiment.txt, git add experiment.txt, git commit -m "Start experiment"
  • Switch back to the main branch: git switch main
  • Delete the “experiment” branch after completing your experiment: git branch -d experiment

Through these exercises, you’ve learned how to manage branches in Git, enabling you to work on different parts of your project simultaneously without interference. Branches are fundamental to Git’s flexibility, allowing for parallel development and easy feature integration.

Continuing with our “SampleProject,” we’ll now explore how to merge branches in Git. Merging is a critical skill for combining changes from different branches into a single branch, allowing for collaborative development and feature integration.

Merging Branches

1. Intro to Merging

Merging is the process of taking the changes from one branch (source) and integrating them into another branch (target). It’s a common practice for incorporating feature development or bug fixes into the main project line.

2. Performing A Fast Forward Merge

A fast forward merge occurs when the target branch’s current commit is in the history of the source branch. Git just moves the target branch forward to the source branch’s latest commit.

  • If you’ve been working on a feature in a branch called feature-x and want to merge it back into main, first switch to the main branch:
    git switch main
    
  • Then merge feature-x into main:
    git merge feature-x
    

3. Visualizing Merges

To visualize the merge process, you can use Git’s graphing tools or a GUI like GitKraken. From the command line:

git log --graph --oneline --all

This command shows a text-based graph of your commits, making it easier to understand the branch and merge history.

4. Generating Merge Commits

If the branches have diverged, Git will generate a merge commit when you merge. This commit has two parents, each pointing to the previous commits of the branches being merged.

  • After ensuring you’re on the target branch (git switch main), merge another branch (e.g., git merge feature-y) to create a merge commit.

5. Oh No! Merge Conflicts!

Merge conflicts happen when Git can’t automatically merge changes because the same lines were modified differently on the branches being merged. Git will halt the merge and ask you to resolve the conflicts manually.

6. Resolving Merge Conflicts

To resolve merge conflicts, you need to edit the files to decide which changes to keep. Conflicted lines are marked by Git, showing both versions separated by <<<<<<<, =======, and >>>>>>>.

  • Open the conflicted files in a text editor.
  • Decide which changes to keep or combine them as needed.
  • After resolving, mark the conflicts as resolved by staging the files: git add <filename>

7. Using VSCode To Resolve Conflicts

VSCode and other modern editors have built-in tools to help resolve merge conflicts visually:

  • Open the conflicted file in VSCode.
  • You’ll see options to accept the Current Change (from the target branch), Incoming Change (from the source branch), both changes, or compare changes.
  • After choosing the desired changes, save the file and stage the changes with Git.

8. Merging Exercise

Let’s practice with a simple merge scenario:

  • Create a new branch and switch to it: git switch -c hotfix
  • Make a small change, like fixing a typo in README.md, then stage and commit it.
  • Switch back to your main branch: git switch main
  • Merge the hotfix branch: git merge hotfix
  • If there are no conflicts, your merge is complete. Otherwise, resolve any conflicts as described and commit the merge.

Through this process, you’ve learned how to perform merges, visualize them, handle merge conflicts, and practice merging changes between branches. Merging is essential for combining work from different branches and maintaining a coherent project history.

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home