Git Branching with Learn Git Branching
Git with Learn Git Branching
In this section I am going to revise my git skill with https://learngitbranching.js.org/
1: Introduction to Git commit
Task 1
Note
We can commit the changes in git with following command.
1
git commit -m "commit remark"
Challenge
In the given challenge we need to make two commit.
Goal of task
Solution
With commit command for twice we solved the challenge.
1
2
git commit
git commit
Successfully solved Task 1
Task 2
Note
We can create the new branch with following command
1
git branch "Branch Name"
We can change our branch with following command
1
git checkout "Branch Name"
Challenge
Challenge
Solution
Step 1:
We made new branch with name bugFix
.
1
git branch bugFix
Step 2:
We move into our newly created branch bugFix
.
1
git checkout bugFix
Task 2 solved
Task 3
Note
We can merge two branches in git with git merge
. Say, we have two branches main
and branchtobemerged
. Now, before we merge branchtobemerged
into main
. We should make proper commit on both branch. Then we can merge simply with
1
git merge branchtobemerged
Challenge
Solution
Steps 1:
Made a new branch bugFix
and get into that branch. Then we made a commit in that branch.
1
2
3
git branch bugFix
git checkout main
git commit
Step 2:
We come back to main and made a commit
1
2
git checkout main
git commit
Step 3:
We merged bugFix
into main
.
1
git merge bugFix
Solved 3rd challenge of Introduction to Git commit
Task 4
Note
Rebase is the technique to align the two different branch into same commit line. For more clarity visit https://www.youtube.com/watch?v=0chZFIZLR_0 .
If we have to rebase a targetbranch
to main
branch, we can first checkout or move to main and run following command.
1
git rebase targetbranch
Challenge
Solution
Step 1: Create a branch bugFix
, checkout to it and make a commit
1
2
3
git branch bugFix
git checkout bugFix
git commit
Step 2: Checkout main
branch and make a commit
1
2
git checkout main
git commit
Step 3: Checkout bugFix
and rebase onto main
1
2
git checkout bugFix
git rebase main
2: Ramping Up
Task 1: Detach yo’ HEAD
Note:
https://www.youtube.com/watch?v=HvDjbAa9ZsY
To detach head from branch to commit, we can checkout to commit
1
git checkout commithash
Question
Solution
Since, we our head is at bugFix
and its commit hash is C4
. So, to solve it we need to checkout on its commit hash. We can do so with following command.
1
git checkout C4
Task 2: Relative Refs (^)
Note:
In Git, relative references, often denoted by the caret symbol (^
), are used to specify commits relative to other commits. HEAD^
refers to the commit before the current HEAD
(i.e., the parent commit of the current commit). Instead of head we can also go with main or other branches. E.g.
1
git checkout main^
Question
Solution
Here, we need to detach head to C3
. For that, we can
1
git checkout C3
Task 3: Relative Refs #2 (~)
Note
It might be tedious to type ^
several times, so Git also has the tilde (~
) operator.
We can undo the changes in git even by erasing the history with
1
git branch -f branchname commit_hash
Question
Solution
Step 1: point main brach to C6 commit
1
git brach -f main C6
Step 2: Point bugFix branch to C0
1
git branch -f bugFix C0
Step 3: Detach head on C1
1
git checkout C1
Task 4: Reversing Changes in Git
Note
There are two primary ways to undo changes in Git – one is using git reset
and the other is using git revert
. The main difference between them is git reset
actually remove the history of commit but git revert
just make another commit.
Question
Solution
Step 1: Reset c1
1
git reset c1
Step 2: Checkout to pushed branch
1
git checkout pushed
Step 3: Now, revert to pushed
1
git revert pushed
3. Moving Work Around
Task 1: Cherry-pick Intro
Note
When we have to make a series of commit by picking specific commit we can use git cherry-pick.
1
git cherry-pick c1 c2 c3
Question
Solution
Its more straight forward.
1
git commit c3 c4 c7
Task 2: Interactive Rebase Intro
Note:
Git has UI based interactive rebase feature that allows us to alot from UI. We can use following command to access interactive rebase
1
git rebase -i c1
Question
Solution
Simply, go with git rebase with interactive flag i.e -i
.
1
git rebase -i c1
Then, simple omit c2
, then arrange c3
,c5
and c4
then confirm.
final stage before confirm
4. A Mixed Bag
Task 1: Grabbing Just 1 Commit
Note
Both git cherry-pick
and git rebase -i
can be used to play with specific commits.
Question:
Solution
Here, we can solve this challenge with git cherry-pick
and git rebase -i
. Since, we are already clearly visualize the structure I preferred wot go with git cherry-pick
.
Step 1: Checkout to main
1
git checkout main
Step 2: Cherry-pick to c4
1
git cherry-pick c4
Taks 2: Juggling Commits
Note:
- We can rebase the branches in more flexible way with interactive mode
1
git rebase -i commithash
- We can point a branch to a commit.
1
git branch -f targetbranch destinationcommit
Question:
Solution:
Step 1. Rebase the branch in interactive
1
git rebase -i c1
arrangement of braches for rebase at step 1
Step 2: Rebase C2’ only
1
git rebase c3'
arrangement of braches for rebase at step 2
Step 3: Rebase at c1
1
git rebase -i c1
arrangement of braches for rebase at step 3
Step 4: To achieve goal we need to point main to caption.
1
git branch -f main caption
finally solved
Task 3: Juggling Commits #2
Note
We can point a branch to a commit.
1
git branch -f targetbranch destinationcommit
Cherry-pick is git flexible way to rebase
1
git cherry-pick targetcommit1 targetcommit2
Question
Solution
Checkout to main and cherry-pick to
c2
1 2
git checkout main git cherry-pick c2
Again checkout to
c1
and cherry-pick toc2’
andc3
.1 2
git checkout c1 git cherry-pick c2' c3
Finally, point main to
c3’
1
git branch -f main
Alternative solution
1
2
3
4
git checkout main
git cherry-pick C2
git commit --amend
git cherry-pick C3
Task 4: Git Tags
Note:
We can tag a special commit.
1
git commit version1 targetcommithash
Question:
Solution:
Tag
c1
withv0
andc2
withv1
1 2
git tag v0 c1 git tag v1 c2
Checkout to
c2
1
git checkout c2
Task 5: Git Describe
Note:
git describe
is a command in Git that provides a human-readable name for a commit. It uses the most recent tag in your repository to create a description that includes the tag name, the number of commits since that tag, and the abbreviated commit hash. Its output is in <tag>-<number_of_commits>-g<commit_hash>
format
1
2
git describe commithash
v1.0-5-gabc1234
Question:
Solution:
Try git describe with any commit
1
git describe c1
Checkout to
bugFix
and make a commit1 2
git checkout bugFix git commit
5. Advanced Topics
Task 1: Rebasing over 9000 times
Note:
Question:
Solution:
- Rebase
bugFix
branch to main then side tobugFix
, thenanother
toside
. Then, point main to latest commit.
1
2
3
4
git rebase main bugFix
git rebase bugFix side
git rebase side another
git branch -f main another
Task 2: Multiple parents
Note:
We can combine the modifiers like HEAD~^2~
1
git checkout HEAD~^2~
Question:
Solution:
Checkout to
c2
1
git checkout HEAD~^2~
Point bugWork branch to c2 commit
1
git branch -f bugWork
Checkout to main to accomplish the level
1
git checkout main
Task 3: Branch Spaghetti
Note:
Question:
Solution:
- Checkout to
c1
then cherry-pickc4
,c3
andc2
Again checkout to
c2
then cherry-pickc5
c4’
c3’
c2’
1 2
git checkout c1 git cherry-pick c5 c4' c3' c2'
Then point respective commit with branch name.
1 2 3 4 5
git branch -f two git checkout c2 git branch -f three git checkout c2' git branch -f one
Finally, completed the challenges related to local branches.