Mastering Git and GitHub: Advanced Techniques for Streamlined Version Control
🌿Git Branching
This feature is provided in Git so that developers can create code related to different functionalities on separate branches.
This helps the development team in creating the code in an uncluttered way. Later this code can be merged with the master branch.
The default branch of git is "Master"
Let Gets start with Practicals :
git init: Initializes an empty Git repository in the current directory.
So, the Default master branch created
touch t1.txt - Creates an empty file named "t1.txt" in the current directory.
git init .
touch f1.txt
git add .
git commit -m "a"
touch f2.txt
git add .
git commit -m "b"
As we created 3 empty files, is whether they are created or not.
ls
Check with the commit
git log --oneline
Create a new branch as the dev branch
git branch dev
Go to the dev branch
git checkout dev
As we created the files and committed them to the master branch: similarly, created files on the dev branch and committed it
touch t1
git add .
git commit -m "c"
touch t2.txt
git add .
git commit -m "d"
touch t3.txt
git add .
git commit -m "e"
Again, check with the log: so you get an idea about it
git log --oneline
Now, switch to the master branch
git checkout master
Create a new file on the master branch
touch f3.txt
git add .
git commit -m "f"
Check with the git log
git log --oneline
Here's a breakdown:
"HEAD" is a symbolic reference to the current branch or commit.
"->" simply indicates that "HEAD" is pointing to something.
"master" is the name of the branch to which "HEAD" is currently pointing.
in short,
"HEAD -> master" tells you that you are currently working on the "master" branch in your Git repository.
Note: Above here are committed on master branch are a, b & f
after merging it should be in one line. so will check by following the way
Now we will do the merging
🌿Git Merge
Syntax: git merge target-branch_name
as we indicated we are in the "dev" branch, so to merge one branch(source branch) to the master(target branch) branch you should have to be in the master branch.
or when you want to merge the first branch(source branch) into another branch(target branch) then you have to be present in the second branch.
so now, go to the master branch (target branch)
git checkout master
git merge dev
Check with the log and files.
git log --oneline
You can see above all the merges are in one line (Which means commits are copied from "dev branch to master").
again, you can check by following way,
ls
Congratulations ! 🎉 Now learn about branching and merging.
🌿Git Rebase
This is called a fast-forward merge.
The commits copy from the child branch are added to the top of the master branch.
This is helpful when we want code from a branch to be reflected as the latest working version on the master.
initialize the new repository, files commit each file
git init
touch f1
git add .
git commit -m "a"
git init
touch f2
git add .
git commit -m "b"
ls
Similarly, Create a new branch having a name (server), and files with commit it.
git branch server
Go to the server branch
git checkout server
git init
touch t1
git add .
git commit -m "c"
touch t2
git add .
git commit -m "d"
git init
touch t3
git add .
git commit -m "e"
git log --oneline
so, go to the master branch
git checkout master
touch f3
git add .
git commit -m "f"
git log --oneline
last time when we merged it all in one line commit, meant in a linear way in merge.
but now we want to latest commit on the top of the branch that time rebase is used.
so, which branch do we need to commit as the latest, on that branch you have to be present. (Go to the server branch)
git checkout server
git rebase master
git checkout master
git merge server
git log --oneline
You can see rebase is done.
🌿Differences in Merge and Rebase
Parameter | Merge | Rebase |
Functionality | The commits copied from the first branch(source branch) into another branch(target branch) | The commits copy from the child/source branch is added to the top of the master branch. |
Commands | git merge source_branch | git rebase target_branch |
Ease of Use | Straightforward to understand | Requires some reordering skills |
Preserves History | Preserves branch history | Creates a linear commit history |
🌿Git Revert and Reset
Git Revert: The "git revert" command is used to undo a specific commit by creating a new commit that undoes the changes introduced by the target commit.
It's a safe way to reverse changes without altering the project's history. Git revert is typically used for shared branches, where altering the history could cause problems for other developers.
Suppose you have a commit history like this:
A - B - C - D
You want to revert commit C, which introduced some unwanted changes. To do this, use git revert c
This will create a new commit that undoes the changes made in commit C. Your commit history will now look like this:
A - B - C - D - E
Commit E is the result of the git revert operation, effectively canceling out the changes introduced in commit C.
Git Reset: The git reset command, on the other hand, is a more powerful and potentially destructive tool. It allows you to move the branch pointer to a different commit, effectively erasing commits from the branch's history.
It's typically used for local branches when you want to rework your commits before pushing them to a shared repository.
Example: Consider the same commit history as before:
A - B - C - D
If you want to remove commit C entirely and make it look like it never happened, use git reset
git reset --hard B
This command moves the branch pointer back to commit B and discards commit C and D. Your commit history becomes:
A - B
Note: Be cautious when using it as git reset --hard permanently deletes commits and their changes.
👉When to Use Git Revert and Reset:
Git Revert: Use git revert when you want to undo a commit without altering the project's history. It's suitable for shared branches and commits that have been pushed to a remote repository.
Git Reset: Use git reset when you need to rework your local branch's history before pushing it to a shared repository. It's a powerful tool, but use it with caution, especially on shared branches.
I hope you enjoy the blog post!
If you do, please show your support by giving it a like ❤, leaving a comment 💬, and spreading the word 📢 to your friends and colleagues 😊