1. 获取 Git 仓库
第一种方法:在现有目录中初始化仓库
直接在目录中输入 :
初始化仓库 $ git init
跟踪文件**$ git add *.c** **$ git add LICENSE**
提交文件**$ git commit -m 'initial project version'**
第二种方法: 克隆现有的仓库 ,从远程仓库中克隆一个到本地仓库
$ git clone https://github.com/libgit2/libgit2 mylibgit
2. 检查当前文件状态
$ git status
$ echo 'My Project' > README
3. 忽略文件
$ cat .gitignore
文件 .gitignore 的格式规范如下:
所有空行或者以 # 开头的行都会被 Git 忽略。
可以使用标准的 glob 模式匹配。
匹配模式可以以(/)开头防止递归。
匹配模式可以以(/)结尾指定目录。
要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。
4. 查看尚未暂存的文件更新了哪些部分
$ git diff
$ git diff --cached
$ git diff --staged
5. 提交更新
$ git commit
$ git commit -m "Story 182: Fix benchmarks for speed"
$ git commit -a -m 'added new benchmarks' 跳过使用暂存区
6.查看提交历史
$ git log
$ git log -p -2
$ git log --stat
$ git log --pretty=format:"%h - %an, %ar : %s"
$ git log --pretty=format:"%h %s" --graph
$ git log --since=2.weeks
7. 重新提交
提交完了才发现漏掉了几个文件没有添加,或者提交信息写错了,可以运行。
$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend
8. 取消暂存的文件
$ git reset HEAD CONTRIBUTING.md
9. 撤消对文件的修改
$ git checkout -- CONTRIBUTING.md
10. 查看远程仓库
$ git remote -v
$ git remote add pb https://github.com/paulboone/ticgit 添加远程仓库
$ git fetch [remote-name] 从远程仓库中拉取
$ git push origin master 推送到远程仓库
$ git remote rename pb paul 远程仓库的移除
$ git remote rm paul 重命名
11. 打标签
$ git tag 列出标签
$ git tag -l 'v1.8.5*' 特定的模式查找标签
$ git tag -a v1.4 -m 'my version 1.4' 创建附注标签
$ git tag v1.4-lw 创建轻量标签
$ git tag -a v1.2 9fceb02 后期打标签
$ git push origin v1.5 共享标签
12. 分支创建
$ git branch testing
$ git log --oneline --decorate 查看各个分支当前所指的对象
$ git checkout testing 分支切换
$ git checkout -b iss53 创建并切换至iss53
13. 合并分支
$ git merge hotfix
14. 变基
$ git checkout experiment
$ git rebase master