GitHub Knowledge

Difference between Git Merge and Git Rebase?

Git Merge
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:

  1. ๐ ๐ข๐ญ ๐๐ข๐Ÿ๐Ÿ: Show file differences not yet staged.

  2. ๐ ๐ข๐ญ ๐œ๐จ๐ฆ๐ฆ๐ข๐ญ -๐š -๐ฆ "๐œ๐จ๐ฆ๐ฆ๐ข๐ญ ๐ฆ๐ž๐ฌ๐ฌ๐š๐ ๐ž": Commit all tracked changes with a message.

  3. ๐ ๐ข๐ญ ๐œ๐จ๐ฆ๐ฆ๐ข๐ญ --๐š๐ฆ๐ž๐ง๐: Modify the last commit.

  4. ๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ญ๐ฎ๐ฌ: Show the state of your working directory.

  5. ๐ ๐ข๐ญ ๐š๐๐ ๐Ÿ๐ข๐ฅ๐ž_๐ฉ๐š๐ญ๐ก: Add file(s) to the staging area.

  6. ๐ ๐ข๐ญ ๐œ๐ก๐ž๐œ๐ค๐จ๐ฎ๐ญ -๐› ๐›๐ซ๐š๐ง๐œ๐ก_๐ง๐š๐ฆ๐ž: Create and switch to a new branch.

  7. ๐ ๐ข๐ญ ๐œ๐ก๐ž๐œ๐ค๐จ๐ฎ๐ญ ๐›๐ซ๐š๐ง๐œ๐ก_๐ง๐š๐ฆ๐ž: Switch to an existing branch.

  8. ๐ ๐ข๐ญ ๐œ๐ก๐ž๐œ๐ค๐จ๐ฎ๐ญ <๐œ๐จ๐ฆ๐ฆ๐ข๐ญ>: Switches the working directory to a specific commit.

  9. ๐ ๐ข๐ญ ๐ฉ๐ฎ๐ฌ๐ก ๐จ๐ซ๐ข๐ ๐ข๐ง ๐›๐ซ๐š๐ง๐œ๐ก_๐ง๐š๐ฆ๐ž: Push a branch to a remote.

  10. ๐ ๐ข๐ญ ๐ฉ๐ฎ๐ฅ๐ฅ: Fetch and merge remote changes.

  11. ๐ ๐ข๐ญ ๐Ÿ๐ž๐ญ๐œ๐ก: Fetch changes from the remote repository without merging.

  12. ๐ ๐ข๐ญ ๐ซ๐ž๐›๐š๐ฌ๐ž -๐ข: Rebase interactively, rewrite commit history.

  13. ๐ ๐ข๐ญ ๐ซ๐ž๐›๐š๐ฌ๐ž ๐›๐ซ๐š๐ง๐œ๐ก_๐ง๐š๐ฆ๐ž: Rebase the current branch onto another branch.

  14. ๐ ๐ข๐ญ ๐œ๐ฅ๐จ๐ง๐ž: Create a local copy of a remote repo.

  15. ๐ ๐ข๐ญ ๐ฆ๐ž๐ซ๐ ๐ž: Merge branches together.

  16. ๐ ๐ข๐ญ ๐ฅ๐จ๐  --๐ฌ๐ญ๐š๐ญ: Show commit logs with stats.

  17. ๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก: Stash changes for later.

  18. ๐ ๐ข๐ญ ๐ฌ๐ญ๐š๐ฌ๐ก ๐ฉ๐จ๐ฉ: Apply and remove stashed changes.

  19. ๐ ๐ข๐ญ ๐ฌ๐ก๐จ๐ฐ ๐œ๐จ๐ฆ๐ฆ๐ข๐ญ_๐ข๐: Show details about a commit.

  20. ๐ ๐ข๐ญ ๐ซ๐ž๐ฌ๐ž๐ญ ๐‡๐„๐€๐ƒ~1: Undo the last commit, preserving changes locally.

  21. ๐ ๐ข๐ญ ๐›๐ซ๐š๐ง๐œ๐ก -๐ƒ ๐›๐ซ๐š๐ง๐œ๐ก_๐ง๐š๐ฆ๐ž: Delete a branch forcefully.

  22. ๐ ๐ข๐ญ ๐ซ๐ž๐ฌ๐ž๐ญ: Undo commits by moving branch reference.

  23. ๐ ๐ข๐ญ ๐ซ๐ž๐ฏ๐ž๐ซ๐ญ ๐œ๐จ๐ฆ๐ฆ๐ข๐ญ_๐ข๐: Create a new commit that undoes the changes of a specific commit.

  24. ๐ ๐ข๐ญ ๐œ๐ก๐ž๐ซ๐ซ๐ฒ-๐ฉ๐ข๐œ๐ค ๐œ๐จ๐ฆ๐ฆ๐ข๐ญ_๐ข๐: Apply changes from a specific commit.

  25. ๐ ๐ข๐ญ ๐›๐ซ๐š๐ง๐œ๐ก: Lists branches.

  26. ๐ ๐ข๐ญ ๐ซ๐ž๐ฌ๐ž๐ญ --๐ฌ๐จ๐Ÿ๐ญ ๐‡๐„๐€๐ƒ^: Undo the last commit, but keep the changes.

  27. ๐ ๐ข๐ญ ๐ซ๐ž๐ฌ๐ž๐ญ --๐ก๐š๐ซ๐: Resets everything to a previous commit, erasing all uncommitted changes. 28: ๐ ๐ข๐ญ ๐›๐ซ๐š๐ง๐œ๐ก --๐ฌ๐ž๐ญ-๐ฎ๐ฉ๐ฌ๐ญ๐ซ๐ž๐š๐ฆ-๐ญ๐จ ๐ซ๐ž๐ฆ๐จ๐ญ๐ž_๐›๐ซ๐š๐ง๐œ๐ก: Sets the upstream branch to the specified remote branch.

Last updated