您的位置:首页 > 其它

git基本操作

2017-02-14 22:29 204 查看
4000

由于正在学习基于github布置木马,因此没有多少git基础的我准备从头学习一下相关基础,学习资料来自菜鸟教程。以下是正文部分:

获取与创建项目命令
git init

git clone

基本快照
git add

git diff

git commit

git reset HEAD

git rm

git mv

Git 分支管理
删除分支

git查看提交历史

Github
添加远程库

git与GitHub远程配置

查看当前的远程库

提取远程仓库

推送到远程仓库

删除远程仓库

获取与创建项目命令

git init

在任何一个目录中执行
git init
都会创建一个git仓库,所有与此项目相关的数据都会放在这里,当前目录中会生成一个.git子目录。

git clone

使用 git clone 拷贝一个 Git 仓库到本地,让自己能够查看该项目,或者进行修改。

如果你需要与他人合作一个项目,或者想要复制一个项目,看看代码,你就可以克隆那个项目。 执行命令:
git clone [url]


[url] 为你想要复制的项目,就可以了。默认情况下,Git 会按照你提供的 URL 所指示的项目的名称创建你的本地项目目录。 通常就是该 URL 最后一个 / 之后的项目名称。如果你想要一个不一样的名字, 你可以在该命令后加上你想要的名称。

基本快照

git add

git add 命令可将该文件添加到缓存,如我们添加以下两个文件:

$ touch README
$ touch hello.php
$ ls
README      hello.php
$ git status -s
?? README
?? hello.php
$


git status
命令用于查看项目的当前状态。
-s
表示简短输出,不加则详细输出。

接下来我们执行 git add 命令来添加文件:

$ git add README hello.php

现在我们再执行 git status,就可以看到这两个文件已经加上去了。

$ git status -s
A  README
A  hello.php
$


新项目中,添加所有文件很普遍,我们可以使用
git add .
命令来添加当前项目的所有文件。当文件被修改(如hello.php被改),前面的A会变成AM,表示加入缓存后有改动,再次git add即可。当你要将你的修改包含在即将提交的快照里的时候,需要执行 git add。

git diff

执行 git diff 来查看执行 git status 的结果的详细信息。

git diff 命令显示已写入缓存与已修改但尚未写入缓存的改动的区别。git diff 有两个主要的应用场景。

尚未缓存的改动:git diff
查看已缓存的改动: git diff --cached
查看已缓存的与未缓存的所有改动:git diff HEAD
显示摘要而非整个 diff:git diff --stat


由于每次调用都会有
warning: LF will be replaced by CRLF in hello.php.
出现,贴出原因及解决办法:


原因分析:

CRLF – Carriage-Return Line-Feed 回车换行

就是回车(CR, ASCII 13, \r) 换行(LF, ASCII 10, \n)。

这两个ACSII字符不会在屏幕有任何输出,但在Windows中广泛使用来标识一行的结束。而在Linux/UNIX系统中只有换行符。

也就是说在windows中的换行符为 CRLF, 而在linux下的换行符为:LF

使用git来生成一个rails工程后,文件中的换行符为LF, 当执行git add .时,系统提示:LF 将被转换成 CRLF

解决方法:

1. 删除刚刚生成的.git文件

$ rm -rf .git
$ git config --gobal core.autocrlf false


这样系统就不会去进行换行符的转换了

再重新执行

$ git init
$ git add .


系统即可正常运行!

git commit

使用 git add 命令将想要快照的内容写入缓存区, 而执行 git commit 将缓存区内容添加到仓库中。

Git 为你的每一个提交都记录你的名字与电子邮箱地址,所以第一步需要配置用户名和邮箱地址。

$ git config --global user.name 'test'
$ git config --global user.email test@test.com


接下来我们写入缓存,并提交对 hello.php 的所有改动。在首个例子中,我们使用
-m
选项以在命令行中提供提交注释。

$ git add hello.php
$ git status -s A README A hello.php $ $ git commit -m '第一次版本提交'
[master (root-commit) d32cf1f] 第一次版本提交
2 files changed, 4 insertions(+)
create mode 100644 README
create mode 100644 hello.php


现在我们已经记录了快照。如果我们再执行 git status:

$ git status
# On branch master
nothing to commit (working directory clean)


以上输出说明我们在最近一次提交之后,没有做任何改动,是一个”working directory clean:干净的工作目录”。

如果你没有设置 -m 选项,Git 会尝试为你打开一个编辑器以填写提交信息。 如果 Git 在你对它的配置中找不到相关信息,默认会打开 vim。屏幕会像这样:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
# modified:   hello.php
#
~
~
".git/COMMIT_EDITMSG" 9L, 257C


如果你觉得
git add
提交缓存的流程太过繁琐,Git 也允许你用 -a 选项跳过这一步。命令格式如下:

git commit -a


修改hello.php后:

$ git commit -am '第二次版本提交'
[master 116fd70] 第二次版本提交
1 file changed, 1 insertion(+), 1 deletion(-)


git reset HEAD

git reset HEAD 命令用于取消已缓存的内容。

在将两个文件修改后,都提交到了缓存区,我们现在要取消其中一个的缓存,操作如下:

$ git status -s
M README #红色
M hello.php  #红色
$ git add .
$ git status -s
M  README #绿色
M  hello.pp #绿色
$ git reset HEAD -- hello.php
Unstaged changes after reset:
M   hello.php
$ git status -s
M  README #绿色
M hello.php  #红色


现在你执行 git commit,只会将 README 文件的改动提交,而 hello.php 是没有的。

$ git commit -m '修改'
[master f50cfda] 修改
1 file changed, 1 insertion(+)
$ git status -s
M hello.php


可以看到 hello.php 文件的修改并为提交。

这时我们可以使用以下命令将 hello.php 的修改提交:

$ git commit -am '修改 hello.php 文件'
[master 760f74d] 修改 hello.php 文件
1 file changed, 1 insertion(+)
$ git status
On branch master
nothing to commit, working directory clean


简而言之,执行 git reset HEAD 以取消之前 git add 添加,但不希望包含在下一提交快照中的缓存。

git rm

git rm 会将条目从缓存区中移除。这与 git reset HEAD 将条目取消缓存是有区别的。 “取消缓存”的意思就是将缓存区恢复为我们做出修改之前的样子。

默认情况下,git rm file 会将文件从缓存区和你的硬盘中(工作目录)删除。

如果你要在工作目录中留着该文件,可以使用 git rm –cached:

如我们删除 hello.php文件:

$ git rm hello.php
rm 'hello.php'
$ ls
README


不从工作区中删除文件:

$ git rm --cached README
rm 'README'
$ ls
README


git mv

git mv 命令做得所有事情就是 git rm –cached 命令的操作, 重命名磁盘上的文件,然后再执行 git add 把新文件添加到缓存区。

我们先把刚移除的 README 添加回来:

$ git add README

然后对其重名:

$ git mv README  README.md
$ ls
README.md


Git 分支管理

分支意味着将工作从主线分离出来,在不影响主线的情况下继续工作。

创建分支命令:

git branch (branchname)


切换分支命令:

git checkout (branchname)


当你切换分支的时候,Git 会用该分支的最后提交的快照替换你的工作目录的内容, 所以多个分支不需要多个目录。

合并分支命令:

git merge


你可以多次合并到统一分支, 也可以选择在合并之后直接删除被并入的分支。

列出分支基本命令:

git branch


没有参数时,git branch 会列出你在本地的分支。

$ git branch
* master


此例的意思就是,我们有一个叫做”master”的分支,并且该分支是当前分支。

当你执行 git init 的时候,缺省情况下 Git 就会为你创建”master”分支。

如果我们要手动创建一个分支。执行 git branch (branchname) 即可。

$ git branch test
$ git branch * master
test


当你以此方式在上次提交更新之后创建了新分支,如果后来又有更新提交, 然后又切换到了”test”分支,Git 将还原你的工作目录到你创建分支时候的样子.

可以使用
git checkout -b (branchname)
命令来创建新分支并立即切换到该分支下,从而在该分支中操作。

$ git checkout -b newtest
Switched to a new branch 'newtest'


删除分支

删除分支命令:

git branch -d (branchname)


git查看提交历史

在使用 Git 提交了若干更新之后,又或者克隆了某个项目,想回顾下提交历史,我们可以使用 git log 命令查看。

针对我们前一章节的操作,使用 git log 命令列出历史提交记录如下:

$ git log
commit 88afe0e02a
daac
dcdfea6844bb627de97da21eb10af1
Merge: 14b4dca d7e7346
Author: w3cschool <w3c@w3cschool.cc>
Date:   Sun Mar 1 15:03:42 2015 +0800

Merge branch 'change_site'

Conflicts:
test.txt

commit 14b4dcadbdc847207651d5a9fae0d315057f346e
Author: w3cschool <w3c@w3cschool.cc>
Date:   Sun Mar 1 14:53:15 2015 +0800

新增加一行

commit d7e734640da06055e107eaf29cf350b3f1de1c2c
Author: w3cschool <w3c@w3cschool.cc>
Date:   Sun Mar 1 14:48:57 2015 +0800

changed the site

commit 556f0a0637978097b82287ac665a717623b21f3f
Author: w3cschool <w3c@w3cschool.cc>
Date:   Sun Mar 1 14:40:34 2015 +0800

removed test2.txt


我们可以用
--oneline
选项来查看历史记录的简洁的版本。

$ git log --oneline
88afe0e Merge branch 'change_site'
14b4dca 新增加一行
d7e7346 changed the site
556f0a0 removed test2.txt
2e082b7 add test2.txt
048598f add test.txt
85fc7e7 test comment from w3cschool.cc


这告诉我们的是,此项目的开发历史。

我们还可以用 –graph 选项,查看历史中什么时候出现了分支、合并。以下为相同的命令,开启了拓扑图选项:

$ git log --oneline --graph
*   88afe0e Merge branch 'change_site'
|\
| * d7e7346 changed the site
* | 14b4dca 新增加一行
|/
* 556f0a0 removed test2.txt
* 2e082b7 add test2.txt
* 048598f add test.txt
* 85fc7e7 test comment from w3cschool.cc


现在我们可以更清楚明了地看到何时工作分叉、又何时归并。

你也可以用
--reverse
参数来逆向显示所有日志。

$ git log --reverse --oneline
85fc7e7 test comment from w3cschool.cc
048598f add test.txt
2e082b7 add test2.txt
556f0a0 removed test2.txt
d7e7346 changed the site
14b4dca 新增加一行
88afe0e Merge branch 'change_site'


如果只想查找指定用户的提交日志可以使用命令:git log –author , 例如,比方说我们要找 Git 源码中 Linus 提交的部分:

$ git log --author=Linus --oneline -5
81b50f3 Move 'builtin-*' into a 'builtin/' subdirectory
3bb7256 make "index-pack" a built-in
377d027 make "git pack-redundant" a built-in
b532581 make "git unpack-file" a built-in
112dd51 make "mktag" a built-in


如果你要指定日期,可以执行几个选项:–since 和 –before,但是你也可以用
--until
--after


例如,如果我要看 Git 项目中三周前且在四月十八日之后的所有提交,我可以执行这个(我还用了 –no-merges 选项以隐藏合并提交):

$ git log --oneline --before={3.weeks.ago} --after={2010-04-18} --no-merges
5469e2d Git 1.7.1-rc2
d43427d Documentation/remote-helpers: Fix typos and improve language
272a36b Fixup: Second argument may be any arbitrary string
b6c8d2d Documentation/remote-helpers: Add invocation section
5ce4f4e Documentation/urls: Rewrite to accomodate transport::address
00b84e9 Documentation/remote-helpers: Rewrite description
03aa87e Documentation: Describe other situations where -z affects git diff
77bc694 rebase-interactive: silence warning when no commits rewritten
636db2c t3301: add tests to use --format="%N"


Github

这是git的远程仓库,用于与他人分享和合作项目。

添加远程库

要添加一个新的远程仓库,可以指定一个简单的名字,以便将来引用,命令格式如下:

git remote add [shortname] [url]


git与github之间通过ssh进行加密通信,因此首先应该有ssh Key:

$ ssh-keygen -t rsa -C "youremail@example.com"


后面的 your_email@youremail.com 改为你在 github 上注册的邮箱,之后会要求确认路径和输入密码,我们这使用默认的一路回车就行。成功的话会在~/下生成.ssh文件夹,进去,打开 id_rsa.pub,复制里面的 key。

回到 github 上,进入 Account => Settings(账户配置)。

左边选择 SSH and GPG keys,然后点击 New SSH key 按钮,title 设置标题,可以随便填,粘贴在你电脑上生成的 key。

为了验证是否成功,输入以下命令:

$ ssh -T git@github.com
Hi ******! You've successfully authenticated, but GitHub does not provide shell access.


出现以上文字说明连接成功

git与GitHub远程配置

具体步骤:

1. 在GitHub中点击Create respository,新建仓库。

2. 在本地命令行输入:

$ mkdir git-test                     # 创建测试目录
$ cd git-test                       # 进入测试目录
$ echo "这里可以随便输" >> README.md     # 创建 README.md 文件并写入内容
$ ls                                        # 查看目录下的文件
README.md
$ git init                                  # 初始化
$ git add README.md                         # 添加文件
$ git commit -m "添加 README.md 文件"        # 提交并备注信息
[master (root-commit) 0205aab] 添加 README.md 文件
1 file changed, 1 insertion(+)
create mode 100644 README.md

# 提交到 Github
$ git remote add origin git@github.com:youe_name/runoob-git-test.git
$ git push -u origin master


查看当前的远程库

要查看当前配置有哪些远程仓库,可以用命令:

git remote


实例

$ git remote
origin
$ git remote -v
origin  git@github.com:tianqixin/runoob-git-test.git (fetch)
origin  git@github.com:tianqixin/runoob-git-test.git (push)


执行时加上 -v 参数,你还可以看到每个别名的实际链接地址。

提取远程仓库

Git 有两个命令用来提取远程仓库的更新。

1、从远程仓库下载新分支与数据:

git fetch


该命令执行完后需要执行git merge远程分支到你所在的分支。

2、从远端仓库提取数据并尝试合并到当前分支:

git pull


该命令就是在执行
git fetch
之后紧接着执行
git merge
远程分支到你所在的任意分支。

假设你配置好了一个远程仓库,并且你想要提取更新的数据,你可以首先执行
git fetch [alias]
告诉 Git 去获取它有你没有的数据,然后你可以执行
git merge[alias]/[branch]
以将服务器上的任何更新(假设有人这时候推送到服务器了)合并到你的当前分支。

推送到远程仓库

推送你的新分支与数据到某个远端仓库命令:

git push [alias] [branch]


以上命令将你的 [branch] 分支推送成为 [alias] 远程仓库上的 [branch] 分支,实例如下。

$ touch runoob-test.txt      # 添加文件
$ git add runoob-test.txt
$ git commit -m "添加到远程"
master 69e702d] 添加到远程
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 runoob-test.txt

$ git push origin master    # 推送到 Github


删除远程仓库

删除远程仓库你可以使用命令:

git remote rm [别名]


实例

$ git remote -v
origin  git@github.com:tianqixin/runoob-git-test.git (fetch)
origin  git@github.com:tianqixin/runoob-git-test.git (push)
# 添加仓库 origin2
$ git remote add origin2 git@github.com:tianqixin/runoob-git-test.git

$ git remote -v
origin  git@github.com:tianqixin/runoob-git-test.git (fetch)
origin  git@github.com:tianqixin/runoob-git-test.git (push)
origin2 git@github.com:tianqixin/runoob-git-test.git (fetch)
origin2 git@github.com:tianqixin/runoob-git-test.git (push)

# 删除仓库 origin2
$ git remote rm origin2
$ git remote -v
origin  git@github.com:tianqixin/runoob-git-test.git (fetch)
origin  git@github.com:tianqixin/runoob-git-test.git (push)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  git