git创建版本库、提交修改文件、查看修改文件
2017-03-23 00:00
399 查看
版本库又名仓库(repository)
第一步:选择一个合适的地方创建版本库的一个空目录:
第二步:通过git init命令把这个目录变成Git可以管理的仓库:
用
把一个文件放到Git仓库
现在我们编写一个
一定要放到
第一步:用命令
第二步,用命令
添加文件到Git仓库,分两步:
第一步,使用命令
第二步,使用命令
修改readme.txt文件,改成如下内容:
运行
用
“distributed”单词。
知道了对readme.txt作了什么修改后,再把它提交到仓库就放心多了,提交修改和提交新文件是一样的两步,
第一步是
在执行第二步
提交后,我们再用
小结
要随时掌握工作区的状态,使用git status命令。
如果git status告诉你有文件被修改过,用git diff可以查看修改内容。
第一步:选择一个合适的地方创建版本库的一个空目录:
ubuntu@ubuntu:~$ mkdir myGitRepository ubuntu@ubuntu:~$ cd myGitRepository/ ubuntu@ubuntu:~/myGitRepository$ pwd /home/ubuntu/myGitRepository
第二步:通过git init命令把这个目录变成Git可以管理的仓库:
ubuntu@ubuntu:~$ git init Initialized empty Git repository in /home/ubuntu/.git/
用
ls -ah命令就可以查看
.git目录
ubuntu@ubuntu:~$ ls -ah . .dmrc .gnupg .sudo_as_admin_successful .. Documents .ICEauthority Templates .bash_history Downloads .local Videos .bash_logout examples.desktop .mozilla .Xauthority .bashrc .gconf Music .xsession-errors .cache .git myGitRepository .xsession-errors.old .config git-2.12.0 Pictures .dbus git-2.12.0.tar.gz .profile Desktop .gitconfig Public
把一个文件放到Git仓库
现在我们编写一个
readme.txt文件,内容如下:
Git is a version control system. Git is free software.
一定要放到
/myGitRepository目录下(子目录也行),因为这是一个Git仓库,放到其他地方Git再厉害也找不到这个文件。
第一步:用命令
git add把文件添加到Git仓库:
ubuntu@ubuntu:~/myGitRepository$ git add readme.txt
第二步,用命令
git commit把文件提交到Git仓库:
ubuntu@ubuntu:~/myGitRepository$ git commit -m "commit a readme.txt" [master (root-commit) 6bd83e3] commit a readme.txt 1 file changed, 2 insertions(+) create mode 100644 myGitRepository/readme.txt
小结
初始化一个Git仓库,使用git init命令。
添加文件到Git仓库,分两步:
第一步,使用命令
git add <file>,注意,可反复多次使用,添加多个文件;
第二步,使用命令
git commit,完成。
修改readme.txt文件,改成如下内容:
Git is a distributed version control system. Git is free software.
运行
git status命令看看结果:
ubuntu@ubuntu:~/myGitRepository$ git status On branch 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: readme.txt Untracked files: (use "git add <file>..." to include in what will be committed) ../.ICEauthority ../.Xauthority ../.bash_history ../.bash_logout ../.bashrc ../.cache/ ../.config/ ../.dmrc ../.gitconfig ../.local/ ../.mozilla/ ../.profile ../.sudo_as_admin_successful ../.xsession-errors ../.xsession-errors.old ../examples.desktop ../git-2.12.0.tar.gz ../git-2.12.0/ no changes added to commit (use "git add" and/or "git commit -a")
git status命令可以时刻掌握仓库当前的状态,上面的命令告诉我们,readme.txt被修改过了,但还没有准备提交的修改。
用
git diff查看具体修改了什么内容:
ubuntu@ubuntu:~/myGitRepository$ git diff diff --git a/myGitRepository/readme.txt b/myGitRepository/readme.txt index 14ee423..dae84f9 100644 --- a/myGitRepository/readme.txt +++ b/myGitRepository/readme.txt @@ -1,2 +1,2 @@ -<U+FEFF>Git is a version control system. -Git is free software. \ No newline at end of file +<U+FEFF>Git is a distributed version control system.^M +Git is free software.^M
git diff顾名思义就是查看difference,可以从上面的命令输出看到,我们在第一行添加了一个
“distributed”单词。
知道了对readme.txt作了什么修改后,再把它提交到仓库就放心多了,提交修改和提交新文件是一样的两步,
第一步是
git add:
ubuntu@ubuntu:~/myGitRepository$ git add readme.txt
在执行第二步
git commit之前,我们再运行
git status看看当前仓库的状态:
ubuntu@ubuntu:~/myGitRepository$ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: readme.txt Untracked files: (use "git add <file>..." to include in what will be committed) ../.ICEauthority ../.Xauthority ../.bash_history ../.bash_logout ../.bashrc ../.cache/ ../.config/ ../.dmrc ../.gitconfig ../.local/ ../.mozilla/ ../.profile ../.sudo_as_admin_successful ../.xsession-errors ../.xsession-errors.old ../examples.desktop ../git-2.12.0.tar.gz ../git-2.12.0/
git status告诉我们,将要被提交的修改包括readme.txt,下一步,就可以放心地提交了:
ubuntu@ubuntu:~/myGitRepository$ git commit -m "add distributed" [master b8bbe97] add distributed 1 file changed, 2 insertions(+), 2 deletions(-)
提交后,我们再用
git status命令看看仓库的当前状态:
ubuntu@ubuntu:~/myGitRepository$ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) ../.ICEauthority ../.Xauthority ../.bash_history ../.bash_logout ../.bashrc ../.cache/ ../.config/ ../.dmrc ../.gitconfig ../.local/ ../.mozilla/ ../.profile ../.sudo_as_admin_successful ../.xsession-errors ../.xsession-errors.old ../examples.desktop ../git-2.12.0.tar.gz ../git-2.12.0/ nothing added to commit but untracked files present (use "git add" to track)
小结
要随时掌握工作区的状态,使用git status命令。
如果git status告诉你有文件被修改过,用git diff可以查看修改内容。
相关文章推荐
- git 分支的 创建 修改 切换 查看 从远程仓库抓取数据 以及 提交时忽略某文件
- Git学习(三)----文件查看修改、提交修改
- Git学习笔记一--创建版本库、添加文件、提交文件等
- git 创建、提交代码仓库,查看修改内容、撤销修改内容
- git - 查看已暂存和未暂存的更新以及文件修改之后暂存区、上次提交过的不同
- git - 查看某次提交某个文件的修改
- git 查看某一个文件的(提交/修改)记录
- git查看某一次提交修改的文件列表
- Git(PART I)创建版本库和提交修改
- Git 文件的修改提交与历史记录查看(02)
- git知识总结2——创建版本库,添加/提交文件
- Git查看某一个文件的修改记录(提交记录)
- Git之创建版本库&提交文件&相关命令
- git——创建仓库、提交、忽略文件、撤消更改、查看提交记录
- Git学习笔记一--创建版本库、添加文件、提交文件等
- Git学习笔记一--创建版本库、添加文件、提交文件等
- 如何创建.gitignore文件,忽略不必要提交的文件
- Android Eclipse之Git插件安装、配置、提交、修改、冲突、查看历史、覆盖。
- git查看某个文件的修改历史
- git使用meld查看文件修改内容