Merge pull request #173 from wenchaoh997/zh_cn

translate zh_cn
This commit is contained in:
Michael Cade 2022-07-27 11:53:52 +01:00 committed by GitHub
commit da65bbac57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,165 +1,164 @@
---
title: '#90DaysOfDevOps - Gitting to know Git - Day 37'
title: '#90DaysOfDevOps - 了解 Git - 第三十七天'
published: false
description: 90DaysOfDevOps - Gitting to know Git
description: 90DaysOfDevOps - 了解 Git
tags: "devops, 90daysofdevops, learning"
cover_image: null
canonical_url: null
id: 1048707
---
## Gitting to know Git
Apoligies for the terrible puns in the title and throughout. I am surely not the first person to turn Git into a dad joke!
## 了解 Git
In the last two posts we learnt about version control systems, and some of the fundamental workflows of git as a version control system [Day 35](day35.md) Then we got git installed on our system, updated and configured. We also went a little deeper on the theory between Client-Server version control system and Git which is a distributed version control system [Day 36](day36.md).
很抱歉使用了双关的标题(Gitting to know Git)。但我肯定不是第一个开这个玩笑的人!
Now we are going to run through some of the commands and use cases that we will all commonly see with git.
在[第三十五天](day35.md)的分享中我们对版本控制、git的基本工作流有了一定了解。然后[第三十六天](day36.md)我们在电脑上安装了git进行了更新和配置。同时也进一步学习了客户端服务器和Git的分布式版本控制。
### Where to git help with git?
现在我们要去学习一些常用的git命令及其案例。
There is going to be times where you just cannot remember or just don't know the command you need to get things done with git. You are going to need help.
### 在哪里查看git的帮助
It goes without saying that google or any search engine is likely to be your first port of call when searching help.
有时候你可能不记得或者不知道该使用哪个git命令。这就需要寻找帮助(help)。
Secondly the next place is going to be the official git site and the documentation. [git-scm.com/docs](http://git-scm.com/docs) Here you will find not only a solid reference to all the commands available but also lots of different resources.
在查找帮助的第一时间,你可能会使用搜索引擎,百度一下。
![](Images/Day37_Git1.png)
第二步会是查看git上的官方内容和文档。这里([git-scm.com/docs](http://git-scm.com/docs))你不仅可以找到所有命令的可靠资料,还可以找到很多不同的资源。
We can also access this same documentation which is super useful if you are without connectivity from the terminal. If we chose the `git add` command for example we can run `git add --help` and we see below the manual.
![](../../Days/Images/Day37_Git1.png)
![](Images/Day37_Git2.png)
在离线环境下,我们也可以通过终端(terminal)来查看相同的文档。如果我们想使用`git add`命令,我们可以运行`git add --help`,这里会看到下面的说明内容。
We can also in the shell use `git add -h` which is going to give us a short summary of the options we have available.
![](../../Days/Images/Day37_Git2.png)
![](Images/Day37_Git3.png)
我们也可以在shell中用`git add -h`来查看简短的选项描述。
### Myths surrounding Git
![](../../Days/Images/Day37_Git3.png)
"Git has no access control" - You can empower a leader to maintain source code.
### Git的错误认知
"Git is too heavy" - Git has the ability to provide shallow repositories which basically means a reduced amount of history if you have large projects.
"Git has no access control" - 你可以授权一个领导者来维护源代码。
### Real shortcomings
"Git is too heavy" - Git可以提供浅层存储库(shallow repositories),这在项目很大的时候,减少了历史记录的数量。
Not ideal for Binary files. Great for source code but not great for executable files or videos for example.
### 存在的缺点
Git is not user friendly, the fact that we have to spend time talking about commands and functions of the tool is probably a key sign of that.
对于二进制文件的处理并不完美。例如Git适用于源代码但不太适合可执行文件或视频文件。
Overall though, git is hard to learn, but easy to use.
Git对用户不是很友好我们也花了一些时间来讨论其中的命令和功能这也是它的一个关键标志。
### The git ecosystem
总的来说git有学习难度但它很好用。
I want to briefly cover the ecosystem around git but not deep dive into some of these areas but I think its important to note these here at a high level.
### Git的生态
Almost all modern development tools support Git.
我想简单聊一下围绕git的生态系统个人觉得有必要从宏观角度去了解这些。
- Developer tools - We have already mentioned visual studio code but you will find git plugins and integrations into sublime text and other text editors and IDEs.
几乎所有现代开发工具支持Git。
- Team tools - Also mentioned around tools like Jenkins from a CI/CD point of view, Slack from a messaging framework and Jira for project management and issue tracking.
- 开发者工具 - 我们之前有提到过visual studio code你发现git的插件被集成到了如sublime text和其他文本编辑器和IDE中。
- 团队工具 - 用来做持续集成/持续部署的Jenkins信息传递框架的Slack和用于项目管理和问题追踪的Jira。
- 云供应商 - 所有大型云供应商都支持Git。 (Microsoft Azure, Amazon AWS, Google Cloud Platform)
- 基于Git的服务 - 有GitHub、GitLab和 BitBucket后续会进一步讨论。我听说这些服务是代码的社交平台
- Cloud Providers - All the large cloud providers support git, Microsoft Azure, Amazon AWS, Google Cloud Platform.
### Git备忘录
- Git-Based services - Then we have the GitHub, GitLab and BitBucket of which we will cover in more detail later on. I have heard these services as the social network for code!
我们没有列出绝大部分的命令但查看了网上的一些备忘录后我想记录一些git的命令及其用途。你不需要把它们全部记下来只需多使用你会学到它的基本用法。
### The Git Cheatsheet
我是从[atlassian](https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet)那里找到的把它们整理下来也是了解Git命令的一种好方法。
We have not covered most of these commands but having looked at some cheatsheets available online I wanted to document some of the git commands and what their purpose are. We don't need to remember these all, and with more hands on practice and using you will pick at least the git basics.
### Git基本操作
I have taken these from [atlassian](https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet) but writing them down and reading the description is a good way to get to know what the commands are as well as getting hands on in every day tasks.
### Git Basics
| Command | Example | Description |
| 命令 | 例子 | 描述 |
| --------------- | ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
| git init | `git init <directory>` | Create an empty git repository in specified directory. |
| git clone | `git clone <repo>` | Clone repository located at <repo> onto local machine. |
| git config | `git config user.name` | Define author name to be used for all commits in current repository `system`, `global`, `local` flag to set config options. |
| git add | `git add <directory>` | Stage all changes in <directory> for the next commit. We can also add <files> and <.> for everything. |
| git commit -m | `git commit -m "<message>"` | Commit the staged snapshot, use <message> to detail what is being committed. |
| git status | `git status` | List files that are staged, unstaged and untracked. |
| git log | `git log` | Display all commit history using the default format. There are additional options with this command. |
| git diff | `git diff` | Show unstaged changes between your index and working directory. |
| git init | `git init <directory>` | 在指定的文件夹`<directory>`里创建一个空的Git仓库。 |
| git clone | `git clone <repo>` | 将`<repo>`中的仓库复制到自己的电脑上。 |
| git config | `git config user.name` | 定义在当前仓库中所有提交的作者名,`system`、`global`、`local`来进行设定。 |
| git add | `git add <directory>` | <directory>中所有变更暂存到下一次提交中。也可以使用`add <files>`和`<.>`来进行操作。 |
| git commit -m | `git commit -m "<message>"` | 提交暂存的快照,使用<message>来说明提交了什么。 |
| git status | `git status` | 列出已暂存、未暂存和未追踪的文件。 |
| git log | `git log` | 使用默认格式显示所有的提交记录。 |
| git diff | `git diff` | 显示索引和工作目录中未暂存的更改。 |
### Git Undoing Changes
### Git的撤销更改
| Command | Example | Description |
| --------------- | ------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| git revert | `git revert <commit>` | Create a new commit that undoes all of the changes made in <commit> then apply it to the current branch. |
| git reset | `git reset <file>` | Remove <file> from the staging area, but leave the working directory unchanged. This unstages a file without overwriting any changes. |
| git clean | `git clean -n` | Shows which files would be removed from the working directory. Use `-f` in place of `-n` to execute the clean. |
| 命令 | 例子 | 描述 |
| --------------- | ------------------------------------- | ------------------------------------------- |
| git revert | `git revert <commit>` | 新建一个提交,撤销<commit>中所有的变更,并应用在当前分支上。 |
| git reset | `git reset <file>` | <file>从暂存中移除,但当前文件不会改变。这是将文件改为未暂存并不改变内容的方式。 |
| git clean | `git clean -n` | 显示哪些文件没有被暂存并将被删除。用`-f`可以强制执行删除清理。 |
### Git Rewriting History
### Git重写历史记录
| Command | Example | Description |
| --------------- | ------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| git commit | `git commit --amend` | Replace the last commit with the staged changes and last commit combined. Use with nothing staged to edit the last commits message. |
| git rebase | `git rebase <base>` | Rebase the current branch onto <base>. <base> can be a commit ID, branch name, a tag, or a relative reference to HEAD. |
| git reflog | `git reflog` | Show a log of changes to the local repositorys HEAD. Add --relative-date flag to show date info or --all to show all refs. |
| 命令 | 例子 | 描述 |
| ---------------|------------------------ |----------------------------|
| git commit | `git commit --amend` | 用暂存的变更替换掉上次的提交,合并上次的提交。当没有暂存时,编辑上次提交的信息。 |
| git rebase | `git rebase <base>` | 从当前分支嫁接到<base><base>可以是对应提交ID、分支名称、标签tag 或是对HEAD的相关引用。 |
| git reflog | `git reflog` | 显示本地仓库HEAD的变更记录。添加 --relative-date 来显示日期时间信息,或 --all 来显示所有信息。 |
### Git Branches
### Git的分支
| Command | Example | Description |
| --------------- | ------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| git branch | `git branch` | List all of the branches in your repo. Add a <branch> argument to create a new branch with the name <branch>. |
| git checkout | `git checkout -b <branch>` | Create and check out a new branch named <branch>. Drop the -b flag to checkout an existing branch. |
| git merge | `git merge <branch>` | Merge <branch> into the current branch. |
| 命令 | 例子 | 描述 |
| ---------------|-----------------------------|------------------------|
| git branch | `git branch` | 显示仓库中所有的分支。添加<branch>参数来新建一个叫<branch>的分支。 |
| git checkout | `git checkout -b <branch>` | 新建并检查一个叫<branch>的新分支。去掉 -b 来检查一个已存在的分支。 |
| git merge | `git merge <branch>` | <branch>合并到当前分支中。 |
### Git Remote Repositories
### Git的远程仓库
| Command | Example | Description |
| --------------- | ------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| git remote add | `git remote add <name> <url>` | Create a new connection to a remote repo. After adding a remote, you can use <name> as a shortcut for <url> in other commands. |
| git fetch | `git fetch <remote> <branch>` | Fetches a specific <branch>, from the repo. Leave off <branch> to fetch all remote refs. |
| git pull | `git pull <remote>` | Fetch the specified remotes copy of current branch and immediately merge it into the local copy. |
| git push | `git push <remote> <branch>` | Push the branch to <remote>, along with necessary commits and objects. Creates named branch in the remote repo if it doesnt exist. |
| 命令 | 例子 | 描述 |
| ------------------|-----------------------------------------|------------------------------------------------------------------------|
| git remote add | `git remote add <name> <url>` | 新建连接一个远程仓库。添加后,你可以使用<name>作为<url>的简写。 |
| git fetch | `git fetch <remote> <branch>` | 从仓库中抓取特定的<branch>。没有特定<branch>时,抓取所有远程的索引。 |
| git pull | `git pull <remote>` | 抓取当前分支的特定<remote>的更新,并马上将它合并到本地文件夹中。 |
| git push | `git push <remote> <branch>` | 将当前分支中的提交推送到<remote>。若远程仓库中不存在<branch>,则新建<branch>|
### Git Diff
### Git的差异比较
| Command | Example | Description |
| --------------- | ------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| git diff HEAD | `git diff HEAD` | Show difference between working directory and last commit. |
| git diff --cached | `git diff --cached` | Show difference between staged changes and last commit |
| 命令 | 例子 | 描述 |
| ---------------|-----------------------------|------------------------|
| git diff HEAD | `git diff HEAD` | 显示当前目录与上次提交不同的部分。 |
| git diff --cached | `git diff --cached` | 显示暂存的变更与上次提交不同的部分。 |
### Git Config
### Git的配置
| Command | Example | Description |
| ----------------------------------------------------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| git config --global user.name <name> | `git config --global user.name <name>` | Define the author name to be used for all commits by the current user. |
| git config --global user.email <email> | `git config --global user.email <email>` | Define author email to be used for all commits by the current user. |
| git config --global alias <alias-name> <git-command> | `git config --global alias <alias-name> <git-command>` | Create shortcut for a git command . |
| git config --system core.editor <editor> | `git config --system core.editor <editor>` | Set the text editor to be used by commands for all users on the machine. <editor> arg should be the comamnd that launches the desired editor. |
| git config --global --edit | `git config --global --edit ` | Open the global configuration file in a text editor for manual editing. |
| 命令 | 例子 | 描述 |
| ---------------|-----------------------------|------------------------|
| git config --global user.name <name> | `git config --global user.name <name>` | 设置当前用户每次提交时使用的作者名称。 |
| git config --global user.email <email> | `git config --global user.email <email>` | 设置当前用户每次提交时使用的作者邮箱。 |
| git config --global alias <alias-name> <git-command> | `git config --global alias <alias-name> <git-command>` | 新建git命令的缩写。 |
| git config --system core.editor <editor> | `git config --system core.editor <editor>` | 设置每个用户预设的文本编辑器。<editor>是打开对应编辑器的命令。 |
| git config --global --edit | `git config --global --edit ` | 用文本编辑器打开全局配置文件,并进行编辑。 |
### Git Rebase
### Git的嫁接(rebase)
| Command | Example | Description |
| ------------------------------------- | ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------- |
| git rebase -i <base> | `git rebase -i <base>` | Interactively rebase current branch onto <base>. Launches editor to enter commands for how each commit will be transferred to the new base. |
| 命令 | 例子 | 描述 |
| ---------------|-----------------------------|------------------------|
| git rebase -i <base> | `git rebase -i <base>` | 将当前分支嫁接到<base>。打开编辑器来输入命令,每个提交将如何转移到新的分支上。 |
### Git Pull
| Command | Example | Description |
| ------------------------------------- | ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------- |
| git pull --rebase <remote> | `git pull --rebase <remote>` | Fetch the remotes copy of current branch and rebases it into the local copy. Uses git rebase instead of merge to integrate the branches. |
| 命令 | 例子 | 描述 |
| ---------------|-----------------------------|------------------------|
| git pull --rebase <remote> | `git pull --rebase <remote>` | 将远程仓库的内容抓取到文件夹中。使用`--rebase`来避免合并分支。 |
### Git Reset
| Command | Example | Description |
| ------------------------- | --------------------------| --------------------------------------------------------------------------------------------------------------------------------------------- |
| git reset | `git reset ` | Reset staging area to match most recent commit, but leave the working directory unchanged. |
| git reset --hard | `git reset --hard` | Reset staging area and working directory to match most recent commit and overwrites all changes in the working directory |
| git reset <commit> | `git reset <commit>` | Move the current branch tip backward to <commit>, reset the staging area to match, but leave the working directory alone |
| git reset --hard <commit> | `git reset --hard <commit>` | Same as previous, but resets both the staging area & working directory to match. Deletes uncommitted changes, and all commits after <commit>. |
### Git的重置
| 命令 | 例子 | 描述 |
| ---------------|-----------------------------|------------------------|
| git reset | `git reset ` | 重置暂存的内容,但不改变当前文件夹。 |
| git reset --hard | `git reset --hard` | 重置暂存的内容和文件夹,匹配最近的提交,并改写所有变更。 |
| git reset <commit> | `git reset <commit>` | 重置当前分支到<commit>,重置暂存的内容,但不改变当前文件夹。 |
| git reset --hard <commit> | `git reset --hard <commit>` | 与上一条类似,但重置暂存内容和文件夹。删除未提交的变更和所有在<commit>之后的内容。 |
### Git Push
| Command | Example | Description |
| ------------------------- | --------------------------| --------------------------------------------------------------------------------------------------------------------------------------------- |
| git push <remote> --force | `git push <remote> --force` | Forces the git push even if it results in a non-fast-forward merge. Do not use the --force flag unless youre absolutely sure you know what youre doing. |
| git push <remote> --all | `git push <remote> --all` | Push all of your local branches to the specified remote. |
| git push <remote> --tags | `git push <remote> --tags` | Tags arent automatically pushed when you push a branch or use the --all flag. The --tags flag sends all of your local tags to the remote repo. |
| 命令 | 例子 | 描述 |
| ---------------|-----------------------------|------------------------|
| git push <remote> --force | `git push <remote> --force` | 强制进行推送。不要使用`--force`,除非你明确清楚自己在做什么。 |
| git push <remote> --all | `git push <remote> --all` | 推送当前分支的所有内容到特定远程仓库。 |
| git push <remote> --tags | `git push <remote> --tags` | 当你推送一个分支或使用`--flag`时Tags不会被自动推送。添加`--tags`后才推送所有tags到远程仓库。 |
## Resources
## 相关资料
- [What is Version Control?](https://www.youtube.com/watch?v=Yc8sCSeMhi4)
- [Types of Version Control System](https://www.youtube.com/watch?v=kr62e_n6QuQ)
@ -170,4 +169,4 @@ I have taken these from [atlassian](https://www.atlassian.com/git/tutorials/atla
- [Git cheatsheet](https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet)
See you on [Day 38](day38.md)
[第三十八天](day38.md)见