Git Workflow

To use Git well, you must understand where your files live at each step. Git has three main areas, and a file moves from one to the next.

The three areas of Git

  • Working directory: the normal project folder on your computer, where you create and edit files.
  • Staging area (index): a waiting room. Here you put the changes you want to include in the next commit.
  • Repository (.git folder): the place where Git saves the commits (the history).
CommandWhat it doesMoves changes from
git addStage the changesWorking directory to Staging area
git commitSave a snapshotStaging area to Repository
git restore --stagedUnstage the changesStaging area to Working directory
git restoreDiscard the changes of a fileRepository to Working directory

Why is there a staging area?

The staging area lets you choose exactly what goes to a commit. Suppose you changed five files, but only two of them are for one bug fix. You stage those two and commit them, and the other three are left for a different commit. This keeps the history clean.

File states

A file in a Git project is always in one of these states:

  • Untracked: a new file, Git does not know about it yet.
  • Modified: a tracked file which you changed, but not staged yet.
  • Staged: the change is marked for the next commit.
  • Committed: the change is safely saved in the repository.

The basic workflow in action

Terminal output

$ mkdir shop
$ cd shop
$ git init
Initialized empty Git repository in /home/robin/shop/.git/
$ echo "Apples" > list.txt
$ git status –short
?? list.txt
$ git add list.txt
$ git status –short
A list.txt
$ git commit -m "Add shopping list"
[main (root-commit) 97a022c] Add shopping list
1 file changed, 1 insertion(+)
create mode 100644 list.txt
$ git status –short
$ echo "Bananas" >> list.txt
$ git status –short
M list.txt

See how the first letters change. ?? means untracked, A means added (staged), nothing means clean, and M means modified but not staged.

Note: The normal daily loop is: edit files, git add, git commit, and sometimes git push. Next we will start with git init.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.