GitHub Knowledge
Difference between Git Merge and Git Rebase?
Git Merge creates a new commit that combines the changes from two branches. This new commit, called a merge commit, has two parents (the tips of the branches being merged).
Git Rebase moves or combines a sequence of commits to a new base commit. Instead of creating a merge commit, it integrates changes by rewriting the commit history.
Example:
If main = B and feature branch is = C
after merge M = B+C
Example:
If main = B and feature branch is = C
after Rebase M = C
History Preservation: Merge preserves the full history of both branches, including all commits made in both branches.
Linear History: Rebase creates a linear, cleaner history by eliminating merge commits and replaying the feature branch commits on top of the target branch.
Merge Commit: A new commit is created that points to both branches as its parents, showing the branch history clearly.
Rewriting History: Commits are rewritten, which can change commit hashes. This makes it unsuitable for public/shared branches.
Common Git Command Use cases:
๐ ๐ข๐ญ ๐๐ข๐๐: Show file differences not yet staged.
๐ ๐ข๐ญ ๐๐จ๐ฆ๐ฆ๐ข๐ญ -๐ -๐ฆ "๐๐จ๐ฆ๐ฆ๐ข๐ญ ๐ฆ๐๐ฌ๐ฌ๐๐ ๐": Commit all tracked changes with a message.
๐ ๐ข๐ญ ๐๐จ๐ฆ๐ฆ๐ข๐ญ --๐๐ฆ๐๐ง๐: Modify the last commit.
๐ ๐ข๐ญ ๐ฌ๐ญ๐๐ญ๐ฎ๐ฌ: Show the state of your working directory.
๐ ๐ข๐ญ ๐๐๐ ๐๐ข๐ฅ๐_๐ฉ๐๐ญ๐ก: Add file(s) to the staging area.
๐ ๐ข๐ญ ๐๐ก๐๐๐ค๐จ๐ฎ๐ญ -๐ ๐๐ซ๐๐ง๐๐ก_๐ง๐๐ฆ๐: Create and switch to a new branch.
๐ ๐ข๐ญ ๐๐ก๐๐๐ค๐จ๐ฎ๐ญ ๐๐ซ๐๐ง๐๐ก_๐ง๐๐ฆ๐: Switch to an existing branch.
๐ ๐ข๐ญ ๐๐ก๐๐๐ค๐จ๐ฎ๐ญ <๐๐จ๐ฆ๐ฆ๐ข๐ญ>: Switches the working directory to a specific commit.
๐ ๐ข๐ญ ๐ฉ๐ฎ๐ฌ๐ก ๐จ๐ซ๐ข๐ ๐ข๐ง ๐๐ซ๐๐ง๐๐ก_๐ง๐๐ฆ๐: Push a branch to a remote.
๐ ๐ข๐ญ ๐ฉ๐ฎ๐ฅ๐ฅ: Fetch and merge remote changes.
๐ ๐ข๐ญ ๐๐๐ญ๐๐ก: Fetch changes from the remote repository without merging.
๐ ๐ข๐ญ ๐ซ๐๐๐๐ฌ๐ -๐ข: Rebase interactively, rewrite commit history.
๐ ๐ข๐ญ ๐ซ๐๐๐๐ฌ๐ ๐๐ซ๐๐ง๐๐ก_๐ง๐๐ฆ๐: Rebase the current branch onto another branch.
๐ ๐ข๐ญ ๐๐ฅ๐จ๐ง๐: Create a local copy of a remote repo.
๐ ๐ข๐ญ ๐ฆ๐๐ซ๐ ๐: Merge branches together.
๐ ๐ข๐ญ ๐ฅ๐จ๐ --๐ฌ๐ญ๐๐ญ: Show commit logs with stats.
๐ ๐ข๐ญ ๐ฌ๐ญ๐๐ฌ๐ก: Stash changes for later.
๐ ๐ข๐ญ ๐ฌ๐ญ๐๐ฌ๐ก ๐ฉ๐จ๐ฉ: Apply and remove stashed changes.
๐ ๐ข๐ญ ๐ฌ๐ก๐จ๐ฐ ๐๐จ๐ฆ๐ฆ๐ข๐ญ_๐ข๐: Show details about a commit.
๐ ๐ข๐ญ ๐ซ๐๐ฌ๐๐ญ ๐๐๐๐~1: Undo the last commit, preserving changes locally.
๐ ๐ข๐ญ ๐๐ซ๐๐ง๐๐ก -๐ ๐๐ซ๐๐ง๐๐ก_๐ง๐๐ฆ๐: Delete a branch forcefully.
๐ ๐ข๐ญ ๐ซ๐๐ฌ๐๐ญ: Undo commits by moving branch reference.
๐ ๐ข๐ญ ๐ซ๐๐ฏ๐๐ซ๐ญ ๐๐จ๐ฆ๐ฆ๐ข๐ญ_๐ข๐: Create a new commit that undoes the changes of a specific commit.
๐ ๐ข๐ญ ๐๐ก๐๐ซ๐ซ๐ฒ-๐ฉ๐ข๐๐ค ๐๐จ๐ฆ๐ฆ๐ข๐ญ_๐ข๐: Apply changes from a specific commit.
๐ ๐ข๐ญ ๐๐ซ๐๐ง๐๐ก: Lists branches.
๐ ๐ข๐ญ ๐ซ๐๐ฌ๐๐ญ --๐ฌ๐จ๐๐ญ ๐๐๐๐^: Undo the last commit, but keep the changes.
๐ ๐ข๐ญ ๐ซ๐๐ฌ๐๐ญ --๐ก๐๐ซ๐: Resets everything to a previous commit, erasing all uncommitted changes. 28: ๐ ๐ข๐ญ ๐๐ซ๐๐ง๐๐ก --๐ฌ๐๐ญ-๐ฎ๐ฉ๐ฌ๐ญ๐ซ๐๐๐ฆ-๐ญ๐จ ๐ซ๐๐ฆ๐จ๐ญ๐_๐๐ซ๐๐ง๐๐ก: Sets the upstream branch to the specified remote branch.
Last updated