这是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
15 changes: 13 additions & 2 deletions pro-git/chapter-02/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Chapter 2: Git Basics

## 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)
Expand All @@ -20,3 +18,16 @@
- [`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)
- [Undoing Things](undoing.md)
- [Working with Remotes](remotes.md)
- [Showing Your Remotes](remotes.md#showing-your-remotes)
- [Adding Remotes](remotes.md#adding-remotes)
- [Fetching and Pulling from Your Remotes](remotes.md#fetching-and-pulling-from-your-remotes)
- [Inspecting a Remote](remotes.md#inspecting-a-remote)
- [Renaming and Removing Remotes](remotes.md#renaming-and-removing-remotes)
- [Tagging](tagging.md)
- [Listing Your Tags](tagging.md#listing-your-tags)
- [Creating Tags](tagging.md#creating-tags)
- [Annotated Tags](tagging.md#annotated-tags)
- [Lightweight Tags](tagging.md#lightweight-tags)
- [Tagging Later](tagging.md#tagging-later)
122 changes: 122 additions & 0 deletions pro-git/chapter-02/remotes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
## Working with Remotes

### Showing Your Remotes

```shell
git remote
```

```shell
git clone git@github.com:schacon/ticgit.git
cd ticgit
git remote
```

```shell
origin
```

We can get more information about a remote by using the `--verbose` option:

```shell
git remote --verbose
```

```shell
origin git@github.com:schacon/ticgit.git (fetch)
origin git@github.com:schacon/ticgit.git (push)
```

### Adding Remotes

```shell
git remote add pb git@github.com:paulboone/ticgit
git remote --verbose
```

```shell
origin git@github.com:schacon/ticgit.git (fetch)
origin git@github.com:schacon/ticgit.git (push)
pb git@github.com:paulboone/ticgit (fetch)
pb git@github.com:paulboone/ticgit (push)
```

### Fetching and Pulling from Your Remotes

Now that we've added `pb` as a shortname to a different remote repository, we can fetch it:

```shell
git fetch pb
```

```shell
remote: Enumerating objects: 43, done.
remote: Counting objects: 100% (22/22), done.
remote: Total 43 (delta 22), reused 22 (delta 22), pack-reused 21 (from 1)
Unpacking objects: 100% (43/43), 5.99 KiB | 58.00 KiB/s, done.
From github.com:paulboone/ticgit
* [new branch] master -> pb/master
* [new branch] ticgit -> pb/ticgit
```

What this does is essentially pulling changes from the `pb` remote repository into our own repository. This includes all branches that `pb` has, but we don't on ours; you can then merge or inspect this changes at any time.

> [!TIP]
>
> - When you a repository is cloned from its remote, the shortname would always be `origin`.
> - The `git fetch` command is used to fetch changes from a remote repository. It doesn't merge the changes into your repository; that would have to be done manually.

### Inspecting a Remote

```shell
git remote show origin
```

```shell
* remote origin
Fetch URL: git@github.com:schacon/ticgit.git
Push URL: git@github.com:schacon/ticgit.git
HEAD branch: master
Remote branches:
master tracked
ticgit tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
```

### Renaming and Removing Remotes

Let's rename the shortname `pb` to `paul` with `git remote rename <current> <new>`:

```shell
git remote rename pb paul
```

```shell
Renaming remote references: 100% (2/2), done.
```

```shell
git remote --verbose
```

```shell
origin git@github.com:schacon/ticgit.git (fetch)
origin git@github.com:schacon/ticgit.git (push)
paul git@github.com:paulboone/ticgit (fetch)
paul git@github.com:paulboone/ticgit (push)
```

If — for whatever reason — you don't want to work with a remote repository anymore, you can remove it with the `git remote remove <shortname>` command:

```shell
git remote remove paul
git remote --verbose
```

```shell
origin git@github.com:schacon/ticgit.git (fetch)
origin git@github.com:schacon/ticgit.git (push)
```
154 changes: 154 additions & 0 deletions pro-git/chapter-02/tagging.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
## Tagging

### Listing your Tags

Let's run the following command to list the tags in the `git` repository:

```shell
git tag | head -n 2
```

```shell
gitgui-0.10.0
gitgui-0.10.1
```

The `git` repository contains more than 900 tags. If you want to limit your tag list to the `1.8.5` series, you can run the following command:

```shell
git tag --list 'v1.8.5*'
```

```shell
v1.8.5
v1.8.5-rc0
v1.8.5-rc1
v1.8.5-rc2
v1.8.5-rc3
v1.8.5.1
v1.8.5.2
v1.8.5.3
v1.8.5.4
v1.8.5.5
v1.8.5.6
```

### Creating Tags

There are two types of tags in `git`: _lightweight_ and _annotated_.

- A lightweight tag is very much like a branch that doesn't change. It's just a pointer to a specific commit.
- Annotated tags, however, are stored as full objects in the `git` database. They're checksummed; contain the tagger name, email, and date; have a tagging message; and can be signed and verified with GNU Privacy Guard (GPG). It's generally recommended that you create annotated tags so you can have all this information; but if you want a temporary tag or for some reason don't want to keep the other information, you can create lightweight tags with the `-a` option:

#### Annotated tags

To create an annotated tag in `git`, you can run the following command:

```shell
git tag --annotate v1.4 --message "My version 1.4"
git tag
```

```shell
v1.4
```

We passed in the `--message` option to provide a tagging message. In order to view the tag data that is stored in the `git` database, you can run the following command:

```shell
git show v1.4
```

```shell
tag v1.4
Tagger: Justin Alex Paramanandan <1155821+jusuchin85@users.noreply.github.com>
Date: Mon Nov 11 10:42:30 2024 +1100

My version 1.4

commit 05c9df45d270b2a83a3733b6334a36f35077ec5f (HEAD -> main, tag: v1.4, origin/main)
Author: Justin Alex Paramanandan <1155821+jusuchin85@users.noreply.github.com>
Date: Mon Nov 11 10:17:12 2024 +1100

Pushing latest updates
```

The `git show` command displays the tag data, the commit that was tagged, and the commit data.

#### Lightweight tags

Lightweight tags are created by running the following command:

```shell
git tag v1.4-lw
git tag
```

```shell
v1.4
v1.4-lw
```

Now if you run the `git show` command to view the tag data, you won't see the extra tag information:

```shell
git show v1.4-lw
```

```shell
commit 05c9df45d270b2a83a3733b6334a36f35077ec5f (HEAD -> main, tag: v1.4-lw, tag: v1.4, origin/main)
Author: Justin Alex Paramanandan <1155821+jusuchin85@users.noreply.github.com>
Date: Mon Nov 11 10:17:12 2024 +1100

Pushing latest updates
```

### Tagging Later

If you forgot to tag a commit, you can tag it later. Let's view the commit history for this repository:

```shell
git log --pretty=oneline
```

```shell
05c9df45d270b2a83a3733b6334a36f35077ec5f (HEAD -> main, tag: v1.4-lw, tag: v1.4, origin/main) Pushing latest updates
093b2f8f79255740a393ac72f28559454c077194 Initial commit
```

The `093b2f8` commit is the initial commit, and we forgot to tag it. To tag this commit, you can run the following command:

```shell
git tag --annotate v1.0 093b2f8
git tag
```

```shell
v1.0
v1.4
v1.4-lw
```

```shell
git show v1.0
```

```shell
tag v1.0
Tagger: Justin Alex Paramanandan <1155821+jusuchin85@users.noreply.github.com>
Date: Mon Nov 11 10:49:33 2024 +1100

This is the initial tag

commit 093b2f8f79255740a393ac72f28559454c077194 (tag: v1.0)
Author: Justin Alex Paramanandan <1155821+jusuchin85@users.noreply.github.com>
Date: Mon Nov 11 10:05:14 2024 +1100

Initial commit

Together with forgotten_file
```

### Sharing Tags

TODO: TBC
71 changes: 71 additions & 0 deletions pro-git/chapter-02/undoing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
## Undoing Things

### Amending the Last Commit

```shell
git commit -m "Initial commit"
git add forgotten_file
git commit --amend
```

> [!IMPORTANT]
>
> - The above command will only work if the forgotten file is not yet staged. If the forgotten file is already staged, you can unstage it with `git reset HEAD <file>` and then commit it with `git commit --amend`.
> - The `--amend` option will only work if you haven't pushed the commit to a remote repository yet. If you have pushed the commit to a remote repository, you will need to force push the amended commit with `git push --force`.

### Unstaging a Staged File

```shell
git add *
git status
```

```shell
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: forgotten_file -> CONTRIBUTING.md
modified: README.md
```

Let's unstage the `README.md` file:

```shell
git restore --staged README.md
git status
```

```shell
git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: forgotten_file -> CONTRIBUTING.md

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
```

### Unmodifying a Modified File

Let's undo all the changes made to the `README.md` file:

```shell
git restore README.md
git status
```

```shell
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: forgotten_file -> CONTRIBUTING.md
```