Popular Searches
Popular Course Categories
Popular Courses

Repositories, commits, branches

Repositories, commits, branches

29 mins Git & GitHub

Repositories, commits, branches in React JS

Course: React JS Training

Topic: Repositories, commits, branches

Course Link: React JS Training

Demo Link: Register for a Course Demo


1. Why version control matters

Every beginner knows this scene: a project folder full of files named index-final.html, index-final-v2.html and index-really-final.html. Each copy was meant to be a safe backup, but after a few days nobody remembers which version actually works, what changed between them, or how to get yesterday's code back.

Version control solves this problem with software instead of copies. Git is the most widely used version control system in the world. It records every saved state of your project, lets you travel back to any earlier state, and lets several people work on the same project without overwriting each other's work.

For this course, Git is not optional extra knowledge. Every React project you build will live inside a Git repository, your future teammates will expect you to branch, commit and push confidently, and the portfolio you publish on GitHub later in this module is built entirely from repositories and commits. Three ideas carry everything: repositories, commits and branches.

  • History: every version of every file is saved, so nothing is ever truly lost.
  • Safety: you can experiment freely, because you can always return to a known good state.
  • Teamwork: several people can change the same project at the same time and combine their work cleanly.
  • Proof of work: a public repository shows employers what you built and how you built it.

2. What is a repository

A repository is a project folder that Git is watching. It contains two things: your actual files (HTML, CSS, JavaScript, images, everything), and a hidden sub-folder called .git where Git stores the complete history of the project.

A repository (usually shortened to "repo") is one project's files plus that project's entire history, stored together in one place.

The .git folder is what turns an ordinary folder into a repository. You will rarely look inside it, because Git manages it for you, but it is where commits, branches and settings live. If you delete it, the folder keeps its files but loses all of its history.

Repositories come in two flavours:

  • Local repository: the copy on your own computer. You edit, commit and experiment here, fully offline if you like.
  • Remote repository: a copy hosted on a service such as GitHub. It is the shared meeting point where teammates send their commits and receive everyone else's.

A typical workflow keeps both: you work in your local repository and regularly sync with the remote one on GitHub. One project normally has one repository, with the remote acting as a second copy of the same history rather than a separate project.


3. Key terms at a glance

Git has a small vocabulary. You will meet these words constantly, so read the table now and return to it whenever a term feels fuzzy.

TermMeaning in plain words
RepositoryA project folder plus its full history, stored in the hidden .git folder.
Working directoryThe files as they currently sit in your project folder, ready to edit.
Staging areaA waiting room where you place the changes you want in the next commit.
CommitA saved snapshot of the project, with a message describing what changed and why.
BranchA movable label pointing at one commit, used to develop work independently.
HEADA pointer that says which branch and commit you are currently on.
RemoteAnother copy of the repository, usually hosted on GitHub.
CloneYour own local copy of a remote repository, created with git clone.
PushSend your local commits up to the remote repository.
PullBring commits from the remote repository down into your local one.

4. How Git thinks: the three areas

The single most useful mental model in Git is that your project exists in three places at once. Understanding the journey of a change through these areas explains almost every command you will type.

working directory   --git add-->   staging area   --git commit-->   repository
 (files you edit)                (ready to save)                (permanent history)
AreaWhat it holdsCommands that act on it
Working directoryYour files exactly as they are on disk right nowgit add sends changes forward
Staging areaOnly the changes you selected for the next commitgit commit saves them; git restore --staged removes them
RepositoryThe permanent chain of commitsgit log and git show read it

Why does the staging area exist? Because a commit should tell one clear story. Suppose you fixed a bug and also renamed a few files in the same sitting. With staging you can commit the bug fix first and the renames separately, even though both kinds of edits sit in your working directory at the same time. You decide what goes into each commit by staging exactly those changes.

git status is your map of this model: it tells you which files are untracked, which are staged, and which are modified but not yet staged.


5. The commands you will use most

These commands cover everyday work with repositories, commits and branches. The worked examples below show each one inside a full, realistic session.

CommandWhat it does
git initTurns the current folder into a new repository.
git statusShows the state of the working directory and staging area.
git add <file>Stages one file for the next commit.
git add .Stages every change in the current folder.
git commit -m "message"Saves the staged changes as a commit; -m supplies the message.
git log --onelineLists the history with one short line per commit.
git diffShows unstaged changes line by line.
git switch -c <branch>Creates a new branch and moves to it in one step.

6. What happens inside a commit

A commit is not a list of changes; it is a snapshot of the whole project at one moment, plus some metadata. When you commit, Git records:

  • A snapshot of every file's content at that moment, stored efficiently so unchanged files are not copied again.
  • Who made it and when: the author name, email and timestamp from your configuration.
  • A message: your human-readable explanation of the change.
  • A unique ID: a long code called a hash (shown in short form as something like 4c2f8a1) that identifies the commit permanently.
  • A parent: the ID of the commit that came before, which is how commits chain into a history.

Because every commit points to its parent, the history is a chain: the newest commit leads back through its parents all the way to the very first commit. A branch is simply a lightweight label stuck on one commit in this chain, and each new commit slides the label forward.

This design explains Git's reliability. Commit IDs are calculated from the stored content, so altering a file would produce a different ID. If a commit exists in your repository, its contents have not been silently changed.


7. Worked example: your first repository

Let us build a tiny portfolio site and save its first commit. Open a terminal and follow along; every line shown is a real command you can reproduce.

Create the project folder and turn it into a repository:

mkdir my-portfolio
cd my-portfolio
git init

Git replies:

Initialized empty Git repository in /home/anita/my-portfolio/.git/

Before the first commit, Git needs to know who you are, because every commit records its author. Configure this once for your whole machine:

git config --global user.name "Anita Sharma"
git config --global user.email "[email protected]"

Now create a file called index.html containing a single heading:

<h1>My Portfolio</h1>

Ask Git what it sees:

git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        index.html

nothing added to commit but untracked files present (use "git add" to track)

Untracked means Git can see the file but is not saving its history yet. Stage it and make the first commit:

git add index.html
git commit -m "Add first version of the home page"
[main (root-commit) 4c2f8a1] Add first version of the home page
 1 file changed, 1 insertion(+)
 create mode 100644 index.html

Three things happened: the staged file was stored as a snapshot, the commit received the ID 4c2f8a1, and the branch main now points at it. The word root-commit tells you this is the very first commit in the chain.


8. Worked example: editing files and reading history

Repositories prove their worth the moment files change. Add a second line to index.html so it reads:

<h1>My Portfolio</h1>
<p>Welcome to my personal website.</p>

Check exactly what changed:

git diff
diff --git a/index.html b/index.html
index 8d1f2a3..5e9c4b7 100644
--- a/index.html
+++ b/index.html
@@ -1 +1,2 @@
 <h1>My Portfolio</h1>
+<p>Welcome to my personal website.</p>

Lines beginning with + were added. Stage, commit, then read the history:

git add index.html
git commit -m "Add a welcome paragraph to the home page"
git log --oneline
5e9c4b7 (HEAD -> main) Add a welcome paragraph to the home page
4c2f8a1 Add first version of the home page

History reads from top to bottom, newest first. HEAD -> main means you are on the main branch, whose newest commit is 5e9c4b7, and each line sits directly above its parent, showing the chain we described earlier.

To see the full detail of one commit, including author, date, complete message and the exact changes, ask for it by ID:

git show 4c2f8a1

And notice what the history guarantees: the earlier one-line version of index.html is still stored in the repository, ready whenever you need it. Nothing was overwritten or lost.


9. What is a branch

A branch is a movable label that points to one commit. When you make a new commit while a branch is active, the label automatically slides forward to that new commit. Nothing is copied and no files are duplicated; a branch costs Git almost nothing, which is why developers create them constantly.

A fresh repository starts with one branch, usually named main (older repositories may call it master). This default branch holds the stable, working version of the project. The everyday rule on teams is simple:

  • main stays clean and working at all times.
  • New work, whether a feature, a fix or an experiment, happens on its own branch.
  • When the work is finished and tested, the branch is combined back into main, which is the subject of the next sub-module.
A branch answers one question: "where does my line of work currently end?" Everything else, including commits and history, is shared.

Branches are why Git feels safe. You can build a risky feature on a branch for a week while main keeps working, and move between the two versions of your project with a single command.


10. How branching works step by step

  1. Check where you are. git status shows the current branch on its first line.
  2. Create a branch. git branch add-navbar places a label named add-navbar on your current commit.
  3. Move to it. git switch add-navbar moves HEAD to the new branch, and the files on your disk change to match that branch's newest commit.
  4. Work and commit. Every commit you make now advances add-navbar, while main stays exactly where it was.
  5. Switch back any time. git switch main instantly shows the main version of every file.

Two habits worth memorising: git switch -c add-navbar creates and switches in one step, and git branch with no arguments lists all branches, marking the active one with an asterisk:

git branch
  add-navbar
* main

The asterisk marks where HEAD is. HEAD is Git's "you are here" pointer: it normally points at a branch, and the branch points at its newest commit. When you switch branches, HEAD moves and your working directory is rewritten to match.

Good branch names describe the job: add-navbar, fix-contact-form, dark-theme. Use lowercase words separated by hyphens, and give each branch one job. One rule protects beginners: commit your work before switching branches, because half-finished edits either block the switch or travel with you to the wrong branch.


11. Worked example: building a feature on a branch

Here is the scenario in full. Your portfolio works, and you want to add a navigation bar. You do not want a half-finished navbar sitting on main, so the work goes on a branch.

git switch -c add-navbar
Switched to a new branch 'add-navbar'

Create a file called navbar.html with a simple menu:

<nav>
  <a href="index.html">Home</a>
  <a href="about.html">About</a>
  <a href="projects.html">Projects</a>
</nav>

Stage and commit it on the branch:

git add navbar.html
git commit -m "Add a navigation bar with three links"
[add-navbar 9f3d2c1] Add a navigation bar with three links
 1 file changed, 4 insertions(+)
 create mode 100644 navbar.html

Now switch back to main and list your files:

git switch main
ls
index.html

navbar.html has vanished from the working directory. It was not deleted; it simply lives on the add-navbar branch. Switch back and it reappears:

git switch add-navbar
git log --oneline
9f3d2c1 (HEAD -> add-navbar) Add a navigation bar with three links
5e9c4b7 Add a welcome paragraph to the home page
4c2f8a1 Add first version of the home page

The branch's history contains all three commits, because add-navbar was created from main and inherited its past. Meanwhile main still ends at 5e9c4b7. This is the whole point of branching: two parallel versions of history sharing a common past, each safe to change independently.


12. Connecting a repository to GitHub

So far everything has lived on your machine. GitHub hosts a remote copy of your repository on the internet, which gives you three things: a backup that survives a broken laptop, a public page where anyone can browse your code, and a shared hub for teamwork.

A remote repository is linked to your local one, not blindly copied. You connect them once:

git remote add origin https://github.com/your-username/my-portfolio.git
git push -u origin main

The first command registers GitHub's address under the nickname origin, the conventional name for a project's main remote. The second sends your main branch, with all of its commits, up to GitHub; the -u flag remembers the pairing so that future uploads need only git push.

Two more commands complete the picture:

  • git clone https://github.com/user/project.git downloads an existing remote repository as a fresh local copy, history included. This is how you take a teammate's project, or your own repository, onto a new machine.
  • git pull fetches commits that others have pushed and brings them into your local repository.

The everyday rhythm on a team is therefore: pull, work, commit, push. Publishing your portfolio properly on GitHub, including presenting it as a live website, is covered in its own sub-module later in this course.


13. Repositories, commits and branches compared

The three ideas form a hierarchy, and keeping their roles separate prevents most beginner confusion.

AspectRepositoryCommitBranch
What it isThe whole project plus its full historyOne saved snapshot of the projectA movable label pointing at one commit
How manyOne per project, with local and remote copiesOne per saved state; many over a project's lifeAs many as you like; cheap to create
Created withgit init or git clonegit commitgit branch or git switch -c
Answers the question"Where does this project live?""What did the project look like, and why did it change?""Which line of work am I on?"
AnalogyThe photo albumOne photographA bookmark placed on one photo

Common beginner mistakes

  • Committing without staging. If git status still lists changes after a commit, you forgot git add, and the commit contains nothing new.
  • Vague messages. "Changes", "update" and "fix" tell future you nothing. Write what changed and why, for example "Fix mobile menu overlapping the header".
  • One giant commit. A commit mixing a bug fix, new styles and renamed files cannot be understood or undone cleanly. Commit one logical change at a time.
  • Working directly on main. Untested edits on main break the stable version for everyone who depends on it. Make a branch first.
  • Skipping git status. It is the cheapest way to know exactly where your changes are before you act on them.

14. Real-world usage: ignoring files with .gitignore

Not every file in a project folder belongs in the repository. Operating systems drop clutter such as .DS_Store, Node projects carry a huge node_modules folder that anyone can rebuild, and configuration files often hold passwords or API keys that must never become public. Git lets you list these exceptions in a plain text file named .gitignore, placed in the root of the repository.

Create one and add a few typical entries for a web project:

.DS_Store
node_modules/
dist/
.env
*.log

Each line is a pattern. A name on its own matches a file or folder anywhere in the project, a trailing slash marks a folder, and * is a wildcard that matches any text, so *.log covers every log file. Once the file is saved, Git stops reporting the ignored items in git status.

Two rules make .gitignore behave predictably:

  • Ignored files must not already be tracked. If you committed notes.txt before adding it to .gitignore, Git keeps tracking it. Remove it from tracking first with git rm --cached notes.txt, commit, and the ignore rule then takes effect.
  • The .gitignore file itself should be committed. It is part of the project's setup, so every teammate gets the same rules automatically.

For teams, the most important entry is .env. Secret keys committed to a public GitHub repository are visible to the whole world, and removing them later does not erase them from history. Preventing the commit is the only reliable protection.


15. Real-world usage: comparing versions and recovering old code

History is only useful if you can inspect it and travel through it. Git gives you three everyday tools for this.

Compare any two commits. Pass two commit IDs to git diff and Git prints exactly what changed between them:

git diff 4c2f8a1 5e9c4b7

Look at an old version of a file without changing anything. Appending :filename to a commit ID prints the file as it was at that moment:

git show 4c2f8a1:index.html
<h1>My Portfolio</h1>

Bring an old version back into your working directory. If a new edit broke the page and you want yesterday's working file back, restore it from the commit you name:

git restore --source 4c2f8a1 index.html

The current file on disk is replaced by the stored version, and your history is untouched. This is the moment version control pays for itself: a mistake that once meant lost work is now a one-line recovery.

A related safety tool is git stash. It temporarily shelves uncommitted changes and clears the working directory, useful when a bug report arrives mid-task:

git stash
git stash pop

The first command parks your half-finished edits safely; the second brings them back when you are ready to continue.


16. Real-world usage: fixing mistakes safely

Beginners fear Git because they think every mistake is permanent. In practice, most corrections are simple, and the safe ones never rewrite shared history.

SituationCommandWhat happens
Staged a file by accidentgit restore --staged fileThe file leaves the staging area; your edits remain on disk.
Edited a file and want the edit gonegit restore fileThe file returns to its last committed state.
Typo in the last commit messagegit commit --amend -m "Correct message"The last commit is replaced by a corrected one.
Forgot to include a file in the last commitgit add file then git commit --amend --no-editThe file joins the last commit; the message stays the same.
Committed something that must be undone, and already pushed itgit revert <commit-id>Git creates a new commit that reverses the old one, leaving an honest record of both.

The key distinction is between rewriting and reversing. --amend is fine before anyone else has your commit, because only your local copy changes. Once a commit has been pushed and teammates may have pulled it, use git revert: it adds a new commit on top rather than erasing the old one, so everyone's history stays consistent.


17. A day on a team: the everyday Git rhythm

On a real project, the commands from this page combine into a repeating rhythm. Here is a complete day, from morning to push, for a developer adding a contact form to a team website.

  1. Start by syncing. Overnight, teammates pushed their work, so pull before touching anything: git switch main then git pull.
  2. Branch for the task. git switch -c add-contact-form. Everything you do today stays off main.
  3. Work in small steps. Build the form's HTML, then stage and commit: git add index.html and git commit -m "Add contact form markup". Add the styling, commit again: git commit -am "Style the contact form". Each commit is one clear, working step.
  4. Push the branch. git push -u origin add-contact-form uploads the branch to GitHub, where teammates can see it. The -u flag links your local branch to its remote copy, so later pushes need only git push.
  5. Keep syncing if the day runs long. If teammates merge work into main during the day, git pull origin main brings those changes into your branch so surprises surface early.
  6. Hand over for review. Opening a pull request to combine the branch into main is the subject of the next sub-module.

Notice the shape of the day: pull, branch, small commits, push. The same loop repeats for every task on every team, which is why interviewers treat it as baseline knowledge.


18. Common mistakes and the correct way

Each row below pairs a frequent beginner mistake with the habit that prevents it.

MistakeWhat goes wrongThe correct way
Committing node_modules or build outputThe repository balloons to thousands of files and becomes impossible to reviewList generated folders in .gitignore before the first commit
Committing passwords or API keysSecrets in a pushed commit are public and stay in history even after deletionKeep secrets in a .env file that is ignored from day one
Messages like "update" or "stuff"History cannot be searched or understood weeks laterWrite what changed and why: "Fix mobile menu overlapping the header"
One giant commit at the end of the weekA single mistake cannot be undone without losing good work with itCommit each logical step as soon as it works
Editing directly on mainBroken code reaches everyone who pullsCreate a branch per task and merge it only when it works
Pushing without pulling firstThe push is rejected because the remote has commits you do not havegit pull at the start of every session and before every push
Deleting the .git folder to "reset" a projectAll history is destroyed instantly and cannot be recoveredUse git restore, git revert or ask for help; never delete .git
Switching branches with half-finished editsThe switch is blocked, or the edits travel to the wrong branchCommit first, or park the edits with git stash

One habit prevents most of this list: run git status before every git add and every git commit. It is a free, instant check on exactly what you are about to record.


19. Best practices

  • Commit small and often. A commit should capture one idea that works. Small commits are easy to understand, easy to review and easy to undo.
  • Write messages in the imperative mood. "Add search filter", not "Added search filter" or "I was adding a filter". The message should complete the sentence "If applied, this commit will ..."
  • One branch, one job. A branch named fix-contact-form should contain only contact-form work. Mixing unrelated changes into one branch makes review painful.
  • Stage deliberately. Prefer git add <file> over git add . when you want a focused commit; use git add . only when everything in the folder genuinely belongs together.
  • Set up .gitignore before the first commit. Retrofitting it means untracking files that are already in history.
  • Read before you write. Check git status and git diff before committing, so you never record something you did not intend.
  • Push at least daily. Unpushed commits exist only on your machine; a failed disk erases them. Pushing is your off-site backup.
  • Never commit secrets. Treat any pushed credential as compromised and replace it, even if you remove it afterwards.

20. Practical project: version a landing page from scratch

This project walks through the full lifecycle with a real mini-project: a personal landing page, built, versioned, branched and published to GitHub. Follow the steps in order; every command is shown.

Step 1: create and initialise the repository

mkdir landing-page
cd landing-page
git init
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Step 2: ignore clutter before the first commit

Create a file named .gitignore containing:

.DS_Store
.env
notes.txt

Step 3: build the HTML and make the first commit

Create index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Anita Sharma | Frontend Developer</title>
</head>
<body>
  <h1>Hi, I am Anita</h1>
  <p>I build responsive websites with HTML, CSS and JavaScript.</p>
</body>
</html>

Stage and commit:

git status
git add .gitignore index.html
git commit -m "Add landing page structure"

Step 4: style the page as a second commit

Create styles.css:

body {
  font-family: Arial, sans-serif;
  margin: 2rem;
  background: #f5f5f5;
}

h1 {
  color: #222;
}

Link it inside the <head> of index.html:

<link rel="stylesheet" href="styles.css">

Then commit the styling as its own logical change:

git add index.html styles.css
git commit -m "Style the landing page background and heading"

Step 5: add a footer on a branch

git switch -c add-footer

Add this markup before the closing </body> tag:

<footer>
  <p>Contact: [email protected]</p>
</footer>

Commit on the branch:

git add index.html
git commit -m "Add a footer with contact email"

Step 6: prove the branches are independent

git switch main
git log --oneline
8b1e4d2 Style the landing page background and heading
7a9c3f1 Add landing page structure

main has two commits and no footer. Switch back and the footer returns:

git switch add-footer
git log --oneline
3f6b8a1 (HEAD -> add-footer) Add a footer with contact email
8b1e4d2 Style the landing page background and heading
7a9c3f1 Add landing page structure

Step 7: publish to GitHub

Create an empty repository named landing-page on GitHub, then connect and push:

git remote add origin https://github.com/your-username/landing-page.git
git push -u origin main
git push -u origin add-footer

Both branches, with their complete histories, are now visible on GitHub. Refresh the page and use the branch dropdown to flip between main and add-footer; the file list changes exactly as it did on your machine.

Step 8: inspect what you built

git log --oneline --graph --all

The --graph flag draws the branch structure and --all shows every branch, giving you a picture of the parallel lines of work you created in a few minutes.


21. Practice Exercises

  • Initialise a repository in an empty folder, then run git status before and after creating a file, and note the exact difference in the output.
  • Make three separate edits to a single HTML file and commit each one separately with a distinct message, then read the story back with git log --oneline.
  • Stage one of two modified files, commit it, and confirm with git status that the second file is still waiting in the working directory.
  • Create a branch named dark-theme, change the background colour in styles.css, commit, then switch to main and verify the colour is unchanged there.
  • Use git show <commit-id>:index.html to display an earlier version of a file, then use git restore --source to bring that version back to disk.
  • Write a .gitignore that excludes *.log and a folder named temp/, create both, and confirm they never appear in git status.
  • Break a file on purpose, run git restore on it, and confirm the committed version returns.
  • Push a branch to GitHub, edit a file through GitHub's web interface, then git pull locally and confirm the edit arrived.

22. Interview Questions and Answers

What is a repository in Git?

A repository is a project folder plus the complete history of that project, stored by Git in a hidden .git sub-folder. The history is a chain of commits, so any earlier state of any file can be retrieved. A project typically has a local repository on the developer's machine and a remote copy hosted on GitHub.

What is the difference between Git and GitHub?

Git is the version control software that runs on your computer and records history. GitHub is a hosting service that stores a remote copy of a Git repository on the internet, making it a shared hub for backup, collaboration and public portfolios. You use Git constantly; GitHub is where repositories meet.

What does git add do, and why is there a staging area?

git add copies changes from the working directory into the staging area, the waiting room for the next commit. The staging area exists so you can compose a commit precisely: when several unrelated edits sit in the working directory at once, you stage only the ones that belong to the story this commit should tell.

What information does a commit contain?

A commit stores a snapshot of the project's content, the author's name and email, a timestamp, the commit message, a unique hash ID, and the ID of its parent commit. The parent link is what chains commits into history, and the content-based hash means a stored commit cannot be altered silently.

What is a branch and why do developers use them?

A branch is a movable label pointing at one commit; it advances automatically as you commit. Developers use branches so that new work proceeds on its own line while the main branch stays stable and working. Because a branch is just a label, creating one is instant and costs no disk space.

What is HEAD?

HEAD is Git's "you are here" pointer. It normally points at the current branch, and the branch points at its newest commit, so HEAD says which commit your working directory currently reflects. Switching branches moves HEAD and rewrites the working directory to match.

What is the difference between git clone and git pull?

git clone creates a brand-new local repository by downloading a remote one, including its full history; you use it once, when joining a project. git pull updates a repository you already have, fetching commits others have pushed and merging them into your local branch.

How do you discard uncommitted changes to a file?

With git restore <file>, which returns the file to its last committed state. If the file is staged rather than merely modified, first unstage it with git restore --staged <file>. Discarding is permanent for those edits, so it is worth checking git diff first.

What is .gitignore used for?

It lists files and folders Git should not track, such as node_modules/, build output, log files and .env files holding secrets. The file itself is committed so every teammate shares the same rules. It only affects untracked files, so something committed earlier must be untracked before an ignore rule applies.

What is the difference between git revert and git reset?

git revert creates a new commit that reverses an earlier commit, so history keeps a record of both the mistake and its correction; it is the safe choice for commits that have already been pushed and shared. git reset moves the branch pointer backwards, rewriting local history, which is suitable only for commits nobody else has.

Why should commits be small and frequent?

Small commits describe one logical change, so their messages stay meaningful, code review stays manageable, and if something breaks you can pinpoint and undo exactly the faulty commit. Rare giant commits mix many changes, making both understanding and recovery difficult.

What happens if you switch branches while you have uncommitted changes?

Git blocks the switch if the edits would be overwritten by the target branch, protecting your work. If the edits do not conflict, they travel with you into the new branch, which is often not what you intended. The disciplined habit is to commit or stash your changes before switching.


23. Quick Revision Checklist

  • A repository is a project folder plus its full history in the hidden .git folder; git init creates one, git clone copies one.
  • Changes travel: working directory, git add, staging area, git commit, repository.
  • git status maps all three areas; run it before every commit.
  • A commit is a snapshot plus author, timestamp, message, hash ID and parent link.
  • git log --oneline reads history newest first; git diff shows what changed; git show inspects one commit.
  • A branch is a movable label on a commit; git switch -c creates and moves to one.
  • Keep main stable; do each task on its own branch with a descriptive name.
  • Commit before switching branches; stash with git stash if you must pause.
  • .gitignore keeps secrets and generated files out of the repository, and is committed itself.
  • Undo safely: git restore for files, git commit --amend for your own last commit, git revert for anything already pushed.
  • The team rhythm is pull, branch, small commits, push; origin names the GitHub remote.

Conclusion

Repositories, commits and branches are the three load-bearing ideas of version control, and everything else in Git is built on them. A repository holds one project and its entire history. A commit is a labelled snapshot that tells one clear story about a change. A branch is a movable label that lets parallel lines of work proceed without endangering each other.

Master the rhythm from this page — git status, git add, git commit, branch, push — and Git stops feeling like a risk and starts feeling like a safety net. The natural next question is how two branches come back together, and how teams review each other's work before it lands on main. That is exactly what the next sub-module, merging and pull requests, covers.

Learn more in the React JS Training course, or register for a course demo.

Previous Next
whatsapp