Git常用命令

命令名称 作用
git config –global user.name用户名 设置用户签名
git config –global user.email邮箱 设置用户签名
git int 初始化本地库
git staus 查看本地库的状态
git add文件名 添加到暂存区
git commit -m “日志信息”文件名称 提交到本地库
git reflog 查看历史记录
git reset –hard 版本穿梭
git init                                                  # 初始化本地git仓库(创建新仓库)
git config --global user.name "xxx"                       # 配置用户名
git config --global user.email "xxx@xxx.com"              # 配置邮件
git config --global color.ui true                         # git status等命令自动着色
git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto
git config --global color.interactive auto
git config --global --unset http.proxy                    # remove  proxy configuration on git
git clone git+ssh://git@192.168.53.168/VT.git             # clone远程仓库
git status                                                # 查看当前版本状态(是否修改)
git add xyz                                               # 添加xyz文件至index
git add .                                                 # 增加当前子目录下所有更改过的文件至index
git commit -m 'xxx'                                       # 提交
git commit --amend -m 'xxx'                               # 合并上一次提交(用于反复修改)
git commit -am 'xxx'                                      # 将add和commit合为一步
git rm xxx                                                # 删除index中的文件
git rm -r *                                               # 递归删除
git log                                                   # 显示提交日志
git log -1                                                # 显示1行日志 -n为n行
git log -5
git log --stat                                            # 显示提交日志及相关变动文件
git log -p -m
git show dfb02e6e4f2f7b573337763e5c0013802e392818         # 显示某个提交的详细内容
git show dfb02                                            # 可只用commitid的前几位
git show HEAD                                             # 显示HEAD提交日志
git show HEAD^                                            # 显示HEAD的父(上一个版本)的提交日志 ^^为上两个版本 ^5为上5个版本
git tag                                                   # 显示已存在的tag
git tag -a v2.0 -m 'xxx'                                  # 增加v2.0的tag
git show v2.0                                             # 显示v2.0的日志及详细内容
git log v2.0                                              # 显示v2.0的日志
git diff                                                  # 显示所有未添加至index的变更
git diff --cached                                         # 显示所有已添加index但还未commit的变更
git diff HEAD^                                            # 比较与上一个版本的差异
git diff HEAD -- ./lib                                    # 比较与HEAD版本lib目录的差异
git diff origin/master..master                            # 比较远程分支master上有本地分支master上没有的
git diff origin/master..master --stat                     # 只显示差异的文件,不显示具体内容
git remote add origin git+ssh://git@192.168.53.168/VT.git # 增加远程定义(用于push/pull/fetch)
git branch                                                # 显示本地分支
git branch --contains 50089                               # 显示包含提交50089的分支
git branch -a                                             # 显示所有分支
git branch -r                                             # 显示所有原创分支
git branch --merged                                       # 显示所有已合并到当前分支的分支
git branch --no-merged                                    # 显示所有未合并到当前分支的分支
git branch -m master master_copy                          # 本地分支改名
git checkout -b master_copy                               # 从当前分支创建新分支master_copy并检出
git checkout -b master master_copy                        # 上面的完整版
git checkout features/performance                         # 检出已存在的features/performance分支
git checkout --track hotfixes/BJVEP933                    # 检出远程分支hotfixes/BJVEP933并创建本地跟踪分支
git checkout v2.0                                         # 检出版本v2.0
git checkout -b devel origin/develop                      # 从远程分支develop创建新本地分支devel并检出
git checkout -- README                                    # 检出head版本的README文件(可用于修改错误回退)
git merge origin/master                                   # 合并远程master分支至当前分支
git cherry-pick ff44785404a8e                             # 合并提交ff44785404a8e的修改
git push origin master                                    # 将当前分支push到远程master分支
git push origin :hotfixes/BJVEP933                        # 删除远程仓库的hotfixes/BJVEP933分支
git push --tags                                           # 把所有tag推送到远程仓库
git fetch                                                 # 获取所有远程分支(不更新本地分支,另需merge)
git fetch --prune                                         # 获取所有原创分支并清除服务器上已删掉的分支
git pull origin master                                    # 获取远程分支master并merge到当前分支
git mv README README2                                     # 重命名文件README为README2
git reset --hard HEAD                                     # 将当前版本重置为HEAD(通常用于merge失败回退)
git rebase
git branch -d hotfixes/BJVEP933                           # 删除分支hotfixes/BJVEP933(本分支修改已合并到其他分支)
git branch -D hotfixes/BJVEP933                           # 强制删除分支hotfixes/BJVEP933
git ls-files                                              # 列出git index包含的文件
git show-branch                                           # 图示当前分支历史
git show-branch --all                                     # 图示所有分支历史
git whatchanged                                           # 显示提交历史对应的文件修改
git revert dfb02e6e4f2f7b573337763e5c0013802e392818       # 撤销提交dfb02e6e4f2f7b573337763e5c0013802e392818
git ls-tree HEAD                                          # 内部命令:显示某个git对象
git rev-parse v2.0                                        # 内部命令:显示某个ref对于的SHA1 HASH
git reflog                                                # 显示所有提交,包括孤立节点
git show HEAD@{5}
git show master@{yesterday}                               # 显示master分支昨天的状态
git log --pretty=format:'%h %s' --graph                   # 图示提交日志
git show HEAD~3
git show -s --pretty=raw 2be7fcb476
git stash                                                 # 暂存当前修改,将所有至为HEAD状态
git stash list                                            # 查看所有暂存
git stash show -p stash@{0}                               # 参考第一次暂存
git stash apply stash@{0}                                 # 应用第一次暂存
git grep "delete from"                                    # 文件中搜索文本“delete from”
git grep -e '#define' --and -e SORT_DIRENT
git gc
git fsck
Administrator@DESKTOP-3FEJFET MINGW64 ~/Desktop
$ git config --global  user.name  zhangsan

Administrator@DESKTOP-3FEJFET MINGW64 ~/Desktop
$ git config --global  user.email  邮箱地址

说明:
签名的作用是区分不同操作者身份。用户的签名信息在每一个版本的提交信息中能够看到,以此确认本次提交是谁做的。Git首次安装必须设置一下用户签名,否则无法提交代码。
※注意:
这里设置用户签名和将来登录GitHub(或其他代码托管中心)的账号没有任何关系。

初始化本地库

1)基本语法
Git init
2)案例实操

Administrator@DESKTOP-3FEJFET MINGW64 ~/Desktop
$ git init
Initialized empty Git repository in C:/Users/Administrator/Desktop/.git/

进行查看

$ ls -al
total 22
drwxr-xr-x 1 Administrator 197121 0 Sep 20 22:06 ./
drwxr-xr-x 1 Administrator 197121 0 Sep 20 22:01 ../
drwxr-xr-x 1 Administrator 197121 0 Sep 20 22:06 .git/

新增文件

Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ vim hello.txt
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ ll
total 1
-rw-r–r– 1 Administrator 197121 10 Sep 20 22:18 hello.txt

Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git add hello.txt #添加到暂存区
warning: in the working copy of ‘hello.txt’, LF will be replaced by CRLF the next time Git touches it

提交本地库

1)基本语法
git commit -m “日志信息””文件名
2)案例实操

Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git commit -m “my first commit” hello.txt
warning: in the working copy of ‘hello.txt’, LF will be replaced by CRLF the next time Git touches it
[master (root-commit) f13b3f1] my first commit
1 file changed, 1 insertion(+)
create mode 100644 hello.txt

查看日志

git reflog #精简的
git log #详细的

Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git reflog
f13b3f1 (HEAD -> master) HEAD@{0}: commit (initial): my first commit

修改文件

使用vim进行修改

查看状态
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git status
On branch master
Changes not staged for commit:
(use “git add
…” to update what will be committed)

(use “git restore

…” to discard changes in working directory)

modified: hello.txt #提示被修改,没添加到暂存区

no changes added to commit (use “git add” and/or “git commit -a”)

再次进行添加暂存区

Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git add hello.txt
warning: in the working copy of ‘hello.txt’, LF will be replaced by CRLF the next time Git touches it

再次上传本地库

Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git commit -m “my second commit” hello.txt
warning: in the working copy of ‘hello.txt’, LF will be replaced by CRLF the next time Git touches it
[master 50b9dfa] my second commit
1 file changed, 1 insertion(+), 1 deletion(-) #显示有一行被修改

查看日志

Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git reflog
50b9dfa (HEAD -> master) HEAD@{0}: commit: my second commit
f13b3f1 HEAD@{1}: commit (initial): my first commit

显示两个版本

历史版本

1)基本语法
1)基本语法-
git reflog查看版本信息
cgit log查看版本详细信息
2)案例实操

Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git reflog
50b9dfa (HEAD -> master) HEAD@{0}: commit: my second commit
f13b3f1 HEAD@{1}: commit (initial): my first commit

$ git log
commit 50b9dfa8cbafdc87c07c407f10c8b5e7adf7d2f3 (HEAD -> master)
Author: zhangsan 2253465337@qq.com
Date: Tue Sep 20 22:33:03 2022 +0800
my second commit
commit f13b3f1ba8f36d3fddd26a7214f3969480316ca8
Author: zhangsan 2253465337@qq.com
Date: Tue Sep 20 22:26:00 2022 +0800
my first commit

版本穿梭

git reset –hard 版本号
实例:

Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git reset –hard 50b9dfa #版本穿梭
HEAD is now at 50b9dfa my second commit
Administrator@DESKTOP-3FEJFET MINGW64 /e/GIT/Git files (master)
$ git reflog 信息查看
50b9dfa (HEAD -> master) HEAD@{0}: reset: moving to 50b9dfa
50b9dfa (HEAD -> master) HEAD@{1}: commit: my second commit
f13b3f1 HEAD@{2}: commit (initial): my first commit

原文地址:http://www.cnblogs.com/yutoujun/p/16915118.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性