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).
| Command | What it does | Moves changes from |
|---|---|---|
git add | Stage the changes | Working directory to Staging area |
git commit | Save a snapshot | Staging area to Repository |
git restore --staged | Unstage the changes | Staging area to Working directory |
git restore | Discard the changes of a file | Repository 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
$ 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.
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |