git_day02_01——git基本操作
2017-05-28 00:00
218 查看
git命令行基本操作
获取git仓库方式有两种在享有的项目或目录下打入所有文件到Git中
从一个服务器克隆一个现有的Git仓库
将目录初始化为git目录
$ git init
该命令将在该目录中创建一个名为.git的子目录,子目录中含有初始化Git仓库所必须的文件,这些文件为Git仓库的骨干,但是仅仅做了初始化的操作,该项目中的文件还没有被跟踪,需要进行一下操作
$ git add *.c $ git add LICENSE $ git commit -m 'initial project version'
克隆现有的仓库
所用的命令是git clone 而不是checkout, 当你执行命令的时候 默认配置下远程Git仓库中的每一个文件的每一个版本都将被拉取下来。克隆仓库的命令格式是git clone [url]
$ git clone https://github.com/libgit2/libgit2[/code] 自定义本地仓库的名字可以使用如下命令$ git clone https://github.com/libgit2/libgit2 mylibgit
记录每次更新到仓库
在工作目录下的每一个文件都不外乎两种状态:已跟踪或未跟踪,已跟踪文件是指那些被纳入了版本控制的文件,在上一次快照中有他们的记录,工作一段时间后,他们的状态可能处于未修改,已修改或已放入暂存区。 工作目录中除了已跟踪文件外的所有其他文件都属于未跟踪文件,他们即不存在于上一次快照记录中,也没有放入暂存区。
编辑过某些文件之后,由于自上次提交后对它们做了修改,Git将它们标记为已修改文件,我们可以将这些修改过的文件放入暂存区,然后提交所有暂存了的修改。
Git文件的声明周期如下:
查看当前文件状态[root@localhost libgit2]# git status On branch master Your branch is ahead of 'origin/master' by 3 commits. (use "git push" to publish your local commits) nothing to commit, working directory clean
暂存新添加的文件 , 显示了当前所在的分支,分支名是"master" 这是默认的分支名[root@localhost libgit2]# echo "My Project" > README [root@localhost libgit2]# git status On branch master Your branch is ahead of 'origin/master' by 3 commits. (use "git push" to publish your local commits) Untracked files: (use "git add <file>..." to include in what will be committed) README nothing added to commit but untracked files present (use "git add" to track)
新添加的文件是没有被跟踪的, 位于Untracked files下面。
可以使用git add 开始跟踪一个文件 运行[root@localhost libgit2]# git add README [root@localhost libgit2]# git status On branch master Your branch is ahead of 'origin/master' by 3 commits. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: README
只要在Change to be committed 这行下面 说明是已经暂存状态,如果此时提交,该文件此时此刻的版本将被留在历史记录中。
暂存已修改的文件[root@localhost libgit2]# git status On branch master Your branch is ahead of 'origin/master' by 3 commits. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: README 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: CONVENTIONS.md
文件 如果出现在Changes not staged for commit 下面,说明被跟踪的文件内容发生改变,但是还没有放到暂存区。要暂存这次更新,需要运行git add命令。[root@localhost libgit2]# git add CONVENTIONS.md [root@localhost libgit2]# git status On branch master Your branch is ahead of 'origin/master' by 3 commits. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: CONVENTIONS.md new file: README
如果再次修改 CONVENTIONS.md 文件后 git status 显示如下[root@localhost libgit2]# git status On branch master Your branch is ahead of 'origin/master' by 3 commits. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: CONVENTIONS.md new file: README 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: CONVENTIONS.md
现在 CONVENTIONS.md 同时出现在暂存区和非暂存区,而git commit时 提交的是最后一次运行git add命令时的哪个版本 ,而不是运行git commit 工作目录中的当前版本,所以运行了 git add 之后又作了修订的文件,需要重新运行git add把罪行版本重新暂存起来。
git status 命令的输出十分详细, 而 git status -s 或 git status --short 命令将得到一种更为紧凑的个数输出 ,如下:[root@localhost libgit2]# git status -s MM CONVENTIONS.md A README
? ? 新添加的未跟踪文件前面有??标记
A 新添加到暂存区中的文件前面有A标记
M 靠左边的文件被修改了 并且放入了暂存区
M靠右边的文件被修改了 没有放入暂存区
MM两个M标识放入暂存区中的文件修改过后没有再次git add 放入暂存区中
忽略文件提交 .gitignore 文件中进行配置
查看已暂存和为暂存的修改 git diff --cached(staged)
查看未暂存文件的修改 git diff
查看已暂存文件的修改 git diff --cached
提交更新 ,并打开默认编辑器编写注释$ git commit
提交更新,在命令行后面加入注释$ git commit -m "Story comment"
跳过使用暂存区域直接提交$ git commit -a -m 'add new benchmarks'
移除文件 git rm[root@localhost libgit2]# git rm ABC rm 'ABC' [root@localhost libgit2]# git status On branch master Your branch is ahead of 'origin/master' by 9 commits. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) deleted: ABC[root@localhost libgit2]# rm -f ABC [root@localhost libgit2]# git status On branch master Your branch is ahead of 'origin/master' by 11 commits. (use "git push" to publish your local commits) Changes not staged for commit: (use "git add/rm <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) deleted: ABC no changes added to commit (use "git add" and/or "git commit -a") [root@localhost libgit2]# git rm ABC rm 'ABC' [root@localhost libgit2]# git status On branch master Your branch is ahead of 'origin/master' by 11 commits. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) deleted: ABC
两种删除文件方式,上面那种最方便,不再纳入版本管理了
如果删除之前修改过并且已经放到暂存区域的话,则必须要用强制删除选项-f (force) 。这是一种安全特性,用于防止误删还没有添加到快照的数据,因为这样的数据是不能恢复的。
如果我们想把文件从Git仓库中删除(从暂存区中删除) ,但是希望保留文件在当前工作目录(磁盘中),但是并不想Git继续跟踪,当你忘记添加.gitignore 文件 可以用 --cached 来达到这一目的[root@localhost libgit2]# git rm --cached ABC rm 'ABC' [root@localhost libgit2]# git status On branch master Your branch is ahead of 'origin/master' by 13 commits. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) deleted: ABC Untracked files: (use "git add <file>..." to include in what will be committed) ABC
git add ABC 之后 又回到 执行 git rm --cached ABC 之前的状态
移动文件 git mv file_from file_to[root@localhost libgit2]# git mv ABC ABCD [root@localhost libgit2]# git status On branch master Your branch is ahead of 'origin/master' by 13 commits. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: ABC -> ABCD
执行该命令相当于执行了下面三个命令:$ mv ABC ABCD $ git rm ABC $ git add ABCD
查看提交历史 git log:
常用选项-p 用来显示每次提交内容差异,也可以加上-2 来显示最近两次提交:$ git log -p -2
撤销操作 ,这里会学习几个撤销你做修改的基本工具,其中一些操作是不可逆的:
有时候我们提交完了才发现漏掉几个文件没有添加,或者提交信息写错了。此时可以用带有 --amend 选项的提交命令尝试重新提交。
这个命令会将暂存区中的文件提交。如果上次提交以来你还未做任何修改(内容修改),那么快照会保持不变,而你所修改的只是提交信息。
文本编辑器启动后,可以看到之前的提交信息,编辑后会覆盖原来的提交信息。
例如,你提交后发现忘记了暂存某些需要的修改,可以像下面这样操作:$ git commit -m 'initial commit' $ git add forgotten_file $ git commit --amend
最终你只会有一个提交 , 第二次提交将代替第一次提交的结果
取消暂存的文件 git reset HEAD <file> ... to unstage[root@localhost libgit2]# git status On branch master Your branch is ahead of 'origin/master' by 13 commits. (use "git push" to publish your local commits) Changes to be committed: (use "git reset HEAD <file>..." to unstage) renamed: ABC -> ABCD
撤销对文件的修改 git checkout -- <file> ...
可以通过以下方便的撤销修改 , 将它还原成上次提交时的样子(或者刚克隆完的样子,或者刚把它放入工作目录时的样子)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)$ git checkout -- ABCD
相关文章推荐
- Git学习足迹---常用的基本操作01
- Git-01_安装与基本操作
- ---Git---01---git简介与基本操作
- windows下GIT使用记录--01基本操作
- git学习 #2:git基本操作
- 01 Linux系统管理—(Linux安装及命令基本操作)
- git 基本操作与规范流程
- Git操作流程,基本命令演示
- Git基本概念及操作(1)
- Git基本操作(add,commit的理解)
- Git学习笔记(2)--Git基本操作
- Git学习笔记(3)--Git的基本操作及一些小技巧
- Git基本命令行操作
- 一步一步学习Git(2)——Git基本操作
- git基本操作
- 01-Oracle学习_引入和DBA基本操作
- git简介、安装、基本操作——Git折腾小记
- pro git 阅读笔记1 基本操作
- 01基本键盘操作——判断按键消息
- Git学习笔记(八) Git基本操作