git 命令操作

175 阅读3分钟

基本操作:

git status:                 --查看文件状态
git diff <filname>:         --查看文件差异
git log:                    --查看提交记录
git mv <oldfil> <newfile>:  --修改,移动文件名称
git rm <file>:              --删除文件
git checkout -- <file>:    --还原本地修改,还没添加到索引
git reset HEAD -- <file>:   --删除已添加到索引的文件
git add -u:                 --只添加已提交过的文件到索引

撤销操作

1.在工作区中的代码,没有add
git checkout .             --撤销所有本地修改过的文件
git checkout fileName,    --撤销单个文件

git clean -xdf            --删除untrack过的文件,目录,和gitignore的untrack 文件/目录
git clean -n              --显示将要删除的文件和目录,只显示,不删除
git clean -f              --删除untrack过的文件
git clean -df             --删除untrack过的文件和目录 



2.代码git add到缓存区,并未commit提交
git reset HEAD .  或者
git reset HEAD <filename>

3.撤销commit
git commit --amend
git commit --amend -m"说明

4.git commit到本地分支、但没有git push到远程
git log # 得到你需要回退一次提交的commit id
git reset --hard <commit_id>  # 回到其中你想要的某个版
git reset --hard HEAD^  # 回到最新的一次提交

5.


配置用户信息:

git config --global user.name "john"
git config --global user.email "john@example.com"

git config --list :     -- 查看配置信息
git config <key>:       -- 查看某一项配置

获取帮助:

git help <verb>
git <verb> --help 
man git-<verb>

比如查看配置信息:
git help config

获取仓库:

git init:                   --初始化仓库
git clone [url]:            --克隆仓库
git clone [url] [newName]:   --克隆仓库并自定义仓库的名称

查看状态:

git status
git status -s :             -- 简洁的状态

这里有几种标记:
??                          -- 未跟踪的文件
A                           -- 新添加到暂存区的文件
M                           -- 修改了并放入了暂存区                         
 M                          -- 修改了但没放入暂存区
D                           -- 删除的文件

查看文件差异:

git diff:                   --查看本地文件差异
git diff --staged           --查看暂存区差异
git difftool                --图形化界面查看差异
git difftool --tool-help    --查看Git Diff插件

跳过使用暂存区:

git commit -a "提交信息"    -- 跳过 git add 步骤,提交所有已跟踪的文件

相当于:
git add -u
git commit -m "提交信息"

移除文件

git rm <file>:             --从暂存区移除文件
git rm -f <file>:           --强制删除

从暂存区删除文件,但保留工作区(比如日志,等不需要的文件传到了暂存区):

git rm --cached <file>

删除文件
比如删除log目录下所有扩展名为.log的文件:

git rm log/\*.log

注意:*号之前有反斜杠

移动文件

git mv file_from file_to

相当于

mv a.txt b.txt
git rm a.txt
git add b.txt

查看提交历史:

git log 
git log -p:                     --显示每次提交的差异  
git log -p <file>:              --查看指定文件的提交历史
git log -n:                     --显示最近n次提交
git log --stat:                 --提交的简略的统计信息
git log --graph:                --显示 ASCII 图形表示的分支合并历史

git log --pretty=online         --一行显示
参数还有:short, full, fuller

撤消操作:

git checkout --<file>:              --取消某个本地文件的修改
git checkout .                      --取消所有本地文件的修改

git reset HEAD <file>:              --取消暂存区的修改

远程仓库的使用:

git remote -v:                              --查看远程仓库
git remote add <shortname> <url>:           -- 添加远程仓库
git pull <remote-name> <remote-branch>:     --从远程仓库拉取代码
git push <remote-name> <remote-branch>:     --提交代码到远程仓库

git remote show <remote-name>:              --查看远程仓库更详细的信息

git remote rename <old-name> <new-name>:    --重命名远程仓库 
git remote rm <remote-name>                 --删除远程仓库

打标签

git tag:                 --列出标签

创建附注标签:

git tag -a v1.4 -m "my version 1.4"

说明:-a 指标签名,-m 指该标签的提交信息

查看标签信息:

git show <tag-name>

创建轻量标签: git tag <tabname> --不带 -a 选项

后期打标签

git tag -a <tag-name> <commit-hash>    

可以先运行 git log --pretty=online   查看提交历史,
然后给需要打标签的信息打上标签

共享标签:

git push origin <tag-name>:                 --显式的提交标签信息

git push origin --tags                      --一次提交多个标签

删除标签

git tag -d <tag-name>                       --删除本地标签
git push <remote-name> :refs/tags/<tag-name>
                                            --这样才能删除远程仓库上的标签