Git开发是每一个人程序开发者都应该尝试学会的技能。
自己以前在小公司工作,版本控制并不完善,代码更改都是用svn服务器。现在所有的代码都是通过git来提交。刚开始接触git的时候自己并不是很熟练命令。
经过别人推荐才知道可以通过一些在线教程来进行学习git命令。Try Git(http://try.github.io/)这个网站,能在其中根据它的教程练习git命令,15分钟内,你就能够掌握那些最基本的常用命令了。
然而这个网站是全英文的教程,你需要对英文有些基本的了解才能在15分钟内掌握这些命令。为此,特意花了些时间将这个教程翻译了一下。
完整的篇幅有些长,这篇先介绍教程中的前半部分命令。
1.1 git init
Git allows groups of people to work on the same documents (often code) at the same time, and without stepping on each other's toes. It's a distributed version control system.
Our terminal prompt below is currently in a directory we decided to name "octobox". To initialize a Git repository here, type the following command:
git允许同组人员在同一时间同时操作同一篇文档(通常是代码),同时不会影响到其他人。这是一个分布式的控制系统。
我们的终端显示了你现在的文件夹,这个文件夹的名字叫做octobox,在这里初始化git库,输入以下命令:git init。
1.2 Checking the Status
Good job! As Git just told us, our "octobox" directory now has an empty repository in/.git/. The repository is a hidden directory where Git operates.
To save your progress as you go through this tutorial -- and earn a badge when you successfully complete it -- head over tocreate a free Code School account. We'll wait for you here.
Next up, let's type the git status command to see what the current state of our project is:
git status
1.2 查看状态。
干的好!就像git告诉你的那样,你的octobox目录下已经有一个空的git库,这个库在git运行时候是隐藏的。
请从头开始创建一个免费的free school账号,这样不仅可以保存你在这个教程中的进度,同时可以在成功完成教程时获得一个标签,我们在这里等你。
接下来,让我们输入git status命令去查看我们项目的当前状态。
1.3 Adding & Committing
I created a file called octocat.txt in the octobox repository for you (as you can see in the browser below).
You should run the git status command again to see how the repository status has changed:
git status
1.3添加并提交。
我在octobox库中创建了一个octocat.txt文件,(就注意下方浏览器)
你应该再次运行git status来查看库的状态是如何变化的。
1.4Adding Changes
Good, it looks like our Git repository is working properly. Notice how Git saysoctocat.txtis "untracked"? That means Git sees thatoctocat.txtis a new file.
To tell Git to start tracking changes made tooctocat.txt, we first need to add it to the staging area by usinggit add.
git add octocat.txt
保存变化:
很好,看起来我们的git库正常的工作了,注意git显示octocat.txt是untracked,这说明git显示octocat.txt是个新的文件。
告诉git开始追踪octocat.txt文本的变化,你首先需要将文档添加到暂存区域。使用命令git add。
1.5Checking for Changes
Good job! Git is now tracking ouroctocat.txtfile. Let's rungit statusagain to see where we stand:
git status
1.5检查更改。
干的漂亮!git现在开始追踪我们的octocat.txt文档了。让我们再次运行git status查看当前的状态。
1.6 Committing
Notice how Git says changes to be committed? The files listed here are in the Staging Area, and they are not in our repository yet. We could add or remove files from the stage before we store them in the repository.
To store our staged changes we run the commit command with a message describing what we've changed. Let's do that now by typing:
git commit -m "Add cute octocat story"
1.6提交。
注意git说明要如何改变么?列表中的文件现在在暂存区域,他们还没有保存到库中。在他们保存到库之前,我们可以进行添加和删除文档操作。
为了保存我们的改变,我们使用commit命令,他描述了我们所做的改变。让我们输入一下内容:
git commit -m "Add cute octocat story"
1.7Adding All Changes
Great! You also can use wildcards if you want to add many files of the same type. Notice that I've added a bunch of .txt files into your directory below.
I put some in a directory named "octofamily" and some others ended up in the root of our "octobox" directory. Luckily, we can add all the new files using a wildcard withgit add. Don't forget the quotes!
git add '*.txt'
1.7添加所有的改变。
很好,你也可以使用wildcards,(通配符*)如果你想保存统一类型的许多文件。注意我现在已经在下方目录中添加了一个txt分支文件。
我在octofamily"目录下放了一些文件,另外一下放在了我们的根目录"octobox下。幸运的是,我们可以使用通配符配合git add命令来添加所有的新文件。不要忘记了单引号。
1.8Committing All Changes
Okay, you've added all the text files to the staging area. Feel free to run git status to see what you're about to commit.
If it looks good, go ahead and run:
git commit -m 'Add all the octocat txt files'
1.8提交所有的改变。
okay,你现在已经添加了所有的text文件到暂存区。可以运行git status来查看你要提交的内容。
如果那看起来不错,就继续运行下一个命令:
git commit -m 'Add all the octocat txt files'
1.9History
So we've made a few commits. Now let's browse them to see what we changed.
Fortunately for us, there'sgit log. Think of Git's log as a journal that remembers all the changes we've committed so far, in the order we committed them. Try running it now:
git log
1.9历史。
所以到目前为止你已经做了一些提交。现在让我们浏览一下我们改变了什么。
幸运的是,我们有git日志。将git log理解为一个日志,他记录目前为止我们提交的所有的更改,按照我们提交的顺序保存在日志中。现在试着运行下面命令:
1.10Remote Repositories
Great job! We've gone ahead and created a new empty GitHub repository for you to use with Try Git athttps://github.com/try-git/try_git.git. To push our localrepoto the GitHub server we'll need to add a remote repository.
This command takes aremote nameand arepository URL, which in your case ishttps://github.com/try-git/try_git.git.
Go ahead and rungit remote addwith the options below:
git remote add origin https://github.com/try-git/try_git.git
1.10远程存储库
我们已经给你创建了一个新的git库供你使用,地址是https://github.com/try-git/try_git.git.为了将你本地的repo提交到git服务器上,我们需要添加一个远程git库
下面的这个命令使用了一个远程名称和版本库URL。对于你来说是:https://github.com/try-git/try_git.git.
继续运行下面的命令,来进行远程git的添加。
git remote add origin https://github.com/try-git/try_git.git
1.11Pushing Remotely
The push command tells Git where to put our commits when we're ready, and now we're ready. So let's push our local changes to ouroriginrepo (on GitHub).
The name of our remote isoriginand the default local branch name ismaster. The-utells Git to remember the parameters, so that next time we can simply rungit pushand Git will know what to do. Go ahead and push it!
git push -u origin master
1.11push远程。
当你准备好了,push命令会告诉git在哪里put我们的提交代码。现在你已经准备好了,push你本地更改到我们的origin repo库上。
我们远程库的名称叫origin,默认的本地分支名称叫master。字母u含义是告诉git保存这些参数,这样下次你可以简单的运行git push,git会按照你之前的参数进行操作。
1.12Pulling Remotely
Let's pretend some time has passed. We've invited other people to our GitHub project who have pulled your changes, made their own commits, and pushed them.
We can check for changes on our GitHub repository and pull down any new changes by running:
git pull origin master
1.2远程pull操作
假设已经过去一段时间,我们邀请其他人到我们的GitHub项目中,他们下载了你的代码,做出他们的改动,并push他们。
你可以检查你GitHub库的变化,可以下载任意一个新的改动版本下来,尝试运行。
1.13Differences
Uh oh, looks like there have been some additions and changes to the octocat family. Let's take a look at what isdifferentfrom our last commit by using thegit diffcommand.
In this case we want the diff of our most recent commit, which we can refer to using theHEADpointer.
git diff HEAD
1.13查看差异
看起来好像在octocat库里已经有一些变化了,我们可以使用gitdiff命令,来查看和最近一次的提交有什么不一样的地方。
在这种情况下,你希望查看你最近提交所产生的差异,我们可以使用head来进行头指针引用。
网友评论