配置用户信息
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
- 注意
如果使用了 --global 选项,那么该命令只需要运行一次
当你想针对特定项目使用不同的用户名称与邮件地址时,可以在那个项目目录下运行没有 --global 选项的命令来配置
检查配置信息
git config --list
基础
1.克隆现有的仓库 — git clone [url]
1. git clone https://github.com/libgit2/libgit2
2. git clone https://github.com/libgit2/libgit2 mylibgit (自定义本地仓库的名字为mylibgit)
2.检查当前文件状态
git status 或者显示简写 git status -s
- 状态返回信息
1.没有任何改动
On branch master
nothing to commit, working directory clean
2.创建新文件(文件未被跟踪 )
On branch master
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)
3.文件处于暂存状态(运行git add之后的状态)
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: README
4.修改已暂存的文件
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: CONTRIBUTING.md
3.暂存已修改文件
git add CONTRIBUTING.md
4.提交更新
git commit -m '提交信息'
跳过使用暂存区域(Git 就会自动把所有已经跟踪过的文件暂存起来一并提交)
git commit -a -m 'added new benchmarks'
5.移除文件
git rm PROJECTS.md
git rm -f PROJECTS.md (强制删除)
git rm --cached README (从 Git 仓库中删除,但保留在当前工作目录中)
6.查看提交历史
git log
git log -p -2 (最近两次提交的内容差异)
git log --stat (每次提交的简略的统计信息)
git log --pretty=oneline (使用不同于默认格式的方式展示提交历史)
git log --pretty=format:"%h - %an, %ar : %s" (定制要显示的记录格式)
7. 撤消操作
取消暂存的文件(运行git add之后)
git reset HEAD CONTRIBUTING.md
撤消对文件的修改
git checkout -- CONTRIBUTING.md
撤销某次提交
git log //查看提交记录
1.回滚*到*某个提交记录
git reset --hard 75d7a025771f1451318be259feaf749c0c28dd98 //指向该提交记录
git push -f
2.回滚某个提交记录
git revert -n 75d7a025771f1451318be259feaf749c0c28dd98 //用于“反做”某一个版本,以达到撤销该版本的修改的目的
git revert --continue
git push
8.Git 别名
git config --global alias.co checkout
远程仓库的使用
1. 查看远程仓库
git remote (列出远程服务器的简写)
git remote -v (显示需要读写远程仓库使用的 Git 保存的简写与其对应的 URL)
2. 从远程仓库中抓取与拉取
git fetch [remote-name] (不会自动合并或修改你当前的工作)
git pull (自动尝试合并到当前所在的分支。)
3. 推送到远程仓库
git push [remote-name] [branch-name] (之前有推送,必须拉取合并之后才能推送)
打标签
1.列出标签
git tag
git tag -l 'v1.8.5*' (使用特定的模式查找标签)
2.创建标签
git tag -a v1.4 -m "my version 1.4" (附注标签)
git tag v1.4 (轻量标签)
3.共享标签(推送标签到远程仓库)
git push origin [tagname]
4.后期打标签
1.git log --pretty=oneline (首先查看提交历史)
2.git tag -a v1.2 9fceb02
5.删除标签
git tag -d <tagname> (删除本地标签)
git push <remote> :refs/tags/<tagname> (更新到远程仓库)
6.检出标签
git checkout 2.0.0
7.批量操作标签
git tag -l | xargs git tag -d #删除所有本地分支
git fetch origin --prune #从远程拉取所有信息
git tag -l | awk '/v0.[0-4].[0-9]/{print}' | xargs git push origin --delete tag #批量删除Tag
注意: 如果你做了某些更改然后提交它们,标签不会发生变化,因此,如果你需要进行更改——比如说你正在修复旧版本的错误——这通常需要创建一个新分支:
分支
1.分支创建与分支切换
git branch testing
git checkout testing
git checkout -b testing (创建新分支且切换到该分支)
2.分支的合并
git checkout master (切换到需要合并的分支)
git merge testing (合并testing分支)
- 遇到冲突时的分支合并
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
解决冲突:
1.手动更改冲突文件
2.使用图形化工具来解决冲突,运行 git mergetool
3.删除分支
git branch -d testing
git push origin --delete testing (删除远程分支)
4.远程分支
git push origin serverfix (推送远程分支)
git checkout -b serverfix origin/serverfix (本地拉取远程分支)