Working with Git is a skill that every programmer must have. To work in a project that using Git, you simply just need have knowledge about commit, branch, merge request,…However, git is more than that.
Three-tree architecturePermalink
Git revert vs Git resetPermalink
-
git revert is a command used in Git to undo a specific commit while preserving the commit history. Unlike git reset, which alters the commit history, git revert creates a new commit that undoes the changes introduced by a previous commit.
- For example, we have 3 commit in repo like this:
- Then run: git revert e4f5g6h, so in repo commit history, it will have a latest commit with default content is: Revert “Add new feature”.
- Meanwhile, with “git reset” provide us 3 mode:
- soft: modified file won’t change, differences will be staged for commit. In other words, it keep changes in the staging area.
- mixed (default mode): modified file won’t change, differences won’t be staged. In other words, it keep changes in the working directory but remove them from the staging area.
- hard: files will be reverted to the state of the selected commit, and any local changes will be lost. In other words, it will remove commits, staging, and working directory changes (and of course, it’s DANGEROUS!).
Git stashPermalink
- git stash temporarily saves changes that you haven’t committed yet, allowing you to switch branches or pull updates without committing incomplete work. Later, you can restore the changes when needed.
-
To stash uncommited changes: git stash
-
To see stash list: git stash list
-
To reapplies the most recent stash but keeps it in the stash list: git stash apply
-
After apply it, and you want to remove this stash from stash list: git stash pop
-
If you have mutiple stash and you want to apply a specific stash: git stash apply stash@{1}
-
To delete a specific stash: git stash drop stash@{0}
- To clear all stashes: git stash clear
Git cherry-pickPermalink
- git cherry-pick is a Git command used to apply the changes introduced by a specific commit (or commits) from one branch onto another branch.
- Basis Syntax: git cherry-pick <commit-hash>
- Example workflow: You’re on the main branch and you want to apply a commit from feature-branch.
- Cherry-pick the commit: git cherry-pick <commit-hash>
- You can cherry-pick multiple commits by listing them: git cherry-pick abc1234 def5678
Git rebase vs Git mergePermalink
- This topic is frequently appear in an interview. Let’s go to clear it!
- Both git rebase and git merge are used to integrate changes from one branch into another, but they do it in different ways.
- git merge:
- Definition: Merges the changes from one branch into another by creating a new “merge commit.”
- How it works:
- It takes the content of two branches (e.g., feature and main) and combines them into a new commit.
- The merge commit has two parent commits (the latest commit of each branch).
- Preserves history: It keeps all the history of both branches intact, showing that a merge occurred.
- git rebase:
- Definition: Rebase takes the changes in one branch and re-applies them on top of another branch, effectively rewriting history.
- How it works:
- It “moves” the entire branch to start from a new commit.
- Rebasing re-applies your commits on top of the current branch, avoiding the merge commit.
- Rewrites history: The commit history becomes linear and clean.
- git merge:
-
Comparison Summary:
Feature git merge
git rebase
History Preserves full branch history Rewrites history (linear history) Merge Commit Creates a merge commit No merge commit, just applies changes linearly Safety Safer, non-destructive Riskier, can cause issues if misused Use Case When you want to preserve history and track merges explicitly When you want a clean, linear history (usually for feature branches) Conflict Resolution Happens once at merge commit Can happen multiple times during rebase