try git在线教程part1

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来进行头指针引用。


©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容