Git Branching
Git branching allows multiple people to work on different parts of a project simultaneously without interfering with each other. It's like having separate workspaces for different tasks while still keeping the main project safe and stable.
Git Revert and Reset
Git Revert:
Git Revert is like a "Ctrl+Z" for your Git history. It undoes a specific commit by creating a new commit that reverses the changes made in the target commit.
Example: Let's say you have this commit history:
A -> B -> C -> D
If you want to undo commit C, you would use
git revert C
, which creates a new commit that undoes the changes in C:A -> B -> C -> D -> E
Now, commit E reverses the changes made in commit C.
Git Reset:
Git Reset is more like a time machine that lets you move the branch pointer to a specific commit, either discarding commits after it or keeping them in your working directory.
Example: Let's say you have this commit history:
A -> B -> C -> D
If you want to move your branch back to commit B and discard commits C and D, you would use
git reset --hard B
:A -> B
Now, commits C and D are gone from your branch history, and your working directory matches the state of commit B.
In Brief: Git Revert creates a new commit to undo a specific commit, while Git Reset moves your branch pointer to a different commit, potentially discarding commits in the process. Revert is safer for shared branches, while Reset should be used with caution, especially on shared branches because it can rewrite history.
Git Rebase and Merge
Git Merge:
Think of Git Merge as combining two different paths into one.
Example: Imagine you and your friend are working on a project. You both create separate branches to work on different features. When your friend finishes their work, you want to bring their changes into your branch.
You are on your branch:
git checkout your-branch
Merge your friend's changes into your branch:
git merge friend-branch
Git will automatically create a new commit that combines your work and your friend's work. It's like merging two roads into one.
Git Rebase:
Think of Git Rebase as rewriting history.
Example: You are working on a feature branch, and while you are working, your main branch (usually 'master' or 'main') has received new updates from others. You want to incorporate these updates into your branch and make it look like you started working on your branch after these updates.
You are on your feature branch:
git checkout feature-branch
Rebase your branch onto the main branch:
git rebase main
Git will move your branch's changes on top of the latest changes from the main branch. It's like rewriting your story as if you started from the latest point in time.
In Brief:
Git Merge combines changes as-is, creating a new merge commit.
Git Rebase rewrites your branch's history to incorporate changes from another branch, making it appear as if you started after those changes.
Kind Note: Use Git Merge when you want to keep a clear history of separate branches merging together. Use Git Rebase when you want a linear and clean history by incorporating changes from another branch into your own.
What is Git Stash?
Imagine you're working on a project, making changes to your code, and suddenly your boss asks you to switch to a different task or fix an urgent bug. You're not done with your current work, but you need to put it aside temporarily and work on something else. This is where Git Stash comes in handy.
Git Stash is like a secret hiding place for your code changes. It allows you to save your ongoing work in a safe spot, so you can switch to a different task without losing your progress.
Here's how it works with a simple example:
Example:
You're working on a feature in your project, and you've made some changes to the code files.
Your boss asks you to fix a critical bug that can't wait, but you don't want to commit your half-done work.
Instead, you run the Git Stash command:
git stash
This command takes all your changes and "stashes" them away in a hidden area.
Now, you can switch to a different branch or task and fix the bug as requested.
Once the bug is fixed, you want to go back to your feature work. You can retrieve your stashed changes using:
git stash pop
This command takes the changes out of the stash and reapplies them to your code.
By using Git Stash, you can easily switch between tasks without worrying about losing your work. It's like putting your code changes in a temporary box and then taking them out when you're ready to continue.
What is Cherry-pick?
It allows you to select and apply specific changes (or "commits") from one branch to another. Imagine it like picking only the cherries you want from a tree without taking the whole tree.
Here's a simple example:
Create a Scenario: Imagine you're working on a software project with your team, and there are two branches in your Git repository:
main
andfeature
.Making Commits: You and your team have been working on different features in separate branches. In the
feature
branch, you've made several commits to add a new feature, such as adding a login page, a registration form, and a dashboard.Cherry-Picking: Now, let's say your team decides that they want to add the login page and the registration form to the
main
branch but not the dashboard. You can use cherry-pick to do this.First, you identify the specific commits that contain the changes for the login page and the registration form in the
feature
branch.Then, you use the cherry-pick command to apply only those selected commits to the
main
branch.
git checkout main # Switch to the main branch
git cherry-pick <commit-hash1> <commit-hash2>
<commit-hash1>
and<commit-hash2>
represents the unique identifiers of the commits you want to pick.
- Result: After running the cherry-pick command, the changes from the selected commits (login page and registration form) are applied to the
main
branch, but the changes for the dashboard remain in thefeature
branch. It's like picking only the cherries (changes) you wanted from thefeature
branch and adding them to themain
branch.
Cherry-picking is useful when you want to bring specific changes from one branch into another without merging the entire branch. It allows you to be selective about which changes you incorporate into your codebase, helping you maintain a cleaner and more organized version control history.