一, Git 基础
1. 查看文件状态
Git status -s
添加 - s 参数将会以简单的格式输出文件名列表
添加 - v 参数将会显示文件的具体修改
M config/index.JS
M config/prod.env.JS
2..gitignore 文件格式
所有空行或者以 # 开头的行都会被 Git 忽略.
可以使用标准的 glob 模式匹配.
匹配模式可以以 (/) 开头防止递归.
匹配模式可以以 (/) 结尾指定目录.
要忽略指定模式以外的文件或目录, 可以在模式前加上惊叹号 (!) 取反.
3.Git diff 查看以暂存和未暂存修改
查看工作区未暂存的修改
Git diff
查看暂存区的修改
- Git diff --cached
- Git diff --staged
4. 文件的提交
跳过使用暂存区(直接提交当前工作区的修改)
Git commit -a -m 'update'
5. 移除文件
如果从工作区手动删除某个文件, 执行提交后删除的文件会出现在
Changes not staged for commit:
下, 如果要从 Git 仓库中删除需要执行 Git rm
Git rm fileName
强制删除
Git rm -f fileName
6. 关于移动文件
在 Git 中移动文件可以执行
Git mv oldFileName newFileName
此时 Git status 可以看到
- Changes to be committed:
- (use "git reset HEAD <file>..." to unstage)
- renamed: oldFileName -> newFileName
其实, 运行 Git mv 就相当于运行了下面三条命令:
- mv README.md README
- Git rm README.md
- Git add README
7. 查看提交历史
默认 Git log 会以时间顺序显示所有的更新
- $ Git log
- commit c7b89b19bcdff982c850721ec310e80dad4092ab (HEAD -> master)
- Author: Mario34 <1506747508@qq.com>
- Date: Tue May 14 18:46:03 2019 +0800
- t3
- commit ab4e5675b37121f67e786a8504d1a1b9ef08b723
- Author: Mario34 <1506747508@qq.com>
- Date: Tue May 14 18:43:19 2019 +0800
- t2
- commit 7b97c7e4dec3fa08f8bc03da4354269028a70404
- Author: Mario34 <1506747508@qq.com>
- Date: Tue May 14 18:41:47 2019 +0800
- first
Git log -p -1 查询最近一次的文件更改
- $ Git log -p -1
- commit 7b97c7e4dec3fa08f8bc03da4354269028a70404
- Author: Mario34 <1506747508@qq.com>
- Date: Tue May 14 18:41:47 2019 +0800
- first
- diff --Git a/README.md b/README.md
- new file mode 100644
- index 0000000..be78fa0
- --- /dev/null
- +++ b/README.md
- @@ -0,0 +1,57 @@
- +# Git 命令深入
- +
- +
- +
- +#### 1. 查看文件状态
- +
- +```bash
选项 | 说明 |
---|---|
-p | 按补丁格式显示每个更新之间的差异。 |
--online | 每次提交的检验码和提交备注 |
--stat | 显示每次更新的文件修改统计信息。 |
--shortstat | 只显示 --stat 中最后的行数修改添加移除统计。 |
--name-only | 仅在提交信息后显示已修改的文件清单。 |
--name-status | 显示新增、修改、删除的文件清单。 |
--abbrev-commit | 仅显示 SHA-1 的前几个字符,而非所有的 40 个字符。 |
--relative-date | 使用较短的相对时间显示(比如,“2 weeks ago”)。 |
--graph | 显示 ASCII 图形表示的分支合并历史。 |
--pretty | 使用其他格式显示历史提交信息。可用的选项包括 oneline,short,full,fuller 和 format(后跟指定格式)。 |
-(n) | 仅显示最近的 n 条提交 |
--since, --after | 仅显示指定时间之后的提交。 |
--until, --before | 仅显示指定时间之前的提交。 |
--author | 仅显示指定作者相关的提交。 |
--committer | 仅显示指定提交者相关的提交。 |
--grep | 仅显示含指定关键字的提交 |
-S | 仅显示添加或移除了某个关键字的提交 |
8. 撤销操作
撤销当前: 如果在一次提交时提交信息出现填写错误, 在未做任何修改的情况下执行
Git commit --amend -m 'newCommit'
重新提交将会覆盖原来的提交信息
- $ Git commit -a -m 'test1'
- [master 8044bf2] test1
- 1 file changed, 3 insertions(+), 1 deletion(-)
- $ Git commit --amend -m 'newCommit'
- [master bfae5a2] newCommit
- Date: Wed May 15 11:29:45 2019 +0800
- 1 file changed, 3 insertions(+), 1 deletion(-)
- $ Git log -2
- commit bfae5a2918ce3ffbbb3b076fa8656d0c97c446a8 (HEAD -> master)
- Author: Mario34 <1506747508@qq.com>
- Date: Wed May 15 11:29:45 2019 +0800
- newCommit
- commit 9e582ae723cd6555811db8339b140050c0b44aec
- Author: Mario34 <1506747508@qq.com>
- Date: Wed May 15 11:25:09 2019 +0800
- init
取消已经暂存的文件
当暂存区存在某个文件时提示 "git reset HEAD <file>..." to unstage
- $ Git status
- On branch master
- Changes to be committed:
- ("git reset HEAD <file>..." to unstage)
- modified: README.md
使用 Git reset HEAD README.md 可以取消暂存, 再次查看文件状态被修改的文件变成 unstaged 状态
- $ Git reset HEAD README.md
- Unstaged changes after reset:
- M README.md
- $ 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.md
- no changes added to commit (use "git add" and/or "git commit -a")
撤销文件修改
将文件还原成上次提交的状态, 此操作将会将会丢失当前工作区的文件, 并且不可回复, 谨慎操作
9. 远程仓库
查看远程仓库: 运行 Git remote 命令查看已经配置的远程仓库
- $ Git remote
- origin
origin 是 Git 给远程仓库的默认名字, 同时也可以指定 - v 显示远程仓库名对应的 url
- $ Git remote -v
- origin https://github.com/userName/demo.git (fetch)
- origin https://github.com/userName/demo.git (push)
查看某一个远程仓库的更多信息, 可以使用 Git remote show [remote-name] 命令
- $ Git remote show origin
- * remote origin
- Fetch URL: https://github.com/userName/demo.git
- Push URL: https://github.com/userName/demo.git
- HEAD branch: master
- Remote branch:
- master tracked
- Local branch configured for 'git pull':
- master merges with remote master
- Local ref configured for 'git push':
- master pushes to master (up to date)
添加远程仓库: 运行
- Git remote add <libraryName> <url>
- $ Git remote add test https://libraryUrl/demo/demo.Git
- $ Git remote
- origin
- test
可以添加多个远程仓库, 并且可以使用仓库对应的名称代替仓库地址
远程仓库中抓取与拉取
$ Git fetch [remote-name]
这个命令会访问远程仓库, 从中拉取所有你还没有的数据. 执行完成后, 你将会拥有那个远程仓库中所有分支的引用, 可以随时合并或查看.
推送到远程仓库
Git push [remote-name] [branch-name]
, 下面是推送当前分支到远程的 master 分支
$ Git push origin master
远程仓库的移除与重命名: 可以运行
Git remote rename <oldName> <newName>
去修改一个远程仓库的简写名
- $ Git remote
- origin
- test
- $ Git remote rename test edited
- $ Git remote
- edited
- origin
可以使用 Git remote rm 命令移除一个远程仓库
- $ Git remote
- edited
- origin
- $ Git remote rm edited
- $Git remote
- origin
10. 打标签
像其他版本控制系统 (VCS) 一样, Git 可以给历史中的某一个提交打上标签, 以示重要.
查看所有标签 Git tag
- $ Git tag
- v0.1
- v1.3$ Git
- $ Git tag -a v1.4 -m 'my version 1.4'
- $ Git tag
- v0.1
- v1.3
- v1.4
- $ Git show v1.4
- tag v1.4
- Tagger: Ben Straub <ben@straub.cc>
- Date: Sat May 3 20:19:12 2014 -0700
- my version 1.4
- commit ca82a6dff817ec66f44342007202690a93763949
- Author: Scott Chacon <schacon@gee-mail.com>
- Date: Mon Mar 17 21:52:11 2008 -0700
- changed the version number
- $ Git tag v1.4-lw
- $ Git tag
- v0.1
- v1.3
- v1.4
- v1.4-lw
- v1.5
- $ Git show v1.4-lw
- commit ca82a6dff817ec66f44342007202690a93763949
- Author: Scott Chacon <schacon@gee-mail.com>
- Date: Mon Mar 17 21:52:11 2008 -0700
- changed the version number
- $ Git log --pretty=oneline
- 15027957951b64cf874c3557a0f3547bd83b3ff6 Merge branch 'experiment'
- a6b4c97498bd301d84096da251c98a07c7723e65 beginning write support
- 0d52aaab4479697da7686c15f77a3d64d9165190 one more thing
- 6d52a271eda8725415634dd79daabbc4d9b6008e Merge branch 'experiment'
- 0b7434d86859cc7b8c3d5e1dddfed66ff742fcbc added a commit function
- 4682c3261057305bdd616e23b64b0857d832627b added a todo file
- 166ae0c4d3f420721acbb115cc33848dfcc2121a started write support
- 9fceb02d0ae598e95dc970b74767f19372d61af8 updated rakefile
- 964f16d36dfccde844893cac5b347e7b3d44abbc commit the todo
- 8a5cbc430f1a9c3d00faaeffd07798508422908a updated readme
- $ Git tag
- v0.1
- v1.2
- v1.3
- v1.4
- v1.4-lw
- v1.5
- $ Git show v1.2
- tag v1.2
- Tagger: Scott Chacon <schacon@gee-mail.com>
- Date: Mon Feb 9 15:32:16 2009 -0800
- version 1.2
- commit 9fceb02d0ae598e95dc970b74767f19372d61af8
- Author: Magnus Chacon <mchacon@gee-mail.com>
- Date: Sun Apr 27 20:43:35 2008 -0700
- updated rakefile
- $ Git push origin v1.5
- Counting objects: 14, done.
- Delta compression using up to 8 threads.
- Compressing objects: 100% (12/12), done.
- Writing objects: 100% (14/14), 2.05 KiB | 0 bytes/s, done.
- Total 14 (delta 3), reused 0 (delta 0)
- To Git@GitHub.com:schacon/simplegit.Git
- * [new tag] v1.5 -> v1.5
- $ Git push origin --tags
- Counting objects: 1, done.
- Writing objects: 100% (1/1), 160 bytes | 0 bytes/s, done.
- Total 1 (delta 0), reused 0 (delta 0)
- To Git@GitHub.com:schacon/simplegit.Git
- * [new tag] v1.4 -> v1.4
- * [new tag] v1.4-lw -> v1.4-lw
- $ Git tag -d v1.4-lw
- Deleted tag 'v1.4-lw' (was e7d5add)
- $ Git push origin :refs/tags/v1.4-lw
- To /Git@GitHub.com:schacon/simplegit.Git
- - [deleted] v1.4-lw
- $ Git checkout 2.0.0
- Note: checking out '2.0.0'.
- You are in 'detached HEAD' state. You can look around, make experimental
- changes and commit them, and you can discard any commits you make in this
- state without impacting any branches by performing another checkout.
- If you want to create a new branch to retain commits you create, you may
- do so (now or later) by using -b with the checkout command again. Example:
- Git checkout -b <new-branch>
- HEAD is now at 99ada87... Merge pull request #89 from schacon/appendix-final
- $ Git checkout 2.0-beta-0.1
- Previous HEAD position was 99ada87... Merge pull request #89 from schacon/appendix-final
- HEAD is now at df3f601... add atlas.JSON and cover image
- $ Git checkout -b version2 v2.0.0
- Switched to a new branch 'version2'
- $ Git config --global alias.ci commit
- $ Git config --global alias.unstage 'reset HEAD --'
- $ Git branch test
- $ Git branch
- * master
- test
- $ Git checkout test
- Switched to branch 'test'
- $ Git checkout master
- Switched to branch 'master'
- $ Git checkout -b bug
- Switched to a new branch 'bug'
- $ Git commit -a -m 'fix bug'
- [master d7b96bd] fix bug
- 1 file changed, 7 insertions(+), 2 deletions(-)
- $ Git checkout master
- Switched to branch master
- $ Git merge bug
- Updating 08bc667..8c393f3
- Fast-forward
- README.md | 4 ++--
- 1 file changed, 2 insertions(+), 2 deletions(-)
- $ Git branch -d bug
- Deleted branch bug (was 08bc667).
- $ Git push origin --delete serverfix
- To https://github.com/schacon/simplegit
- - [deleted] serverfix
- $ Git checkout experiment
- $ Git rebase master
- First, rewinding head to replay your work on top of it...
- Applying: added staged command
- $ Git checkout master
- $ Git merge experiment
来源: http://www.jianshu.com/p/375d84673ae2