upstream → git@github.com:rajasgs/git-test.git (the original / parent repo)
Now the commands you listed:
Let us walk through them.
1. Add the parent as a new remote
origin already points to your fork.
This command adds another remote called upstream which points to the parent/original repository.
After this, you have at least two remotes:
origin → your fork (rjsgstest/git-test-sgstest)
upstream → original (rajasgs/git-test)
You do this only once per clone.
You can confirm with:
2. Fetch the latest changes from the parent
Downloads all branches, tags, etc. from the upstream remote into your local repo.
Does not change your current branch yet.
After fetch, Git has:
upstream/main (or upstream/master, depending on the parent’s default branch)
and possibly other upstream branches.
Think: “sync my local metadata with the parent, but don’t touch my working tree yet.”
3. Merge the parent’s branch into your current branch
This assumes:
You are currently on, say, master or main in your local repo.
The parent’s default branch is master.
This command:
Takes commits from upstream/master
Merges them into your current branch (e.g., master).
So your local branch now includes whatever new changes happened in the parent repository.
If the parent’s default branch is main instead of master, you would do:
4. Alternatively: git pull upstream main
This is basically a shortcut for:
So:
git pull = fetch + merge (by default)
git pull upstream main:
Fetches from the upstream remote
Merges upstream/main into your current local branch.
Note:
Your sample sequence has both:
In practice, you would normally do one or the other, not both, and you must use the correct branch name (either master or main, depending on how the parent repo is configured).
5. Push updated fork back to GitHub
This pushes your current local branch (now updated with upstream changes) to origin (your fork) on GitHub.
Result:
Your fork rjsgstest/git-test-sgstest is now in sync with the parent rajasgs/git-test for that branch.
Often you will be on main locally, so you’re effectively doing:
or simply:
Conceptual Summary
origin: your fork on GitHub (where you push).
upstream: original parent repo (where you pull new changes from).
Usual workflow to sync fork with parent:
Make sure you are on the correct local branch:
Get latest from parent:
Update your fork on GitHub:
3 Git basic commands
Below is a precise explanation of each command and what it shows inside your Git repository.
1.
git remote
Purpose: Lists the names of remote repositories that this project knows about.
Example output:
This means your local repo is linked to two remotes (often:
origin → your fork
upstream → parent/original)
This command shows only the remote names, not their URLs.
2.
git remote -v
Purpose: Shows the remote repositories and their fetch/push URLs.
Example output:
Use this to verify:
Which remote points where
Whether fetch and push URLs differ
3.
git branch -r
Purpose: Lists remote branches only.
Example output:
Meaning:
They exist on remote servers (origin/upstream)
You haven't necessarily checked them out locally
These appear after git fetch
Think: branches that exist "somewhere online."
4. Git branch
git branch -l
or simply
git branch
Purpose: Lists local branches stored inside your machine.
Example output:
These are branches you have created or checked out.
Difference Summary
Command
Shows
Local / Remote
git remote
Remote names
N/A
git remote -v
Remote names + URLs
N/A
git branch -r
Remote branches
Remote
git branch -l (or git branch)
Local branches
Local
Visual Mapping
Local branch →
Points to
Remote branch
main
tracking
origin/main
feature/api
local only
none
hotfix
local until push
origin/hotfix
Remote-tracking branches (origin/main, upstream/main) represent the last known state of those remotes after fetching/pulling.
Below are the main methods to install or upgrade Git on macOS. The correct method depends on how you prefer to manage software.
1. Install/Upgrade Git Using Homebrew (Recommended)
If you already use Homebrew, this is the cleanest and most current method.
Install:
Upgrade:
Verify version:
Check where Git is installed:
Homebrew places Git under:
2. Install Git from official installer (GUI Installer)
Download macOS Git installer:
Search: “Git for macOS installer” → This is published on git-scm.com, which redirects to the current signed macOS installer.
Run the .pkg, follow setup, close terminal, reopen.
Check:
3. Install Using Xcode Command Line Tools (basic but older version)
If you type git and macOS asks to install developer tools:
This installs Git bundled with Apple dev tools, but this version is usually older.
4. Verify macOS Git Path Priority (Important)
If you have multiple Git installations:
To list all:
If the Homebrew version is installed but not being used, you may need to update your PATH:
For Apple Silicon:
For Intel:
Reload terminal:
Which Method Should You Use?
Method
Version Freshness
Best Use
Homebrew
Very up-to-date
Active development
Git Installer (pkg)
Updated manually
GUI users
Xcode tools
Often outdated
Basic mac Git usage
Confirm Git is Upgraded
7 get all commits
Below is a clear explanation of the command:
What this command does
This command prints all commits in the entire repository history, across all branches, in a single line format.
Meaning:
It does not show only the current branch
It includes commits reachable from all heads
You see the commit hash followed by the commit message on one line
Example output:
Breaking down the command
Part
Meaning
git rev-list
Low-level command to list commit objects
--all
Includes commits from all branches, tags, heads
--pretty=oneline
Show simplified output: one commit per line
Without --pretty=oneline, rev-list prints only the commit hashes in a long block.
Why or when to use this command
This command is helpful for:
Auditing the entire history
Searching commit messages manually
Feeding commit lists to scripts
Exporting commit logs
Debugging or tracing repository evolution
Alternative Commands for Similar Output
Command
Output
git log --oneline --all
Human-readable version
git log --graph --oneline --all
Visual branch graph
git log --pretty=format:"%h %an %s" --all
Custom formatting
Example graph:
Summary
git rev-list --all --pretty=oneline provides a raw, script-friendly list of every commit in the repository.
It displays commit hash + subject message, one line each.
Useful when performing automation, analysis, history export, or debugging.
Here’s a clear explanation of what the documentation at that link means — i.e. what it means to “change the default branch” on GitHub, and when/why you’d do that. (GitHub Docs)
What is the “default branch” on GitHub
The default branch is the branch that GitHub treats as the main line of development for a repository (the “main” line). (GitHub Docs)
It’s the branch that is checked out when someone clones the repo without specifying a branch. (GitHub Docs)
It’s also the default base branch for new Pull Requests and many automated workflows (CI, deployment, etc.). (GitHub Docs)
Because of that, choosing the correct default branch influences the repository’s workflow and collaboration model.
When / Why You’d Change the Default Branch
You might want to change the default branch when:
You rename your main development branch (e.g. from master → main). (R-bloggers)
You restructure branching strategy: maybe want a different branch (e.g. main, dev, release) as default.
You create a new “mainline” branch that should become the canonical base for PRs/merges.
After merging or deprecating the old default, you want a clean “central branch” for future development.
Changing default branch ensures new clones and collaborators automatically use the correct branch, and that pull requests target the intended branch by default.
What the Documentation Requires / Preconditions
The repository must have at least two branches (so that there’s a branch to switch to). (GitHub Docs)
You must have admin access to the repository to change the default branch. (GitHub Docs)
If there are organization-level branch-management rules (e.g. protected branches policy), those may limit whether you can change defaults. (GitHub Docs)
How to Change the Default Branch on GitHub
Go to the repository’s main page on GitHub. (GitHub Docs)
Click the Settings tab (or if hidden, open the dropdown menu). (GitHub Docs)
Scroll to the “Default branch” section. Next to the current default branch name, click the “edit” icon. (GitHub Docs)
From the dropdown, select the branch you want as the new default. (GitHub Docs)
Click Update; then read the warning prompt and confirm by clicking “I understand, update the default branch.” (GitHub Docs)
Once confirmed: the new default branch becomes the base for future clones, pull requests, and other GitHub interactions. (GitHub Docs)
Consequences & What You Should Consider
New clones → by default checkout the new default branch.
Existing clones will still have their original checked-out branch. Developers may need to manually switch to the new default and, optionally, set their local branch to track the corresponding remote branch.
Open Pull Requests (PRs) — their base may remain the old default (depending on your GitHub settings). You’ll want to review and re-base/target PRs appropriately if needed.
CI / automation / tooling / scripts — any pipelines, hooks, or scripts referencing the old default branch name may break; they need updates to refer to the new branch.
Collaborator communication — you should inform the team of the branch rename/default-branch change to avoid confusion.
Below is the correct, realistic explanation: Git does not store the “parent branch” of a branch. Once a branch is created, Git only stores commits, not where the branch was created from. That is why there is no built-in command git parent, and determining “parent branch” is guesswork based on commit history.
Why Git has no concept of a formal parent branch
When you run:
Git only stores:
A pointer to a commit hash
Where new commits will attach
It does not store:
“The feature/login branch was created from main at this time.”
This is intentional; Git is a directed acyclic graph of commits, not a tree of branches.
How we infer a parent branch
There are techniques that inspect history to guess which branch you originally branched from.
Approach 1: Using merge-base
To check where two branches diverged:
This gives the common ancestor commit.
To view the commit:
This doesn’t return the parent branch, but the divergence commit.
Approach 2: Show where the branch began (first commit not in main)
This often reveals the first commit exclusive to the branch.
Approach 3: Script to guess “parent branch”
Git community often uses this StackOverflow-shared script:
This prints the likely ancestor branch name.
But remember — this is a guess, not guaranteed.
Approach 4: Using reflog (only local, only if never cleaned)
You may find:
This is the closest to the truth, but works only if reflog is intact and only on the developer’s machine where the branch was created.
Important Concept — Branch Parent Is Contextual
Example:
Developer A creates branch → pushes to repo
Developer B clones later
Developer B cannot know the origin branch from Git alone.
Unless:
There is a naming convention (e.g., feature/abc-from-main)
There are pull request policies
Tools like Jira store branch metadata
Teams solve this by:
Workflow Pattern
Parent Tracking
GitHub PR workflow
PR target determines parent
GitFlow
Dumb knowledge: features always branch from develop
Trunk-based development
Only main exists
Ticket naming conventions
e.g. feature/123-from-main
Summary
| Can Git tell you the parent branch? | No |
| Can Git infer one? | Sometimes |
| Is the inference guaranteed? | No |
| Best reliable source | PR history or your branching policy |
13 When did I create a branch
Below is a clear technical explanation of how to determine when a Git branch was created using reflog, plus what the commands in your example represent.
Core Concept: Git Does not Store Branch Creation Time
Git does not record the timestamp of branch creation as metadata.
However, Git reflog maintains a history of HEAD and reference movements, so we infer the creation moment by examining when a branch first pointed to a commit.
The Command
Shows movements of HEAD (checkouts, merges, commits).
Displays dates in your local time.
Useful to see when you switched branches, created branches, checked out from where, etc.
To Check Reflog of a Specific Branch
This shows all reference updates for the branch dev — including the moment it first appeared as a reflog entry (i.e., creation).
Example:
This means the branch dev was created or checked-out at that moment.
Similarly:
Shows history for that branch’s reference.
Interpreting Your Sample Output
Example log snippet:
This means:
On Aug 10, 2022 at 15:08, the user checked out into dev
If that commit didn’t previously exist under that branch name, this reflects the branch creation moment.
Another entry:
This suggests that branch Docker-etl-dev existed before 13:02:48.
Important: Reflog Can Expire
Reflog holds a limited history (default 90 days, but can be configured), and may not help for branches created long ago.
From StackOverflow – The Key Takeaways
Link 1:How to determine when a git branch was created?
The accepted consensus: No direct metadata.
Reflog is the best practical method but not guaranteed if garbage collected.
Link 2:How to go back to the previous master branch
Demonstrates using reflog to determine movement between branches, letting you return to a previous state.
Example:
Alternative — Infer Branch Creation Without Reflog
If reflog expired but you know branch parent:
This finds the first commit that exists only in this branch, which is often close to its creation.
Example:
This won’t give an exact time if branch creation preceded first commit, but it provides earliest unique commit timestamp.
Generates a patch (set of code changes) representing the difference between two branches:
Pipes that patch output as input (|) to:
Where - means “read patch from STDIN”.
git applyapplies the patch to your current working directory.
Result:
The changes that exist in new-feature2 but not in new-feature are applied to your current branch without merging, without committing, and without changing history.
Practical Interpretation
Command
Meaning
git diff A..B
Show changes between A and B
git apply
Apply those changes as modifications
This workflow is effectively:
“Take the diff between these two branches and apply that set of edits here.”
When You Would Use This
Use cases:
Scenario
Reason
Copy specific changes without merging
Avoids merging unwanted commits
Backporting code
Patch only selected updates
Apply changes temporarily for testing
Reversible
Move part of a feature into another branch
Manual patching
This is similar to cherry-pick, but:
cherry-pick applies the commit(s) with history
diff | apply applies the changes without carrying commit metadata
Important Notes
Behavior
Explanation
No commit created
Changes appear as uncommitted in working directory
Can conflict
Same as any patch
Reversible
Using git apply -R (reverse apply)
To reverse:
Example Flow
Suppose you are currently on branch test-branch:
Now test-branch contains edits from new-feature2 that differ from new-feature.
Check what changed:
Commit if desired:
Difference: Cherry-Pick vs Diff|Apply
| Feature | cherry-pick | diff | apply |
|--------|-------------|------|
| Keeps commit message | Yes | No |
| Keeps author name | Yes | No |
| Applies uncommitted patch | No | Yes |
| Can combine multiple commits before applying | Harder | Easy |
| Transfers history | Yes | No |
Summary: diff|apply = patch transfer without history.
Short Summary
git diff A..B shows changes
| git apply - applies those changes as a patch
Useful when you want only the code changes, not the commit history
17 Git for each Ref
Below is a step-by-step technical explanation of the full command:
This script lists all local branches and prints which remote branch each one is tracking.
Break Down the Command
Part 1 — List all local branches
refs/heads/* = namespace where local branches are stored.
--format='%(refname:short)' prints only the short branch name (e.g., dev instead of refs/heads/dev).
This outputs something like:
Output is piped into while.
Part 2 — Loop through each branch
Reads each branch name into variable b.
Part 3 — Check if this branch has a configured remote tracking branch
Looks up: branch.<branchname>.remote
Example: branch.dev.remote = origin
If it exists, assigns the remote name to variable r.
If a branch is not tracking anything, this IF fails and nothing prints for that branch.
Part 4 — Retrieve the merge reference configured for that branch
Looks up: branch.<branchname>.merge
Example: branch.dev.merge = refs/heads/main
This is the remote branch name that local branch pulls from.
Part 5 — Print result in readable form
Breakdown of ${m##*/} (Bash substring expression):
Removes everything before the last /
Converts refs/heads/main → main
Example Output:
Meaning:
Local branch dev tracks remote branch origin/main.
What This Script Essentially Does
Local Branch
Remote Name
Remote Branch
It produces a mapping like:
This answers questions like:
Which remote branch does my local branch track?
Is dev pulling from main or from dev?
Which remote branch will git pull apply to?
Which branch would --set-upstream refer to?
Why People Use This Command
It is especially useful when:
Working in large repos with many feature branches
Auditing tracking branches
Diagnosing unexpected pulls or merges
Verifying CI/CD dependencies
Cleaning unused or incorrectly configured branches
Summary
This one-liner:
Lists all local branches
Finds their configured remote tracking branch
Prints it in local -> remote/branch format
A simpler conceptual description:
Show every local branch and the remote branch it tracks
Below is a clear explanation of Git Stash, git stash list, and git stash show.
What Git Stash Does
git stash saves your uncommitted changes (modified or staged) and clears your working directory, letting you switch branches or work on something else without committing.
Example:
You are in the middle of work
You need to switch branches urgently (bug fix, code review, etc.)
You do not want to commit yet
You run:
Git:
Saves your changes in a temporary stack
Restores the branch to a clean state
git stash list
Displays all stashes saved in your repository.
Example output:
Meaning:
stash@{0} is the most recent
Each stash stores:
The branch name you were on
The commit you were working against
Optional message
git stash show
Shows which files have changed in the most recent stash (stash@{0}).