Git Lifecycle

GIT Tutorials


The Git lifecycle describes how a file moves from your working directory to the staging area, local history, and a remote repository. Understanding each state helps you choose the right command and avoid committing unintended work.

The Four Git Areas

Area What It Contains Useful Command
Working directory Files you can edit, including untracked and modified files git diff
Staging area The exact changes selected for the next commit git diff --staged
Local repository Committed snapshots stored in the .git directory git log --oneline
Remote repository Branches shared through a server such as GitHub or GitLab git push

Key point: A file can contain both staged and unstaged changes. Git tracks changes, not only whole-file labels.

Start with an Untracked File

Create a repository and a new file:

git init -b main
echo "# Task Board" > README.md
git status --short --branch

Example output:

## No commits yet on main
?? README.md

?? means Git sees the file but does not track it yet.

Stage the File

Add the file to the staging area:

git add README.md
git status --short

Example output:

A  README.md

A in the first column means the file is staged as a new file. Review the staged patch before committing:

git diff --staged

Commit the Staged Snapshot

A commit records the current staging area, not every change in the working directory:

git commit -m "Add project readme"

Example output:

[main (root-commit) 3ac12f8] Add project readme
 1 file changed, 1 insertion(+)
 create mode 100644 README.md

After the commit, a clean status means the working directory and staging area match HEAD:

git status --short --branch

Example output:

## main

Modify a Tracked File

Edit README.md, then inspect its state:

git status --short

Example output:

 M README.md

M in the second column means the working-tree file differs from the staged copy.

Understand Staged and Unstaged Changes Together

Stage the current edit, then make another edit to the same file. Short status can show both states:

git add README.md
# Edit README.md again
git status --short

Example output:

MM README.md

The first M represents a staged change. The second represents a newer unstaged change. Use git diff --staged for the first and git diff for the second.

Unstage or Discard Changes

Unstage a path while keeping its edits:

git restore --staged README.md

Discard unstaged edits only when you no longer need them:

git restore README.md

Warning: The second command replaces uncommitted working-tree edits. Review git diff first.

Push Commits to a Remote

Add a remote once, then push main and set its upstream:

git remote add origin https://github.com/example/task-board.git
git push -u origin main

Example output:

To https://github.com/example/task-board.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

Git pushes commits, not uncommitted working-tree or staging-area changes.

Inspect Local and Remote State

Fetch remote references without changing your current branch:

git fetch origin
git status --short --branch

Example output:

## main...origin/main

If the branches differ, status can report how many commits the local branch is ahead or behind.

Conclusion

Git separates editing, staging, committing, and sharing. Check status often, review both unstaged and staged diffs, and remember that only committed snapshots can be pushed to a remote repository.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram