Git 2 - Git Basics
1 add
- github 上建立新的 repository,命名 demo
- git clone 到本地 github 目录
- 将自己之前的项目 copy 到该 demo 目录下
- 运行 cat .gitignore
注意: .gitignore的目录并不一定要是根目录,比如在 try 文件夹下建立一个 .gitignore 来设置 try 文件夹中的忽略文件:$ cat try/.gitignore 即可。其 ignore 的内容为
rd.txt
/rd/
如果此前这些文件已经被 track ,则使用 git rm --cached 删除 staged 区的对应文件。
- git add .
- git status 查看,除去被 ignore 的文件,其他文件都是 staged 状态,说明 gitignore 生效
- 修改其中一个被跟踪的文件 readme.md,再查看,则之后修改的文件状态为 modified (changes not staged for commit)
注意:add 的意思是 add this content to the next commit(即 add 后会进入 staged 状态)
2 commit
- 将 git add .,之后git commit -m "inital"
- git status,此时nothing to commit, working directory clean
- 之后 push 之前再 commit 就需要使用 git commit --amend.可以修改提交说明,修改后,按Esc进入命令行,:,wq或wq!(强制执行)。之后可以查看本次的 commit 信息:master 分支, ccced2c 是 SHA-1 校验和。
$ git commit --amend
[master ccced2c] first commit
158 files changed, 17500 insertions(+)
create mode 100644 .classpath
create mode 100644 .gitignore
- 直接 commit ,无需 add 。使用$git commit -a -m 'added'
3 查看修改
- git diff (查看 working directory 与 staged 区的不同)
- $git diff --staged(查看 staged 区与 commit 区的不同)
4 删除文件(指从 staging 区删除,也就是不再 track)
- 工作区和 staging 区都删除
xueyuan@EF-XUEYUAN /D/github/demo (master)
$ git rm try.md
rm 'try.md'
xueyuan@EF-XUEYUAN /D/github/demo (master)
$ git diff
xueyuan@EF-XUEYUAN /D/github/demo (master)
$ git diff --staged
diff --git a/try.md b/try.md
deleted file mode 100644
index e69de29..0000000
- 只 staging 区删除,工作区不删除(忽略 gitignore 时经常使用)
xueyuan@EF-XUEYUAN /D/github/demo (master)
$ git rm --cached try.md
rm 'try.md'
xueyuan@EF-XUEYUAN /D/github/demo (master)
$ git diff
xueyuan@EF-XUEYUAN /D/github/demo (master)
$ git diff --staged
diff --git a/try.md b/try.md
deleted file mode 100644
index e69de29..0000000
注意:$git rm log/*.log 中的“”
5 重命名
$git mv file_from file_to
6 查看 commit 历史
$git log 所有历史
$git log -p -2 最后两此提交作比较
$git log --stat 查看每次提交的简略统计数据(修改文件数,修改行数)
限制输出数量如:
$git log --since=2.weeks
其他如下:
xueyuan@EF-XUEYUAN /D/github/demo (master)
$ git log --pretty=oneline
c38bac265c1337835cad91f9bf0cb2d2aa91a103 second commit
62ff73d37c185377cf1911a3e5fe2f58225f0136 first commit
xueyuan@EF-XUEYUAN /D/github/demo (master)
$ git log --pretty=format:"%h - %an, %ar : %s"
c38bac2 - xueyuan, 9 minutes ago : second commit
62ff73d - xueyuan, 3 hours ago : first commit
xueyuan@EF-XUEYUAN /D/github/demo (master)
$ git log --pretty=format:"%h %s"--graph
c38bac2 second commit--graph
62ff73d first commit--graph
7 撤销操作
(1)重提交
$git commit --amend
$git commit -m 'initial commit'
$git add forgotten_file
$git commit --amend
这样第二次提交取代第一次,一共只有一次提交
(2)Unstaging a Staged File
$git reset HEAD CONTRIBUTING.md
$git add .
$git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
modified: CONTRIBUTING.md
$git reset HEAD CONTRIBUTING.md
Unstaged changes after reset:
M CONTRIBUTING.md
$git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> 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: CONTRIBUTING.md
(3)Unmodifying a Modified File
恢复到last committed (or initially cloned, or however you got it into your working directory)
$git checkout -- CONTRIBUTING.md
$git checkout -- CONTRIBUTING.md
$git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> READMEc
注意:所有(包括被删除的分支,和--amend的提交)被 commit 到 Git 的都可以被恢复。
8 远程仓库
- 查看远程仓库:$git remote -v
- 添加远程仓库:$git remote add pb https://github.com/paulboone/ticgit
- 抓取本地库没有的信息:
- $git fetch pb(需手动合并)
- $git pull pb(自动合并到当前分支,常用)
- 推送数据到远程仓库:$git push [remote名] [branch名]
注意:如果 push 前有他人已经推送了更新,则你的推送会被驳回。此时必须先把他人的更新抓取到本地,并入自己的项目,再推送。 - 查看远程仓库信息:$git remote show [remote-name]
- 删除远程仓库:$git remote rm [remote-name]
- 重命名远程仓库:$git remote rename [old] [new]