mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-03 23:40:14 +08:00
finishing 20150520 How to master the art of Git.md
This commit is contained in:
parent
2c6be637c8
commit
32ee093048
@ -1,262 +0,0 @@
|
||||
translated by zhousiyu325
|
||||
How to master the art of Git
|
||||
============================================================
|
||||
|
||||
![](https://cdn-images-1.medium.com/max/2000/1*wOJzZIRawP0SmlPDu32mxw.jpeg)
|
||||
|
||||
### Step up your software development game with 7 simple Git commands.
|
||||
|
||||
Have you ever wondered how one learns to use Git well? You use Git very poorly for a long time. Eventually, you’ll get the hang of it. That’s why I’m here. I’ll take you on a journey to enlightenment. These are my basic guidelines of how to speed up the process of learning Git significantly. I’ll cover what Git actually is and the 7 Git commands I use the most. This article is mainly aimed towards aspiring developers and college freshmen who are in need of an introductory explanation of what Git is and how to master the basics.
|
||||
|
||||
* * *
|
||||
|
||||
You can go ahead and read the whole article or hurt my feelings significantly by only reading the TLDR;
|
||||
|
||||
#### TLDR;
|
||||
|
||||
When in the process of learning Git, make a habit of following these steps:
|
||||
|
||||
1. `git status` all the time!
|
||||
|
||||
2. Try only to change files you really want to change.
|
||||
|
||||
3. `git add -A` is your friend.
|
||||
|
||||
4. Feel free to `git commit -m "meaningful messages"` .
|
||||
|
||||
5. Always `git pull` before doing any pushing,
|
||||
but after you have committed any changes.
|
||||
|
||||
6. Finally, `git push` the committed changes.
|
||||
|
||||
* * *
|
||||
|
||||
#### Do not go gentle into that good night.
|
||||
|
||||
The universal first step for any developer is to choose a common place to host his or her code base. Voilá, [GitHub][4]! The meeting place for all things regarding code. To be able to understand the concept of GitHub you would first need to understand what Git really is.
|
||||
|
||||
Git is a version control software, based on the command line, with a few desktop apps available for Windows and Mac. Created by Linus Torvalds, the father of Linux and one of the most influential people in computer science, ever. Channeling this merit, Git has become a standard for a vast majority of software developers regarding sharing and maintaining code. Those were a bunch of large words. Let’s break it down. Version control software, means exactly what it says. Git allows you to have a preview of all the versions of your code you have ever written. Literally, ever! Every code base a developer has, will be stored in its respective repository, which can be named anything from _pineapple_ to _express_ . In the process of developing the code within this repository you will make countless changes, up until the first official release. Here lies the core reason why version control software is so important. It enables you, the developer, to have a clear view of all changes, revisions and improvements ever done to the code base. In turn making it much easier to collaborate, download code to make edits, and upload changes to the repository. However, in spite of all this awesomeness, one thing takes the crown as the most incredible. You can download and use the files even thought you have nothing to do with the development process!
|
||||
|
||||
Let’s get back to the GitHub part of the story. It’s just a hub for all repositories, where they can be stored and viewed online. A central meeting point for like minded individuals.
|
||||
|
||||
#### Let’s start using it already!
|
||||
|
||||
Okay, remember, Git is a software, and like any other software you’ll first need to install it:
|
||||
|
||||
[Git - Installing Git
|
||||
If you do want to install Git from source, you need to have the following libraries that Git depends on: autotools…git-scm.com][5][][6]
|
||||
|
||||
*Anchorman voice*
|
||||
_Please click on the link above, and follow the instructions stated…_
|
||||
|
||||
Done installing it, great. Now you will need to punch in [github.com][7] in your browsers address bar. Create an account if you don’t already have one, and you’re set to rock’n’roll! Jump in and create a new repository, name it Steve for no reason at all, just for the fun of having a repository named Steve. Go ahead and check the _Initialize this repository with a README _ checkbox and click the create button. You now have a new repository called Steve. Be proud of yourself, I sure am.
|
||||
|
||||
|
||||
![](https://cdn-images-1.medium.com/max/1000/1*Hw0s4yAfViZQNXO7YB0X8A.png)
|
||||
|
||||
#### Starting to use Git for real this time.
|
||||
|
||||
Now comes the fun part. You’re ready to clone Steve to your local machine. View this process as simply copying the repository from GitHub to your computer. By clicking the _clone or download_ button you will see a URL which will look something like this:
|
||||
|
||||
```
|
||||
https://github.com/yourGithubAccountName/Steve.git
|
||||
```
|
||||
|
||||
Copy this URL and open up a command prompt. Now write and run this command:
|
||||
|
||||
```
|
||||
git clone https://github.com/yourGithubAccountName/Steve.git
|
||||
```
|
||||
|
||||
Abrakadabra! Steve has automagically been cloned to your computer. Looking in the directory where you cloned the repository, you’ll see a folder named Steve. This local folder is now linked with it’s _origin, _ the original repository on Github.
|
||||
|
||||
Remember this process, you will surely repeat it many times in your career as a software developer. With all this formal stuff done, you are ready to get started with the most common and regularly used Git commands.
|
||||
|
||||
|
||||
![](https://cdn-images-1.medium.com/max/800/1*txfAZdt3U0u9V9WTEZiyNQ.jpeg)
|
||||
Lame video game reference
|
||||
|
||||
#### You’re actually just now starting to use Git for real.
|
||||
|
||||
Open up the Steve directory and go ahead and open a command prompt from within the same directory. Run the command:
|
||||
|
||||
```
|
||||
git status
|
||||
```
|
||||
|
||||
This will output the status of your working directory, showing you all the files you have edited. This means it’s showing you the difference between the files on the origin and your local working directory. The status command is designed to be used as a _commit_ template. I’ll come back to talking about commit a bit further down this tutorial. Simply put, `[git status][1]` shows you which files you have edited, in turn giving you an overview of which you wish to upload back to the origin.
|
||||
|
||||
But, before you do any of that, first you need to pick which files you wish to send back to the origin. This is done with:
|
||||
|
||||
```
|
||||
git add
|
||||
```
|
||||
|
||||
Please go ahead and create a new text file in the Steve directory. Name it _pineapple.txt_ just for the fun of it. Write whatever you would want in this file. Switch back to the command prompt, and run `git status` once again. Now, you’ll see the file show up in red under the banner _untracked files._
|
||||
|
||||
```
|
||||
On branch master
|
||||
Your branch is up-to-date with 'origin/master'.
|
||||
Untracked files:
|
||||
(use "git add <file>..." to include in what will be commited)
|
||||
```
|
||||
|
||||
```
|
||||
pineapple.txt
|
||||
```
|
||||
|
||||
The next step is to add a file to staging. Staging can be viewed as a context where all changes you have picked will be bundled into one, when the time comes to commit them. Now you can go ahead and add this file to staging:
|
||||
|
||||
```
|
||||
git add -A
|
||||
```
|
||||
|
||||
The _-A _ flag means that all files that have been changed will be staged for commit. However, `git add` is very flexible and it is perfectly fine to add files one by one. Just like this:
|
||||
|
||||
```
|
||||
git add pineapple.txt
|
||||
```
|
||||
|
||||
This approach gives you power to cherry pick every file you wish to stage, without the added worry that you’ll change something you weren’t supposed to.
|
||||
|
||||
After running `git status` once again you should see something like this:
|
||||
|
||||
```
|
||||
On branch master
|
||||
Your branch is up-to-date with 'origin/master'.
|
||||
Changes to be committed:
|
||||
(use "git reset HEAD <file>..." to unstage)
|
||||
```
|
||||
|
||||
```
|
||||
new file: pineapple.txt
|
||||
```
|
||||
|
||||
Ready to commit the changes? I sure am.
|
||||
|
||||
```
|
||||
git commit -m "Write your message here"
|
||||
```
|
||||
|
||||
The [Git commit][9] command stores the current files in staging in a new commit along with a log message from the user describing the changes. The _-m _ flag includes the message written in double quotes in the commit.
|
||||
|
||||
Checking the status once again will show you:
|
||||
|
||||
```
|
||||
On branch master
|
||||
Your branch is ahead of 'origin/master' by 1 commit.
|
||||
(use "git push" to publish your local commits)
|
||||
nothing to commit, working directory clean
|
||||
```
|
||||
|
||||
All changes have now been bundled into one commit with one dedicated message regarding the work you have done. You’re now ready to `[git push][2]`this commit to the _origin_ . The push command does literally what it means. It will upload your committed changes from your local machine to the repository origin on GitHub. Go back to the command prompt and run:
|
||||
|
||||
```
|
||||
git push
|
||||
```
|
||||
|
||||
It will ask you to enter your GitHub username and password, after which you will see something like this:
|
||||
|
||||
```
|
||||
Counting objects: 3, done.
|
||||
Delta compression using up to 4 threads.
|
||||
Compressing objects: 100% (2/2), done.
|
||||
Writing objects: 100% (3/3), 280 bytes | 0 bytes/s, done.
|
||||
Total 3 (delta 0), reused 0 (delta 0)
|
||||
To https://github.com/yourGithubUserName/Steve.git
|
||||
c77a97c..08bb95a master -> master
|
||||
```
|
||||
|
||||
That’s it. You have uploaded the local changes. Go ahead and look at your repository on GitHub and you’ll see that it now contains a file named _pineapple.txt_ .
|
||||
|
||||
What if you work in a team of developers? Where all of them push commits to the origin. What happens then? This is where Git starts to show it’s real power. You can just as easily [pull][10] the latest version of the code base to your local machine with one simple command.
|
||||
|
||||
```
|
||||
git pull
|
||||
```
|
||||
|
||||
But Git has it’s limitations. You need to have matching versions to be able to push changes to the origin. Meaning the version you have locally needs to be exactly the same as the one on the origin. When pulling from the origin you shouldn’t have files in the working directory, as they will be overwritten in the process. Hence me giving this simple advice. When in the process of learning Git, make a habit of following these steps:
|
||||
|
||||
1. `git status` all the time!
|
||||
|
||||
2. Try only to change files you really want to change.
|
||||
|
||||
3. `git add -A` is your friend.
|
||||
|
||||
4. Feel free to `git commit -m "meaningful messages"` .
|
||||
|
||||
5. Always `git pull` before doing any pushing,
|
||||
but after you have committed any changes.
|
||||
|
||||
6. Finally, `git push` the committed changes.
|
||||
|
||||
* * *
|
||||
|
||||
Phew, are you still with me? You’ve come a long way. Have a break.
|
||||
|
||||
|
||||
![](https://cdn-images-1.medium.com/max/800/1*vrNe9wt2zHczmXx-tkGipw.jpeg)
|
||||
Lame video-game references, once again…
|
||||
|
||||
Rested up? Great! You’re ready for some error handling. What if you accidentally changed some files your shouldn’t have touched. No need to freak out, just use `[git checkout][3]`. Let’s change something in the _pineapple.txt_ file. Add another line of text in there, let’s say, _“Steve is mega-awesome!”_ . Go ahead, save the changes and check the `git status`.
|
||||
|
||||
```
|
||||
On branch master
|
||||
Your branch is up-to-date with 'origin/master'.
|
||||
Changes not staged for commit:
|
||||
(use "git add <file>..." to update what will be committed)
|
||||
(use "git checkout -- <file>..." to discard changes in working directory)
|
||||
```
|
||||
|
||||
```
|
||||
modified: pineapple.txt
|
||||
```
|
||||
|
||||
```
|
||||
no changes added to commit (use "git add" and/or "git commit -a")
|
||||
```
|
||||
|
||||
As expected it has been registered as a change. But what if Steve really isn’t that mega-awesome? What if Steve is mega-lame? Worry not! The simplest way to revert the changes is to run:
|
||||
|
||||
```
|
||||
git checkout -- pineapple.txt
|
||||
```
|
||||
|
||||
Now you will see the file has been returned to it’s previous state.
|
||||
|
||||
But what if you really mess up. I mean like majorly mess things up, and need to reset everything back to the state the _origin_ is in. No need to worry, during emergencies like this we have this beauty:
|
||||
|
||||
```
|
||||
git reset --hard
|
||||
```
|
||||
|
||||
The [Git reset][11] command with the _--hard_ flag discards all changes since the last commit. Pretty handy sometimes.
|
||||
|
||||
* * *
|
||||
|
||||
To wrap up, I’d like to encourage you to play around with Git as much as possible. It’s by far the best way of learning how to use it with confidence. Apart from that, make a habit of reading the Git documentation. As confusing as it may seem at first, after a few moments of reading you will get the hang of it.
|
||||
|
||||
_Hope you guys and girls had as much fun reading this article as I had writing it._ _Feel free to share if you believe it will be of help to someone, or if you liked it, click the 💚 below so other people will see this here on Medium._
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://hackernoon.com/how-to-master-the-art-of-git-68e1050f3147
|
||||
|
||||
作者:[Adnan Rahić][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://hackernoon.com/@adnanrahic
|
||||
[1]:https://git-scm.com/docs/git-status
|
||||
[2]:https://git-scm.com/docs/git-push
|
||||
[3]:https://git-scm.com/docs/git-checkout
|
||||
[4]:https://github.com/
|
||||
[5]:https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
|
||||
[6]:https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
|
||||
[7]:https://github.com/
|
||||
[8]:https://git-scm.com/docs/git-add
|
||||
[9]:https://git-scm.com/docs/git-commit
|
||||
[10]:https://git-scm.com/docs/git-pull
|
||||
[11]:https://git-scm.com/docs/git-reset
|
251
translated/tech/20170520 How to master the art of Git.md
Normal file
251
translated/tech/20170520 How to master the art of Git.md
Normal file
@ -0,0 +1,251 @@
|
||||
掌握 Git 之美
|
||||
=====================================================
|
||||
|
||||
![](https://cdn-images-1.medium.com/max/2000/1*wOJzZIRawP0SmlPDu32mxw.jpeg)
|
||||
|
||||
### 使用 7 条简单的 Git 命令配置你的软件开发游戏
|
||||
|
||||
你是否曾经想知道如何学好 Git?你长期以来都是很糟糕地在使用 Git。最终,你会掌握它的窍门。那就是我写这篇文章的原因,我将带你去启蒙之旅。这儿是我关于如何加快 Git 学习过程的基本指南。我将介绍 Git 的实际情况以及我使用最多的 7 条 Git 命令。本文主要针对有兴趣的开发人员和大学新生,他们需要关于 Git 的介绍以及如何掌握基础知识。
|
||||
|
||||
***
|
||||
|
||||
你可以往前继续阅读整篇文章,或者只读 TLDR 部分,尽管这将使我很受伤。
|
||||
|
||||
#### TLDR
|
||||
|
||||
在学习 Git 的过程中,请养成下面这些步骤的习惯:
|
||||
|
||||
1. 随时使用 `git status`!
|
||||
|
||||
2. 只更改那些你真正想更改的文件。
|
||||
|
||||
3. `git add -A` 会是你的朋友
|
||||
|
||||
4. 随时使用命令 `git commit -m "meaningful messages"`。
|
||||
|
||||
5. 做任何推送之前先使用命令 `git pull`,但是这需要在你提交过一些更改之后。
|
||||
|
||||
6. 最后,`git push`推送提交的更改。
|
||||
|
||||
***
|
||||
|
||||
#### 不要温和地走进那个良夜
|
||||
|
||||
对任何开发人员来说,通常第一步都是选择一个广泛使用的地方托管他或她的代码库。那就是,[GitHub][4]。它是有关代码的所有事情的会场。要理解 GitHub 的概念,你先需要知道什么是 Git。
|
||||
|
||||
Git 是一款基于命令行的版本控制软件,在 Windows 和 Mac 系统上也有几款可用的桌面应用。 Git 由 Linux 之父 Linus Torvalds 开发,Linus Torvalds 还是是计算机科学中最有影响力的人物之一。因为这一优势,Git 已经成为绝大多数软件开发人员关于共享和维护代码的标准。那些是一大堆话,让我们将其分解。正如它的名字所说,版本控制软件 Git 让你可以预览你写过的代码的所有版本。从字面上来说, 开发人员的每个代码库都将永远存储在其各自的仓库中,仓库可以叫做任何名字,从 *pineapple* 到 *express* 。在此仓库开发代码的过程中,你将进行出无数次的更改,直到第一次正式发布。这就是版本控制软件如此重要的核心原因所在。它让作为开发人员的你可以清楚地了解对代码库进行的所有更改、修订和改进。反过来使协同合作更容易,下载代码进行编辑,然后将更改上传到仓库。然而,尽管有了这么多好处,但是仍然有一件事让好处更进一层。你可以下载并使用这些文件,即使你在整个开发过程中什么事也没有做。
|
||||
|
||||
让我们回到文章的 GitHub 部分。它只是所有仓库的枢纽,可以这些仓库存储在其中并在线浏览。它是一个让有着共同兴趣的人相聚的地方。
|
||||
|
||||
#### 我们开始使用它了!
|
||||
|
||||
OK,记住,Git 是一款软件,像任何其他软件一样,你首先需要安装它:
|
||||
|
||||
[Git - Installing Git
|
||||
If you do want to install Git from source, you need to have the following libraries that Git depends on: autotools…git-scm.com][5][][6]
|
||||
|
||||
*Tips: 请点击上面的链接,然后按照说明开始。*
|
||||
|
||||
完成了安装过程,很好。现在你需要在你的浏览器地址栏输入[github.com][7]访问该网站。如果你还没有帐号的话需要新创建一个帐号,然后开始其他事情。登录并创建一个新仓库,命名为 Steve ,没有什么理由,只是为了有一个名为史蒂夫的仓库的乐趣。选中 *Initialize this repository with a README* 复选框并点击创建按钮。现在你有了一个叫做 Steve 的仓库。我相信你会为你自己感到自豪。
|
||||
|
||||
![](https://cdn-images-1.medium.com/max/1000/1*Hw0s4yAfViZQNXO7YB0X8A.png)
|
||||
|
||||
#### 现在开始在使用 Git。
|
||||
|
||||
现在是比较有趣的部分。你将把 Steve 克隆到你本地的机器上。可以把这个过程看作从 Github 上复制仓库到你的电脑上。点击 _clone or download_ 按钮,你将看到一个类似下面这样的 URL:
|
||||
|
||||
```
|
||||
https://github.com/yourGithubAccountName/Steve.git
|
||||
```
|
||||
|
||||
复制这个 URL 并打开命令提示符窗口。现在输入并运行条命令:
|
||||
|
||||
```
|
||||
git clone https://github.com/yourGithubAccountName/Steve.git
|
||||
```
|
||||
Abrakadabra!Steve 仓库已经被自动克隆到了你的电脑上。查看你克隆这个仓库的目录,你会看到一个叫做 Steve 的文件夹。这个本地的文件夹现在已经链接到了它的 _origin_ ,也就是 GitHub 上的远程仓库。
|
||||
|
||||
记住这个过程,在你的软件开发工程人员的职业生涯中你一定会重复这个过程很多次的。完成所有这些准备工作之后,你就可以开始使用最普通且常用的 Git 命令了。
|
||||
|
||||
![](https://cdn-images-1.medium.com/max/800/1*txfAZdt3U0u9V9WTEZiyNQ.jpeg)
|
||||
|
||||
#### 你现在已经开始在真实场景使用 Git 了。
|
||||
|
||||
打开 Steve 目录并在同一目录中打开命令提示符窗口,运行下面的命令:
|
||||
|
||||
```
|
||||
git status
|
||||
```
|
||||
|
||||
这会输出你的工作目录的状态,让你知道所有你编辑过的文件。这意味着它显示了远程库中的文件和本地工作目录中的文件之间的差异。status 命令被用来作为 *commit* 的模版,我将在这篇教程后面进一步谈论 commit 。简单的说,`[git status][1]`告诉你你编辑过哪些文件,反过来给你一个你想要上传到远程库的概述。
|
||||
|
||||
但是,在你做任何上传之前,你首先需要做的是选择你需要发送回远程库的文件。使用下面命令完成:
|
||||
|
||||
```
|
||||
git add
|
||||
```
|
||||
|
||||
接着在 Steve 目录新建一个文本文件,取名为 _pineapple.txt_ ,只是出于好玩。在这个文件里面随便写些你想写的内容,返回命令提示符,然后再次输入 `git status`。现在,你将看到这个文件出现在红色标记 _untracked files_ 下面。
|
||||
|
||||
```
|
||||
On branch master
|
||||
Your branch is up-to-date with 'origin/master'.
|
||||
Untracked files:
|
||||
(use "git add <file>..." to include in what will be commited)
|
||||
```
|
||||
|
||||
```
|
||||
pineapple.txt
|
||||
```
|
||||
|
||||
下一步就是将它添加到暂存区。暂存区可以看作是这样的一个环境:所有你做过的更改在提交时都将作为一次更改被提交。现在,你可以将这个文件加入暂存区:
|
||||
|
||||
```
|
||||
git add -A
|
||||
```
|
||||
|
||||
_-A_ 选项意味着所有你更改过的文件都会被加到暂存区等待提交。然而, `git add` 非常灵活,它也可以像这样一个文件一个文件的添加:
|
||||
|
||||
```
|
||||
git add pineapple.txt
|
||||
```
|
||||
|
||||
这种方法让你有能力选择你想要暂存的每一个文件,而不用担心你会改变你不想改变的事情。
|
||||
|
||||
```
|
||||
On branch master
|
||||
Your branch is up-to-date with 'origin/master'.
|
||||
Changes to be committed:
|
||||
(use "git reset HEAD <file>..." to unstage)
|
||||
```
|
||||
|
||||
```
|
||||
new file: pineapple.txt
|
||||
```
|
||||
|
||||
准备好提交更改了吗?开始吧。
|
||||
|
||||
```
|
||||
git commit -m "Write your message here"
|
||||
```
|
||||
|
||||
[Git commit][9] 命令会将存储在暂存区中的文件和来自用户的用于描述更改的日志文件一起存储在一个新的地方。_-m_选项加入写在双引号内的信息。
|
||||
|
||||
再次检查状态,你会看到:
|
||||
|
||||
```
|
||||
On branch master
|
||||
Your branch is ahead of 'origin/master' by 1 commit.
|
||||
(use "git push" to publish your local commits)
|
||||
nothing to commit, working directory clean
|
||||
```
|
||||
|
||||
所有的更改现在都被加入到一次提交当中了,同时会有一条与你所做相关的信息。现在你想用 `[git push]` 将这次提交推送到远程库 *origin*。这条命令就像字码意义所说,它会把你提交的更改从本地机器上传到 GitHub 的远程仓库中。返回到命令提示符,然后运行:
|
||||
|
||||
```
|
||||
git push
|
||||
```
|
||||
|
||||
你会被要求输入你的 GitHub 帐号和密码,之后你会看到类似下面的这些内容:
|
||||
|
||||
```
|
||||
Counting objects: 3, done.
|
||||
Delta compression using up to 4 threads.
|
||||
Compressing objects: 100% (2/2), done.
|
||||
Writing objects: 100% (3/3), 280 bytes | 0 bytes/s, done.
|
||||
Total 3 (delta 0), reused 0 (delta 0)
|
||||
To https://github.com/yourGithubUserName/Steve.git
|
||||
c77a97c..08bb95a master -> master
|
||||
```
|
||||
|
||||
就是这样。你已经成功上传了你本地的更改。看看你在 GitHub 上的仓库,你会看到它现在包含了一叫做 _pineapple.txt_ 的文件。
|
||||
|
||||
如果你是一个开发小组的一员呢?如果他们都推送提交到 origin,将会发生什么?这就是 Git 真正开始发挥它的魔力的时候。你可以 使用一条简单的命令轻松地将最新版本的代码库 [拉][10] 到你本地的机器上:
|
||||
|
||||
```
|
||||
git pull
|
||||
```
|
||||
|
||||
但是 Git 也有限制:你需要有相匹配的版本才能推送到 origin。这意味着你本地的版本需要和 origin 的版本大致一样。当你从 origin 拉取文件时,在你的工作目录中不能有文件,因为它们将会在这个过程中被覆盖。因此我给出了这条简单的建议。在学习 Git 的过程中,请养成下面这些步骤的习惯:
|
||||
|
||||
1. 随时使用 `git status`!
|
||||
|
||||
2. 只更改那些你真正想更改的文件。
|
||||
|
||||
3. `git add -A` 会是你的朋友
|
||||
|
||||
4. 随时使用命令 `git commit -m "meaningful messages"`。
|
||||
|
||||
5. 做任何推送之前先使用命令 `git pull`,但是这需要在你提交过一些更改之后。
|
||||
|
||||
6. 最后,`git push`推送提交的更改。
|
||||
|
||||
***
|
||||
|
||||
嘿!你还在看这篇文章吗?你已经看了很久了,休息一下吧!
|
||||
|
||||
![](https://cdn-images-1.medium.com/max/800/1*vrNe9wt2zHczmXx-tkGipw.jpeg)
|
||||
Lame video-game references, once again…
|
||||
|
||||
休息好了吗?好的!你已经准备好了开始处理一些错误。如果你不小心更改了一些你本不应该更改的文件后怎么办呢?不需要担心,只需要使用 `[git checkout][3]`。让我们在文件 _pineapple.txt_ 里更改一些内容:在文件中加入一行,比方说,_“Steve is mega-awesome!”_ 。然后保存更改并用 `git status` 检查一下:
|
||||
|
||||
```
|
||||
On branch master
|
||||
Your branch is up-to-date with 'origin/master'.
|
||||
Changes not staged for commit:
|
||||
(use "git add <file>..." to update what will be committed)
|
||||
(use "git checkout -- <file>..." to discard changes in working directory)
|
||||
```
|
||||
|
||||
```
|
||||
modified: pineapple.txt
|
||||
```
|
||||
|
||||
```
|
||||
no changes added to commit (use "git add" and/or "git commit -a")
|
||||
```
|
||||
|
||||
正如预料的那样,它已经被记录为一次更改了。但是,假如 Steve 实际上并不是很优秀呢?假如 Steve 是一个瘸子呢?不用担心!最简单的还原更改的方式是运行命令:
|
||||
|
||||
```
|
||||
git checkout -- pineapple.txt
|
||||
```
|
||||
|
||||
现在你会看到文件已经恢复到了先前的状态。
|
||||
|
||||
但是假如你玩砸了呢?我是说,事情已经变得混乱,并且需要把所有东西重置到与 origin 一样的状态。也不需要担心,在这种紧急情况下我们可以享受 Git 的美妙之处:
|
||||
|
||||
```
|
||||
git reset --hard
|
||||
```
|
||||
|
||||
[Git reset][11] 命令和 _--hard_ 选项一起可以抛弃自上次提交以来的所有更改,有些时候真的很好用。
|
||||
|
||||
****
|
||||
|
||||
最后,我想鼓励你尽可能多地使用 Git。这是能够熟练使用它的最好学习方式。除此之外,养成阅读 Git 文档的习惯。一开始可能会有些云里雾里,但是过段时间后你就会明白它的窍门了。
|
||||
|
||||
*希望你们(小伙子和姑娘们)读这篇文章的时候会和我写它时一样的开心。如果你认为这篇文章对别人有用,你尽可以与别人分享它。或者,如果你喜欢这篇文章,你可以在下面点下赞以便让更多的人看到这篇文章。*
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://hackernoon.com/how-to-master-the-art-of-git-68e1050f3147
|
||||
|
||||
作者:[Adnan Rahić][a]
|
||||
译者:[zhousiyu325](https://github.com/zhousiyu325)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://hackernoon.com/@adnanrahic
|
||||
[1]:https://git-scm.com/docs/git-status
|
||||
[2]:https://git-scm.com/docs/git-push
|
||||
[3]:https://git-scm.com/docs/git-checkout
|
||||
[4]:https://github.com/
|
||||
[5]:https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
|
||||
[6]:https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
|
||||
[7]:https://github.com/
|
||||
[8]:https://git-scm.com/docs/git-add
|
||||
[9]:https://git-scm.com/docs/git-commit
|
||||
[10]:https://git-scm.com/docs/git-pull
|
||||
[11]:https://git-scm.com/docs/git-reset
|
Loading…
Reference in New Issue
Block a user