Git常用命令

作者: wangbu2 | 来源:发表于2017-09-06 19:02 被阅读0次

1、创建仓库

git clone

$ git clone <url> 克隆远程仓库到本地

git init

$ git init 初始化本地仓库

2、修改和提交

git status

$ git status 查看状态

git add

$ git add . 跟踪所有改动过的文件
$ git add <file> 跟踪指定的文件

git mv

$ git mv <old> <new> 修改文件名

git commit

$ git commit -m "commit messages..." 提交所有更新过的文件,并记录一行简单提交信息
$ git commit 启动编辑器,记录详细提交信息
$ git commit --amend 修改最后一次提交信息
$ git commit -am "Some information..." 添加跟踪并提交文件

3、查看提交历史

git log

$ git log 查看提交历史
$ git log <file> 只显示指定目录、文件的日志
$ git log -p <file> 显示文件的改动,只查看文件的提交日志以及提交前后的差别
$ git blame <file> 以列表方式查看指定文件的提交历史
$ git log --pretty-short 只显示提交信息的第一行
$ git log --graph 以图表形式查看分支

git diff

$ git diff 查看工作树和暂存区的差别
$ git diff HEAD 查看工作树和最新提交的差别

git reflog

$ git reflog 查看当前仓库的日志

4、撤销

git reset

$ git reset --hard <commit> 回溯历史版本
$ git reset --hard HEAD 撤销工作目录中所有未提交文件的修改内容
$ git checkout HEAD <file> 撤销指定的未提交文件的修改内容

<commit>即某一次commit的hashcode。

关于git reset命令的详细介绍,请参考另一篇文章:git reset 命令详解

git revert

$ git revert <commit> 撤销指定的提交

git log

$ git log --before="1 days" 退回到之前1天的版本

git rm

$ git rm -f <file> 强制删除文件
$ git rm --cached <file> 停止跟踪文件,但不删除

5、分支与标签

git branch

$ git branch 显示所有本地分支
$ git branch -a 同时显示本地仓库和远程仓库的分支信息
$ git branch <new-branch> 创建新分支
$ git branch -d <branch> 删除本地分支

git checkout

$ git checkout <branch> 切换到指定分支
$ git checkout -b branch-A 创建分支A,并切换到分支A

6、合并

git merge

$ git merge <branch> 合并指定分支到当前分支
$ git merge --no-ff branch-A 将分支A合并到master分支。

7、远程操作

git remote

$ git remote -v 查看远程仓库库信息
$ git remote show <remote> 查看指定远程仓库信息
$ git remote add <remote> <url> 设置本地仓库的远程仓库
$ git remote rename <remote> <remote new name> 重命名远程仓库
$ git remote rm <remote> 解除远程仓库关联

<remote>默认为origin。

git fetch

$ git fetch <remote> 从远程仓库获取代码

git pull

$ git pull <remote> <branch> 下载代码及快速合并

git push

$ git push 上传当前分支代码到远程仓库
$ git push -u origin <branch> 在远程仓库新建分支,并上传代码

8、其他

git config

$ git config --global user.name "Your Name"
$ git config --global user.email "e-mail@example.com"
$ git config --list 查看用户配置列表

git --version

$ git --version 可以查看当前 Git 版本

touch

$ touch .gitignore 创建.gitignore文件

相关文章

网友评论

    本文标题:Git常用命令

    本文链接:https://www.haomeiwen.com/subject/itcvjxtx.html