From 52b7aefe7960561bf3bf9ce474c05946d7a53378 Mon Sep 17 00:00:00 2001 From: Justin Alex Paramanandan <1155821+jusuchin85@users.noreply.github.com> Date: Mon, 11 Nov 2024 10:56:44 +1100 Subject: [PATCH 1/3] 2024-11-11: Pro Git - Undoing Commits - Working with Remotes - Tagging --- pro-git/chapter-02/README.md | 15 +++- pro-git/chapter-02/remotes.md | 122 +++++++++++++++++++++++++++ pro-git/chapter-02/tagging.md | 154 ++++++++++++++++++++++++++++++++++ pro-git/chapter-02/undoing.md | 71 ++++++++++++++++ 4 files changed, 360 insertions(+), 2 deletions(-) create mode 100644 pro-git/chapter-02/remotes.md create mode 100644 pro-git/chapter-02/tagging.md create mode 100644 pro-git/chapter-02/undoing.md diff --git a/pro-git/chapter-02/README.md b/pro-git/chapter-02/README.md index 03ec543..78f907b 100644 --- a/pro-git/chapter-02/README.md +++ b/pro-git/chapter-02/README.md @@ -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) @@ -20,3 +18,16 @@ - [`git log --since=`](log-output.md#git-log---sincen) - [`git log -S `](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) diff --git a/pro-git/chapter-02/remotes.md b/pro-git/chapter-02/remotes.md new file mode 100644 index 0000000..264e380 --- /dev/null +++ b/pro-git/chapter-02/remotes.md @@ -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 are 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 `: + +```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 ` 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) +``` diff --git a/pro-git/chapter-02/tagging.md b/pro-git/chapter-02/tagging.md new file mode 100644 index 0000000..b940993 --- /dev/null +++ b/pro-git/chapter-02/tagging.md @@ -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'll don'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 diff --git a/pro-git/chapter-02/undoing.md b/pro-git/chapter-02/undoing.md new file mode 100644 index 0000000..2ce38c2 --- /dev/null +++ b/pro-git/chapter-02/undoing.md @@ -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 ` 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 ..." 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 ..." to unstage) + renamed: forgotten_file -> CONTRIBUTING.md + +Changes not staged for commit: + (use "git add ..." to update what will be committed) + (use "git restore ..." 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 ..." to unstage) + renamed: forgotten_file -> CONTRIBUTING.md +``` From 4738a887df32a436e2b2c861d5f618ada9a79b9d Mon Sep 17 00:00:00 2001 From: Justin Alex Paramanandan <1155821+jusuchin85@users.noreply.github.com> Date: Mon, 11 Nov 2024 10:58:37 +1100 Subject: [PATCH 2/3] Update pro-git/chapter-02/tagging.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- pro-git/chapter-02/tagging.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pro-git/chapter-02/tagging.md b/pro-git/chapter-02/tagging.md index b940993..aadff23 100644 --- a/pro-git/chapter-02/tagging.md +++ b/pro-git/chapter-02/tagging.md @@ -89,7 +89,7 @@ v1.4 v1.4-lw ``` -Now if you run the `git show` command to view the tag data, you'll don't see the extra tag information: +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 From 7f0b1ad36f0ca5a3c6a0989c107ce9d6e43799c6 Mon Sep 17 00:00:00 2001 From: Justin Alex Paramanandan <1155821+jusuchin85@users.noreply.github.com> Date: Mon, 11 Nov 2024 11:00:09 +1100 Subject: [PATCH 3/3] Update pro-git/chapter-02/remotes.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- pro-git/chapter-02/remotes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pro-git/chapter-02/remotes.md b/pro-git/chapter-02/remotes.md index 264e380..49b1139 100644 --- a/pro-git/chapter-02/remotes.md +++ b/pro-git/chapter-02/remotes.md @@ -59,7 +59,7 @@ From github.com:paulboone/ticgit * [new branch] ticgit -> pb/ticgit ``` -What this does are 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. +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] >