Chinese language support

(1)installing-git-arch.zh-cn.md
(2)installing-git-ubuntu.zh-cn.md
(3)keeping-your-fork-synced-with-this-repository.zh-cn.md
(4)resetting-a-commit.zh-cn.md
(5)squashing-commits.zh-cn.md
(6)stashing-a-file.zh-cn.md
(7)storing-credentials.zh-cn.md
This commit is contained in:
jasontjiunardi
2025-05-13 15:16:13 +08:00
parent b7e061b81c
commit 42242c1aa9
7 changed files with 551 additions and 0 deletions
@@ -0,0 +1,83 @@
# 在 Arch Linux 上安装 Git
要在 Arch Linux 上安装 Git,可以使用包管理器 pacman。首先,打开终端并使用以下命令更新系统:
```shell
$ sudo pacman -Syu
```
接下来,运行以下命令安装 Git
```shell
$ sudo pacman -S git
```
要确认 Git 是否正确安装,运行以下命令:
```shell
$ git --version
```
你应该会看到类似以下的输出:
```shell
Output
$ git version 2.34.1
```
# 设置 Git
配置可以通过使用 git config 命令来完成。
具体来说,你需要提供你的名字和电子邮件地址,因为 Git 会将这些信息嵌入到你做的每个提交中。
你可以通过输入以下命令来添加这些信息:
现在我们已经完成了 Git 的安装,让我们使用 "git config" 命令配置 Git 以供首次使用。
我们需要确保你的用户名和电子邮件地址设置正确。要设置它们,使用以下命令:
```shell
$ git config --global user.name "Your Name"
$ git config --global user.email "youremail@domain.com"
```
你可以通过在终端中输入以下命令来显示所有已设置的配置项:
```shell
$ git config --list
```
如果所有配置字段已按照你的需求设置,输出应该类似于:
```shell
user.name=Your Name
user.email=youremail@domain.com
```
# 持久化 Git 凭证
默认情况下,Git 每次与远程仓库交互时都会提示你重新输入用户名和密码。你可以配置 Git 来缓存或存储你的凭证,以避免这种情况。以下是两种常用的方法:
### 1. 凭证缓存
Git 可以将你的凭证暂时存储在内存中,这样你就不需要频繁地重新输入它们。运行以下命令启用凭证缓存:
```shell
$ git config --global credential.helper cache
```
默认情况下,凭证会缓存 15 分钟。要调整超时时间(例如,1 小时),可以使用:
```shell
$ git config --global credential.helper 'cache --timeout=3600'
```
---
### 2. 凭证存储
如果你更倾向于将凭证永久存储为明文(不太安全,但方便),可以使用以下命令:
```shell
$ git config --global credential.helper store
```
使用此方法时,你的凭证将以明文形式保存在 `~/.git-credentials` 文件中。特别是在共享或公共计算机上使用此方法时,请小心操作。
@@ -0,0 +1,104 @@
# 在 Ubuntu OS 上安装 Git
默认情况下,Git 很可能已经在你的 Ubuntu 操作系统中安装好了。你可以通过打开终端并输入以下命令来确认:
```shell
$ git --version
```
如果你看到类似下面的输出,那么恭喜你!你已经成功安装了 Git。
```shell
Output
$ git version 2.34.1
```
如果适用于你,接下来可以继续进行 Git 配置,去[设置 Git](#设置-Git)。
如果输出中没有显示 Git 版本号,你仍然可以通过 Ubuntu 的 APT 包管理器来安装 Git。
首先,通过使用 apt 包管理工具更新本地包索引。返回到你的终端并输入以下命令。
```shell
$ sudo apt update
```
完成后,输入以下命令来安装 Git
```shell
$ sudo apt install git
```
你可以通过运行以下命令并检查是否收到相关输出,来确认 Git 是否已正确安装:
```shell
$ git --version
```
```shell
Output
$ git version 2.34.1
```
Git 成功安装后,接下来可以配置 Git。
# 设置 Git
配置可以通过使用 git config 命令来完成。
具体来说,你需要提供你的名字和电子邮件地址,因为 Git 会将这些信息嵌入到你做的每个提交中。
你可以通过输入以下命令来添加这些信息:
现在我们已经完成了 Git 的安装,让我们使用 "git config" 命令配置 Git 以供首次使用。
我们需要确保你的用户名和电子邮件地址设置正确。要设置它们,使用以下命令:
```shell
$ git config --global user.name "Your Name"
$ git config --global user.email "youremail@domain.com"
```
你可以通过在终端中输入以下命令来显示所有已设置的配置项:
```shell
$ git config --list
```
如果所有配置字段已按照你的需求设置,输出应该类似于:
```shell
user.name=Your Name
user.email=youremail@domain.com
```
...
# 持久化 Git 凭证
默认情况下,Git 会在每次你推送到远程仓库时要求你输入用户名和密码。
在 Git 中,你可以配置凭证缓存,以避免每次输入用户名和密码。以下是实现这一目标的几种方法:
1. 凭证缓存:Git 提供了一个凭证缓存系统,可以在指定的时间内将你的凭证存储在内存中。这样,你就不需要每次与远程仓库交互时重新输入凭证。
要启用凭证缓存,你可以使用以下命令:
```shell
$ git config --global credential.helper cache
```
默认情况下,Git 会将凭证缓存 15 分钟。你可以通过指定 --timeout 选项并跟上所需的秒数来调整缓存超时时间。
例如,要将缓存超时设置为 1 小时(3600 秒),可以使用:
```shell
$ git config --global credential.helper 'cache --timeout=3600'
```
2. 凭证存储:这将 Git 的凭证助手设置为 "store"。使用这个凭证助手时,Git 会将远程仓库的凭证存储在磁盘上的一个明文文件中。这种方法是最简单的,但存储明文凭证的方式也是最不安全的。
```shell
$ git config --global crednetial.helper store
```
使用存储凭证助手时,输入的凭证会永久保存在 Linux 或 macOS 上的 ~/.git-credentials 文件中,或 Windows 上的 %USERPROFILE%\.git-credentials 文件中。这些凭证将以明文格式存储,这意味着如果有人获取到该文件,就可以读取凭证。
使用存储凭证助手的优点是,你每次与远程仓库交互时,不需要再次输入凭证。然而,特别是在使用共享或公共计算机时,请注意存储明文凭证的安全隐患。
@@ -0,0 +1,45 @@
# 保持你的分叉与该仓库同步
首先,应该理解完整同步的流程,这一点非常重要。在这个流程中,有三个不同的仓库:我的公共仓库在 GitHub 上 `github.com/firstcontributions/first-contributions.git`,你在 GitHub 上的仓库分叉 `github.com/Your-Name/first-contributions/`,以及你本地机器上的仓库,你应该在其中进行工作。这种合作方式通常用于开源项目,称为 `Triangle Workflows`
<img style="float;" src="https://firstcontributions.github.io/assets/additional-material/triangle_workflow.png" alt="triangle workflow" />
为了保持你的两个仓库与我的公共仓库同步,我们首先需要将公共仓库的内容拉取并与本地机器上的仓库合并。
我们的第二步是将你的本地仓库推送到你的 GitHub 分叉。如前所述,只有通过你的分叉你才能发起一个“拉取请求”。因此,你的 GitHub 分叉是最后更新的仓库。
现在,让我们看看如何做到这一点:
首先,你必须确保自己处于主分支上。要知道自己当前在哪个分支,可以检查的第一行:
```
git status
```
如果你不在主分支上,输入以下命令切换到主分支:
```
git checkout main
```
然后,你应该将我的公共仓库添加到你的 Git 仓库中,使用 `add upstream remote-url`
```
git remote add upstream https://github.com/firstcontributions/first-contributions.git
```
这告诉 Git,指定的 URL 位置有该项目的另一个版本,并且我们将其命名为 `upstream`。一旦你的 Git 配置了上游仓库,你就可以拉取公共仓库的最新版本:
```
git fetch upstream
```
你刚刚拉取了我仓库的最新版本(`upstream` 远程仓库)。现在,你需要将公共仓库的内容合并到你的主分支中:
```
git rebase upstream/main
```
在这里,你正在将公共仓库合并到你的主分支。现在,你本地机器上的主分支已更新。最后,如果你将主分支推送到你的 GitHub 分叉,那么你的 GitHub 分叉也会更新:
```
git push origin main
```
请注意,这里你推送的是名为 `origin` 的远程仓库。
如果你想同时将我仓库的最新更改(`upstream` 远程仓库)拉取并合并到你本地的分支中,可以直接使用:
```
git pull upstream main
```
到目前为止,你的所有仓库都已更新。做得很好!每当你的 GitHub 仓库提示你比公共仓库落后几个提交时,你都应该执行这些操作。
@@ -0,0 +1,20 @@
# 重置一个提交
```reset``` 是一个用于将仓库回退到之前某个提交的命令,丢弃该提交之后的所有更改。<br/>
重置和撤销提交的主要区别在于,git reset ```取消暂存文件并将我们的更改带回工作目录```
而 git revert ```从远程仓库中删除提交```。<br/>
```git reset``` 可以通过以下命令来实现:
- 以下命令将以两个参数的方式给出所有提交的摘要:
- 提交哈希的前七个字符 - 这是我们在 **reset** 命令中需要引用的内容。
- 提交信息
```
git log --oneline
```
- 可以使用以下命令将仓库重置到特定的提交:<br />
```git reset commithash```
其中 commithash 是我们在日志中找到的提交哈希的前 7 个字符。
@@ -0,0 +1,86 @@
# What is squashing?
In git, squashing refers to rewriting the history of your commits, so you end up with one commit with a description of the changes done.
It's usually done in open source projects because a lot of the history of a branch in open source projects is only relevant to the developer who created it, and this provides a simpler way to describe the changes made and also revert them if needed.
# How do you squash commits?
First, perform a git log to review the commits you would like to merge in your current branch.
```
git log
```
You should see a series of your commits like so:
```
commit blablabla
Author: omguhh
Date: 10/10/20
Commit message 1
commit blablabla2
Author: omguhh
Date: 10/10/20
Commit message 2
```
So now that you see the commits you wish to merge to one, we can move along into doing that with ```git rebase```. Assuming you're already familiar with ```git rebase```, we can starting squashing commits in the interactive mode of git rebase that you can activate like so:
```
git rebase -i
```
Now, with interactive rebasing you can specify the starting and end point of how far back you want to go with commits like so:
```
git rebase -i HEAD~2
```
Running this command will show you something like the following:
```
pick blablabla Changing test01.txt file
pick blablabla2 Adding dummy01.txt file
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
```
So if you want to squash ```blablabla2``` into ```blablablabla```, you would change the following :
```
pick blablabla Changing test01.txt file
squash blablabla2 Adding dummy01.txt file
```
If all goes well, you'd get a result that looks like this:
```
# This is a combination of 2 commits.
# The first commit's message is:
commit message 1
# This is the 2nd commit message:
commit message 2
```
That you can freely change before you decide to exit the editor to save these changes.
Running git log again should show you the commit message you entered before exiting the screen with the commits combined into one.
@@ -0,0 +1,161 @@
# 使用 Git Stash 暂存工作进度
如果你正在进行一个大型开发任务,突然需要切换分支去做其他事情,但当前代码还没写完、也没有测试,
你可能并不希望提交这些不完整的更改。可 Git 不允许你直接切换分支,除非先处理这些更改。
那该怎么办呢?如何避免提交未完成的代码,同时还能自由切换分支?
这就是本教程要讲解的内容。
## 暂存你的工作(Stashing
假设你在项目的某个分支中修改了一些文件,此时运行 ```git status``` 可以看到:
```
$ git status
# 当前分支:master
# 暂存区中的更改:
# (使用 "git reset HEAD <file>..." 来取消暂存)
#
# 修改: index.html
#
# 未暂存的更改:
# (使用 "git add <file>..." 来更新将要提交的内容)
#
# 修改: lib/simplegit.rb
#
```
此时你想切换分支,但又不想提交更改。那就使用 ```git stash```:
```
$ git stash
Saved working directory and index state \
"WIP on master: 049d078 added the index file"
HEAD is now at 049d078 added the index file
(要恢复这些更改,输入 "git stash apply")
```
现在你的工作目录是干净的,可以使用 ```git status``` 查看:
```
$ git status
# 当前分支:master
没有要提交的内容,工作目录干净
```
此时你可以切换到任意分支继续开发。你 stash 的内容被保存在一个栈(stack)中。你可以使用 ```git stash list``` 查看所有保存的 stash
```
$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
stash@{2}: WIP on master: 21d80a5 added number to log
```
如果你想重新应用刚刚保存的 stash,可以使用 ```git stash apply```。默认情况下,它会应用最近一次保存的 stash。
如果你想应用指定的 stash,可以使用命令 ```git stash apply <stash-name>```,将 `<stash-name>` 替换为对应名称:
```
$ git stash apply
# 当前分支:master
# 未暂存的更改:
# (使用 "git add <file>..." 来更新将要提交的内容)
#
# 修改: index.html
# 修改: lib/simplegit.rb
#
```
你会发现 Git 恢复了你在执行 stash 时未提交的更改。
在这个示例中,你在应用 stash 时处于干净的工作目录,且是在与 stash 创建时相同的分支;
但请注意:**并不要求工作目录必须干净,也不需要在原分支才能成功应用 stash。**
你可以在一个分支中保存 stash,之后切换到另一个分支并重新应用它。
即使当前工作目录中存在未提交的更改,也可以应用 stash;但如果某些内容无法干净地应用,Git 会提示合并冲突。
文件中的更改虽然恢复了,但之前已暂存(staged)的文件并没有恢复到暂存区。
要恢复这些被暂存的更改,你需要使用带有 ```--index``` 参数的 ```git stash apply```
```
$ git stash apply --index
# 当前分支: master
# 已暂存更改:
# (使用 "git reset HEAD <file>..." 取消暂存)
#
# 修改: index.html
#
# 未暂存更改:
# (使用 "git add <file>..." to update what will be committed)
#
# 修改: lib/simplegit.rb
#
```
`apply` 命令仅仅是恢复 stash 内容,它不会自动从 stash 栈中移除对应条目。
如果你想删除某个 stash,可以使用 ```git stash drop``` 并指定 stash 名称:
```
$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
stash@{2}: WIP on master: 21d80a5 added number to log
$ git stash drop stash@{0}
Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43)
```
你也可以使用 ```git stash pop``` 命令,它会应用最后一次 stash 的内容并将其从栈中删除。
## 取消应用已应用的 StashUn-applying
有时你应用了 stash,做了一些工作,但之后想要**撤销**刚刚恢复的 stash 更改。
Git 并没有内建 ```git unapply``` 命令,但你可以使用“反向补丁”来实现类似效果:
```$ git stash show -p stash@{0} | git apply -R```
如果不指定 stash,Git 默认使用最新的 stash
```$ git stash show -p | git apply -R```
你也可以为此配置一个快捷别名:
```
$ git config --global alias.stash-unapply '!git stash show -p | git apply -R'
$ git stash apply
$ #... 进行工作
$ git stash-unapply
```
## 从 Stash 创建新分支
如果你 stash 了某些更改,但后来继续在该分支上进行开发,
再次应用 stash 时可能会因为文件已被修改而引发**冲突**。
如果你想更方便地重新测试 stash 的内容,可以使用 ```git stash branch``` 命令。
它会执行以下操作:
1. 创建一个新分支;
2. 回到你 stash 时所在的提交;
3. 应用 stash 内容;
4. 应用成功后自动删除 stash。
示例:
```
$ git stash branch testchanges
Switched to a new branch "testchanges"
# 当前分支: testchanges
# 已暂存的更改:
# (使用 "git reset HEAD <file>..." 取消暂存)
#
# 修改: index.html
#
# 未暂存的更改:
# (使用 "git add <file>..." 来更新将要提交的内容)
#
# 修改: lib/simplegit.rb
#
Dropped refs/stash@{0} (f0dfc4d5dc332d1cee34a634182e168c4efc3359)
```
这是一个非常实用的快捷方式,可以轻松恢复你 stash 的内容,并在一个新分支中继续开发。
@@ -0,0 +1,52 @@
# 存储凭据(用户名与密码)
你可能遇到过这样的烦恼——每次访问仓库都要输入用户名和密码,这很麻烦,而且若耗时过长还会打断你的工作流。
但其实没必要如此繁琐。
这里我们介绍一种常见的方式: [git credential cache](https://git-scm.com/docs/git-credential-cache)。
**注意:** 请遵循你所在单位或学校的安全策略。
## 凭据缓存(Caching
我们可以使用 Git 的 credential cache 来存储用户名和密码。
**警告:** 此方法会将凭据以*明文*形式保存在你电脑的硬盘上。
任何人都可以访问该文件,比如恶意的 NPM 模块。
### 全局凭据缓存
如果你希望为所有仓库启用凭据缓存,只需执行以下命令:
```
$ git config --global credential.helper cache
```
**提醒:** 请遵循你所在单位或学校的安全策略。
### 仓库级别的凭据缓存
如果你只想为当前仓库启用缓存,可以使用以下命令:
```
$ git config credential.helper cache
```
**提醒:** 请遵循你所在单位或学校的安全策略。
### 缓存超时时间
如果不指定缓存时间,凭据可能会被永久保留在内存中。
你可以通过以下命令设置缓存的持续时间(单位为秒):
```
git config credential.helper 'cache --timeout=<timeout>'
```
使用此 helper,凭据只会存储在内存中,不会写入磁盘,且在指定时间后会自动清除。
默认超时时间是 900 秒(15 分钟)。
#### 参考资料:
[Stack Overflow](https://stackoverflow.com/questions/35942754/how-can-i-save-username-and-password-in-git)
### [附加材料](additional-material.md)