Five Git States and Their Sequential Operations
This article explains Git’s four areas (working, staging, local repository, remote repository) and five file states (origin, modified/untracked, staged, committed, pushed), detailing step‑by‑step commands such as git add, commit, push, and how to revert changes using git reset, checkout, and clean.
Sequential Operations Between Five States
Four Areas
Five States
Sequential Operations
Step 0: Working area and repository are consistent.
Step 1: Files are added, deleted or modified, entering the Modified state.
Step 2: git add moves files to the Staged state.
Step 3: git commit creates a commit, entering the Committed state.
Step 4: git push uploads the commit, entering the Pushed state.
Reverting Operations Between Five States
Modified but Not Staged
Staged but Not Committed
At this point git add has been executed but git commit has not, and git diff shows no changes.
Because git diff checks the difference between the working area and the staging area.
git reset --hard is equivalent to performing git reset followed by git checkout (a two‑step operation).
Committed but Not Pushed
After a commit, a version hash is generated in the repository; this hash can be used later to revert to that specific commit.
Pushed to Remote
Use with caution : normally the local branch is newer than the remote, so a normal git push suffices, but if you need to overwrite the remote (e.g., after a rollback or a divergent history), add the -f flag to force the push.
Summary
First, run git status to view the current state.
git reset can operate on the local repository to revert or advance to any version, or on the staging area to undo staged changes; it has --hard and --soft options. The soft option (or omitting it) leaves the reverted changes in the working area.
git checkout is used to revert deletions and modifications, while git clean -df reverts added files.
git diff shows changes and differences between the working area, staging area, and repository, with various parameters to control the scope.
© Copyright: Content sourced from the web; rights belong to the original author. If any infringement is found, please inform us and we will remove it promptly. Original link: https://geektutu.com/post/git-four-areas-five-states.html
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
