What is Git Branching?
Git branching is a feature in Git that allows you to diverge from the main line of development and continue to do work without messing up that main line.
Git branching is like creating a copy of your project to work on without affecting the original.
Branching means you can create a copy of your project to work on a specific feature, fix a bug, or experiment with new ideas without affecting the main project.
What is Git Revert and Reset?
Git Revert
"Git revert" is used to undo the changes introduced by a specific commit by creating a new commit with the opposite changes. This method maintains the project history intact, making it clear what was changed and why it was reverted.
Use case: When you want to undo changes in a way that preserves the history of the repository.
linux$ git revert <commit-hash>
Git reset
"Git reset" is used to undo changes by moving the current branch pointer to a different commit.
Moves the branch pointer back to a previous commit, effectively discarding any commits made after that point.
Rewrites the commit history, which can be risky if others have already pulled your code.
There are three types of reset,
- Soft Reset (--soft): Moves the branch pointer to a different commit but leaves your working directory and the index (staging area) unchanged.
linux$ git reset --soft <commit-hash>
- Mixed Reset (--mixed): Moves the branch pointer to a different commit and resets the index, but leaves your working directory unchanged.
linux$ git reset --mixed <commit-hash>
- Hard Reset (--hard): Moves the branch pointer to a different commit and resets both the index and working directory to match that commit.
linux$ git reset --hard <commit-hash>
For a better understanding of Git Rebase and Merge, check out this article.
What is Git Rebase and Merge?
Git Merge
"Git merge" combines changes from one branch into another. It creates a new commit, called a "merge commit," which has two parent commits.
Suppose you want to merge two branch then first go to the main base branch and then try to merge branch that you want to merge, for e.g,
linux$ git checkout master # Switched to master branch
linux$ git merge feature-branch # Trying to merge branch feature-branch to main
Git Rebase
"Git rebase" moves or combines a sequence of commits to a new base commit. Instead of creating a merge commit, rebase rewrites the project history by creating new commits for each commit in the original branch.
When you want to incorporate changes from one branch into another without creating a merge commit, resulting in a cleaner project history.
linux$ git checkout feature-branch
linux$ git rebase master
For a better understanding of Git Rebase and Merge, check out this article.
Task 1 : Feature Development with Branches
Create a Branch and Add a Feature
- Add a text file called version01.txt inside the Devops/Git/ directory with “This is the first feature of our application” written inside.
linux/Devops/Git/Adv_git$ ls
version01.txt
linux/Devops/Git/Adv_git$ cat version01.txt
This is the first feature of our application.
- Create a new branch from "master".
linux/Devops/Git/Adv_git$ git branch dev
linux/Devops/Git/Adv_git$ git branch
dev
* master
- Commit your changes with a message reflecting the added feature.
linux$ git checkout dev
D README.md
Switched to branch 'dev'
linux$ git add version01.txt
linux$ git commit -m "Added new feature"
Push Changes to GitHub
- Push your local commits to the repository on GitHub.
/linux/Devops/Git/Adv_git$ git status
On branch dev
nothing to commit, working tree clean
linux/Devops/Git/Adv_git$ git push origin dev
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Writing objects: 100% (3/3), 300 bytes | 150.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'dev' on GitHub by visiting:
remote: https://github.com/ojasjawale/Adv_git/pull/new/dev
remote:
To github.com:ojasjawale/Adv_git.git
* [new branch] dev -> dev
Add More Features with Separate Commits
Update version01.txt with the following lines, committing after each change.
1st line: This is the bug fix in development branch
linux/Devops/Git/Adv_git$ git commit -am "Added feature2 in development branch"
[dev 4cf98e2] Added feature2 in development branch
1 file changed, 2 insertions(+)
linux/Devops/Git/Adv_git$ git log --oneline
4cf98e2 (HEAD -> dev) Added feature2 in development branch
63c03df (origin/dev) Added new feature
9f59996 (origin/master, origin/HEAD, master) Initial commit
- 2nd line: This is Gadbad code.
linux/Devops/Git/Adv_git$ echo "This is gadbad code" >> version01.txt
linux/Devops/Git/Adv_git$ git commit -am "Added feature3 in development branch"
[dev 04600e2] Added feature3 in development branch
1 file changed, 2 insertions(+)
linux/Devops/Git/Adv_git$ git log --oneline
04600e2 (HEAD -> dev) Added feature3 in development branch
4cf98e2 Added feature2 in development branch
63c03df (origin/dev) Added new feature
9f59996 (origin/master, origin/HEAD, master) Initial commit
- 3rd line: This feature will Gadbad everything from now.
linux/Devops/Git/Adv_git$ echo "This feature will gadbad everything from now" >> version01.txt
linux/Devops/Git/Adv_git$ git commit -am "Added feature4 in development branch"
[dev 5dc8e94] Added feature4 in development branch
1 file changed, 1 insertion(+)
linux/Devops/Git/Adv_git$ git log --oneline
5dc8e94 (HEAD -> dev) Added feature4 in development branch
04600e2 Added feature3 in development branch
4cf98e2 Added feature2 in development branch
63c03df (origin/dev) Added new feature
9f59996 (origin/master, origin/HEAD, master) Initial commit
Restore the File to a Previous Version
- Revert or reset the file to where the content should be “This is the bug fix in development branch”.
linux/Devops/Git/Adv_git$ cat version01.txt
This is the first feature of our application.
This is the bug fix in development branch
This is gadbad code
This feature will gadbad everything from now
linux$ git revert HEAD~2
linux/Devops/Git/Adv_git$ cat version01.txt
This is the bug fix in development branch
Task 2 : Working with Branches
Demonstrate Branches
- Create 2 or more branches and take screenshots to show the branch structure.
linux$ git branch stg
linux$ git branch prd
linux$ git branch
* dev
master
prd
stg
Merge Changes into Master
- Make some changes to the dev branch and merge it into master.
linux/Devops/Git/Adv_git$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
linux/Devops/Git/Adv_git$ git merge dev
Updating 9f59996..6f3f6fe
Fast-forward
README.md | 1 -
version01.txt | 1 +
2 files changed, 1 insertion(+), 1 deletion(-)
delete mode 100644 README.md
create mode 100644 version01.txt
Practice Rebase
- Try rebasing and observe the differences.
linux/Devops/Git/Adv_git$ git rebase master
Current branch master is up to date.
Connect Me
Follow me for more amazing content :)