这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 20 additions & 150 deletions pro-git/chapter-02/README.md
Original file line number Diff line number Diff line change
@@ -1,152 +1,22 @@
# Chapter 2: Git Basics

## Lifecycle of the Status of a File

```mermaid
sequenceDiagram
participant utrack as Untracked
participant umod as Unmodified
participant mod as Modified
participant stage as Staged

utrack->>stage: Add the file
umod->>mod: Edit the file
mod->>stage:Stage the file
umod->>utrack:Remove the file
stage->>umod:Commit
```

## More on the `git status` Command

- `git status`: Show the status of changes as untracked, modified, or staged.

```shell
git status
```

```shell
On branch 2024-10-21_pro-git
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
modified: pro-git/chapter-01/README.md

Untracked files:
(use "git add <file>..." to include in what will be committed)
pro-git/chapter-02/

no changes added to commit (use "git add" and/or "git commit -a")
```

- Shorter `git status`:

```shell
git status --short
```

```shell
M README.md
M pro-git/chapter-01/README.md
?? pro-git/chapter-02/
```

- the first column represents the status of the staging area[^1]
- the second column represents the status of the working directory[^1]

## Ignoring Files

Rules of `.gitignore` files:

- Blank lines or lines starting with `#` are ignored.
- Standard [glob patterns](#glob-patterns) work, and will be applied recursively throughout the entire working directory (e.g. `*.log`).
- You can start patterns with a forward slash (`/`) to avoid recursivity (e.g. `/*.log`).
- You can end patterns with a forward slash (`/`) to specify a directory (e.g. `log/`).
- You can negate a pattern by starting it with an exclamation point (`!`).

### Glob patterns

Glob patterns are regex[^2] for shell environments. Here are some examples:

- `*`: matches zero or more characters
- `[abc]`: matches any of the characters a, b, or c
- `?`: matches a single character
- brackets enclosing characters separated by a hyphen (`-`) represent a range (e.g. `[1-5]` matches any digit from 1 to 5)
- two asterisks (`**`) represent any number of directories (e.g. `a/**/z` matches `a/z`, `a/b/z`, `a/b/c/z`, etc.)

### Example `.gitignore` file[^3]

```gitignore
# ignore all .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in any directory named build
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf file in the doc/ directory and any of its subdirectories
doc/**/*.pdf
```

## Compare Changes Made with `git diff`

Use `git status` as a primer for this:

```shell
git status
```

```shell
On branch 2024-10-21_pro-git
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
modified: pro-git/chapter-01/README.md

Untracked files:
(use "git add <file>..." to include in what will be committed)
pro-git/chapter-02/

no changes added to commit (use "git add" and/or "git commit -a")
```

Let's check what was changed in `README.md`:

```shell
git diff README.md
```

```shell
diff --git a/README.md b/README.md
index 74889a8..b4e04ff 100644
--- a/README.md
+++ b/README.md
@@ -6,4 +6,4 @@ Contains a collection of READMEs for the various areas of my learning. Some exer

- [GNU `grep` and `ripgrep`](gnu-grep/)
- [LDAP](ldap/)
-- [Pro Git](pro-git/)
\ No newline at end of file
+- [Pro Git](pro-git/)
```

## Removing Files from the Staging Area, but Retaining in the Working Directory

```shell
git rm --cached FILENAME
```

This way, the file will no longer be tracked, but you want to retain in the working directory — perhaps to be added to the `.gitignore` file.

[^1]: [See Chapter 01](../chapter-01/README.md) for more information on the staging area and the working directory.
[^2]: Regular expressions
[^3]: [Examples from GitHub](https://github.com/github/gitignore)
## Table of Contents

- [Lifecycle of the Status of a File](lifecycle.md)
- [More on the `git status` Command](status.md)
- [Ignoring Files](ignoring-files.md)
- [Glob Patterns](ignoring-files.md#glob-patterns)
- [Example `.gitignore` file](ignoring-files.md#example-gitignore-file)
- [Compare Changes Made with `git diff`](diff-rm.md#compare-changes-made-with-git-diff)
- [Removing Files from the Staging Area, but Retaining in the Working Directory](diff-rm.md#removing-files-from-the-staging-area-but-retaining-in-the-working-directory)
- [Checking Commit History](commit-history.md#checking-commit-history)
- [`git log`](commit-history.md#git-log)
- [`git log --patch -<n>`](commit-history.md#git-log---patch--n)
- [`git log --stat`](commit-history.md#git-log---stat)
- [`git log --pretty=<option>`](commit-history.md#git-log---prettyoption)
- [Combining the `--pretty` option with the `--graph` option](commit-history.md#combining-the---pretty-option-with-the---graph-option)
- [Common options to `git-log`](commit-history.md#common-options-to-git-log)
- [Limiting Log Output](log-output.md)
- [`git log --since=<n>`](log-output.md#git-log---sincen)
- [`git log -S <string>`](log-output.md#git-log--s-string)
- [Common options to limit output to `git-log`](log-output.md#common-options-to-limit-output-to-git-log)
164 changes: 164 additions & 0 deletions pro-git/chapter-02/commit-history.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
## Checking Commit History

> [!TIP]
> More options for `git log` can be found here [^1].

### `git log`

Show the commit history:

```shell
commit ca82a6dff817ec66f44342007202690a93763949 (HEAD -> master, origin/master, origin/HEAD)
Author: Scott Chacon <schacon@gmail.com>
Date: Mon Mar 17 21:52:11 2008 -0700

changed the verison number

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <schacon@gmail.com>
Date: Sat Mar 15 16:40:33 2008 -0700

removed unnecessary test code

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon <schacon@gmail.com>
Date: Sat Mar 15 10:31:28 2008 -0700

first commit
```

### `git log --patch -<n>`

Show the commit history with the diff of each commit (with `<n>` being the last `n` entries to show):

```shell
commit ca82a6dff817ec66f44342007202690a93763949 (HEAD -> master, origin/master, origin/HEAD)
Author: Scott Chacon <schacon@gmail.com>
Date: Mon Mar 17 21:52:11 2008 -0700

changed the verison number

diff --git a/Rakefile b/Rakefile
index a874b73..8f94139 100644
--- a/Rakefile
+++ b/Rakefile
@@ -5,7 +5,7 @@ require 'rake/gempackagetask'
spec = Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.name = "simplegit"
- s.version = "0.1.0"
+ s.version = "0.1.1"
s.author = "Scott Chacon"
s.email = "schacon@gmail.com"
s.summary = "A simple gem for using Git in Ruby code."

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <schacon@gmail.com>
Date: Sat Mar 15 16:40:33 2008 -0700

removed unnecessary test code

diff --git a/lib/simplegit.rb b/lib/simplegit.rb
index a0a60ae..47c6340 100644
--- a/lib/simplegit.rb
+++ b/lib/simplegit.rb
@@ -18,8 +18,3 @@ class SimpleGit
end

end
-
-if $0 == __FILE__
- git = SimpleGit.new
- puts git.show
-end
```

### `git log --stat`

Show the commit history with the number of insertions and deletions in each commit:

```shell
commit ca82a6dff817ec66f44342007202690a93763949 (HEAD -> master, origin/master, origin/HEAD)
Author: Scott Chacon <schacon@gmail.com>
Date: Mon Mar 17 21:52:11 2008 -0700

changed the verison number

Rakefile | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <schacon@gmail.com>
Date: Sat Mar 15 16:40:33 2008 -0700

removed unnecessary test code

lib/simplegit.rb | 5 -----
1 file changed, 5 deletions(-)

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon <schacon@gmail.com>
Date: Sat Mar 15 10:31:28 2008 -0700

first commit

README | 6 ++++++
Rakefile | 23 +++++++++++++++++++++++
lib/simplegit.rb | 25 +++++++++++++++++++++++++
3 files changed, 54 insertions(+)
```

### `git log --pretty=<option>`

Show the commit history in one line. Options include:

- `oneline`: show the commit hash and the commit message in one line

```shell
git log --pretty=oneline

ca82a6dff817ec66f44342007202690a93763949 (HEAD -> master, origin/master, origin/HEAD) changed the verison number
085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test code
a11bef06a3f659402fe7563abf99ad00de2209e6 first commit
```

- `format:"%h - %an, %ar : %s"`: show the abbreviated commit hash (`%h`), the author name (`%an`), the author date, relative (`%ar`), and the subject (`%s`) of the commit

```shell
git log git log --pretty=format:"%h - %an, %ad : %s"

ca82a6d - Scott Chacon, 7 years ago : changed the verison number
085bb3b - Scott Chacon, 7 years ago : removed unnecessary test code
a11bef0 - Scott Chacon, 7 years ago : first commit
```

## Combining the `--pretty` option with the `--graph` option

Combining both the `--pretty` and `--graph` options will show the commit history in a graph format. Below is an example of the commit history of this repository:

```shell
git log --pretty=format:"%h %s" --graph

* 0931157 2024-10-21: Pro Git Learnings (#3)
* 9540054 Merge branch 'main' into development
|\
| * cbf9b53 First PR for LDAP (#2)
* | ed64291 First PR for `gnu grep` (#1)
|/
* fe49b8b Initial commit
```

### Common options to `git-log`

- `-p` or `--patch`: Show the patch introduced with each commit.
- `--stat`: Show statistics for files modified in each commit.
- `--shortstat`: Show only the changed/insertions/deletions line from the `--stat` option.
- `--name-only`: Show the list of files modified after the commit information.
- `--name-status`: Show the list of files modified after the commit information, indicating the status of the changes.
- `--abbrev-commit`: Show only the first few characters of the SHA-1 checksum instead of all 40.
- `--relative-date`: Display the date in a relative format (e.g. "2 weeks ago") instead of using the full date format.
- `--graph`: Show an ASCII graph of the branch and merge history beside the log output.
- `--pretty`: Show commits in an alternate format. Options include `oneline`, `short`, `full`, `fuller`, `format`, and `email`.
- `--oneline`: Shorthand for `--pretty=oneline --abbrev-commit` used together.

[^1]: [Manual for `git-log`](https://git-scm.com/docs/git-log)
50 changes: 50 additions & 0 deletions pro-git/chapter-02/diff-rm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## Compare Changes Made with `git diff`

Use `git status` as a primer for this:

```shell
git status
```

```shell
On branch 2024-10-21_pro-git
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
modified: pro-git/chapter-01/README.md

Untracked files:
(use "git add <file>..." to include in what will be committed)
pro-git/chapter-02/

no changes added to commit (use "git add" and/or "git commit -a")
```

Let's check what was changed in `README.md`:

```shell
git diff README.md
```

```shell
diff --git a/README.md b/README.md
index 74889a8..b4e04ff 100644
--- a/README.md
+++ b/README.md
@@ -6,4 +6,4 @@ Contains a collection of READMEs for the various areas of my learning. Some exer

- [GNU `grep` and `ripgrep`](gnu-grep/)
- [LDAP](ldap/)
-- [Pro Git](pro-git/)
\ No newline at end of file
+- [Pro Git](pro-git/)
```

## Removing Files from the Staging Area, but Retaining in the Working Directory

```shell
git rm --cached FILENAME
```

This way, the file will no longer be tracked, but you want to retain in the working directory — perhaps to be added to the `.gitignore` file.
Loading