Fundamentals 12 min read

Master Git Branching, Rebase, and Stash: Essential Commands for Developers

This guide explains core Git concepts such as local and remote branches, commit IDs, creating and amending branches, when to use rebase versus merge, interactive rebasing to squash commits, cherry‑picking, file checkout, pruning unused objects, and stash management, providing practical command examples for everyday development.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Git Branching, Rebase, and Stash: Essential Commands for Developers

Prerequisites

Before diving in, understand that the master branch refers to the main code‑submission branch.

Local and Remote Branches

A local branch is created with git checkout -b <branch-name>. A remote branch is created by pushing with git push -u origin <branch-name>, which results in a tracking branch named origin/<branch-name> on your machine.

Typical workflow involves a local branch xxx and its remote counterpart origin/xxx. Synchronizing them is done with git fetch --all, which updates the local copy of origin/xxx from the server.

Commit ID

Each commit generates a unique SHA‑1 identifier. This ID can be used to locate the exact snapshot of the repository, ensuring that code is never lost and can be retrieved at any time.

Usage Guide

Creating a Branch

To create a new branch based on the latest remote master without checking out master first, run:

git fetch --all origin/master
git checkout -b <new-branch> origin/master

This creates <new-branch> directly from origin/master.

Amending a Commit

If you have already committed but need to modify the last commit, use: git commit --amend Amend only works if the previous commit has not been pushed. If you must overwrite a remote branch (dangerous), you can force‑push with git push -f, but use this with extreme caution.

Rebase vs. Merge

This article focuses on rebase . Typical scenarios:

Scenario 1: Your feature branch branch‑a has diverged from master. Run git rebase origin/master to replay your commits on top of the latest master. After rebasing, you will need to force‑push because the history has changed.

Scenario 2: A teammate has merged changes into master that you also need. Rebase branch‑a onto origin/master to incorporate those updates and resolve conflicts.

Scenario 3: When pulling changes from a shared branch, prefer git pull --rebase over the default git pull (which merges) to keep a linear history.

Interactive Rebase – Squashing Commits

To combine several recent commits into one, start an interactive rebase: git rebase -i HEAD~5 In the editor, change the action of the commits you want to merge from pick to squash (or s). Save and exit, then edit the combined commit message when prompted.

Cherry‑Pick a Commit

If you need a specific commit from branch‑b in branch‑a, locate its SHA‑1 and run: git cherry-pick <commit-id> To copy a single file from another branch without affecting other changes:

git checkout <branch> -- <file-path>

Resetting a File

To revert a file to the version in another branch:

git checkout <branch> -- <file>

Cleaning Unused Objects

git prune

removes unreachable objects from the local .git/objects directory. For most cases, git gc is recommended. To delete local branches that no longer exist on the remote, you can use:

git fetch --all --prune && git branch -vv | grep gone | awk '{print $1}' | xargs git branch -D

Stashing Changes

Save unfinished work with git stash. Retrieve the most recent stash with git stash pop (removes it) or git stash apply (keeps it). To apply a specific stash, first list them: git stash list Each entry shows an identifier; apply it with:

git stash pop/apply <stash-id>

Conclusion

The commands and workflows described above are frequently used in daily development. Mastering branch creation, rebasing, cherry‑picking, pruning, and stashing will make version‑control operations smoother and reduce the risk of lost work.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GitrebaseVersion Controlbranchingcherry-pick
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.