Published 2月 22, 2024 by with 0 comment

Git Lab 03 - Recording Changes to the Repository




Lab 3.1: Checking the Status of Your Files
The main tool you use to determine which files are in which state is the git status command.
The test-1.txt file: a new file without staging it. (Untracked)

git status - Show the working tree status

Example Code:
$ git status
$ echo "test-1" > test-1.txt
$ git status


Lab 3.2: Tracking New Files
The test-1.txt file: a new file without staging it. (Untracked)
In order to begin tracking a new file, you use the command git add.
Now, the file test-1.txt is staged because it’s under the “Changes to be committed” heading.

git add - Add file contents to the index

Example Code:
$ git status
$ git add test-1.txt
$ git status


Lab 3.3: Staging Modified Files
The test-1.txt file: a new file with staging it. (Staged)

Let’s edit the test-1.txt file that was already tracked and check status. (Modified)
Example Code:
$ git status
$ echo "test-1, test-1" >> test-1.txt
$ git status

And then add a new file to your project, test-2.txt file and check status. (Untracked)
The test-2.txt file appears under a section named “Changes not staged for commit” which means that a file that is tracked has been modified in the working directory but not yet staged.
Example Code:
$ git status
$ echo "test-2" > test-2.txt
$ git status

The test-1.txt file: Stage it and check status. (Staged)
The test-2.txt file: a new file without staging it. (Untracked)
Example Code:
$ git status
$ git add text-1.txt
$ git status


Lab 3.4: Viewing Your Staged and Unstaged Changes
The test-1.txt file: Staged. (Staged)
The test-2.txt file: a new file without staging it. (Untracked)

git diff - Show changes between commits, commit and working tree, etc

I can use git diff --staged. This command compares your staged changes to your last commit.
Example Code:
$ git status
$ git diff
$ git diff --staged

Let’s edit a test-1.txt file that was already tracked. And check test-1.txt status. (Modified)
To see what you’ve changed but not yet staged, type git diff with no other arguments.
Example Code:
$ git status
$ echo "test-1, test-1, test-1" >> test-1.txt
$ git status
$ git diff


Lab 3.5: Committing Your Changes
The test-1.txt file: (Modified)
The test-2.txt file: a new file without staging it. (Untracked).
Let's staged every files (git add --all or git add -A) before commit.

git commit - Record changes to the repository
git commit -m [message]

Example Code:
$ git status
$ git add --all
$ git status
$ git commit -m "add test-1.txt, test-2.txt"
$ git status


Reference:
1. Git - Commands Reference

2. Git Basics - Recording Changes to the Repository

3. Git 教程


最初發表 / 最後更新: 2024-02-22 / 2024-02-22

0 comments:

張貼留言