git命令复习

git 命令复习

参考链接:Git Cheat Sheet 中文版

配置

命令 说明
git config --list 列出当前配置
git config --local --list 列出当前仓库配置
git config --global --list 列出全局配置
git config --system --list 列出系统配置
git config --global user.name "zyj" 设置用户名
git config --global user.email 设置用户的邮箱

配置文件

文件地址 说明
<repo>/.git/config Repository配置对应的配置文件路径[–local]:
~/.gitconfig 用户全局配置对应的配置文件路径[–global]:
/etc/gitconfig 系统配置对应的配置文件路径[–local]:(不一定存在)

创建

复制一个已创建的仓库:
1
2
3
4
# 通过ssh
$ git clone ssh://user@domain.com/repo.git
# 通过HTTP
$ git clone http://domain.com/user/repo.git
创建一个本地新仓库
1
git init

本地修改

命令 说明
git status 显示工作路径下已修改的文件
git diff 显示与上次提交版本的不同
git add . 把当前所有修改添加到下次提交中
git add -p <file> 把对某个文件的修改添加到下次提交中
git commit -a 提交本地的所有修改
git commit 提交之前已标记的变化
git commit -m 'message here' 提交之前已标记的变化并附加消息提交
git commit --date="date –date=’n day ago’" -am "Commit Message" 提交,并将提交时间设置为之前的某个日期

修改上次提交

命令 说明
git commit --amend 修改上次提交,既可以对上次提交的内容进行修改,也可以修改提交说明
git commit --amend --no-edit 编辑器会弹出上一次提交的信息,加入–no-edit标记会修复提交但不修改提交信息
git commit --amend --author="username <email>" 修改用户信息
git push --force-with-lease origin master 修改提交强制同步到远程

保存当前修改,并返回上一次干净的仓库

命令 说明
git stash,git stash save 保存当前的修改
git stash list 暂存的内容列表
git stash apply 重新存储最后一次缓存的内容,当前的修改保存到当前分支,缓存标签仍然存在于 git stash list列表中
git stash apply 0 重新存储编号为0的内容,当前的修改保存到当前分支,缓存标签仍然存在于 git stash list列表中
git stash pop 重新存储最后一次缓存的内容,当前的修改保存到当前分支,删除git stash list 列表中的存储编号
git stash pop 1 重新存储编号为0的缓存的内容,当前的修改保存到当前分支,删除git stash list 列表中的存储编号
git stash show [stash] 显示stash和上一个提交点的差异
git stash drop [0] 删除编号为0的暂存
git stash clear 清空所有的暂存列表

搜索

命令 说明
git grep "hello" 从当前目录的所有文件中查找文本内容
git grep "hello" v2.5 从某一版本中搜索文本

提交历史

命令 说明
git log 显示所有的提交记录,(显示hash,作者信息,提交的标题和时间)
git log --oneline
git log --author="zyj"
git log -p <file> 显示某个文件的所有修改
git log origin/master --left-right 显示远端的提交历史
git blame <filename> 查看某个文件的修改历史
git blame -L <start>,<end> <filename> 查看某个文件的修改历史,并指定开始行和结束行
git reflog show 显示所有分支的(包括远端)的提交历史
git reflog delete 删除reflog

分支与标签

命令 说明
git branch 列出所有的分支
git branch -r 列出所有的远端分支
git checkout <branch> 切换分支
git checkout -b <branch> 创建并切换到新分支
git branch <new-branch> 基于当前分支创建新分支
git branch --track <new-branch> <remote-branch> 基于远程分支创建新的可追溯的分支
git branch -d <branch> 删除本地分支
git branch -D <branch> 强制删除一个分支
git tag <tag_name> 给当前版本打标签,没有说明
git tag -a <tag_name> 给当前版本打标签,并附上说明
git tag -l 查看所有的标签列表
git tag -d <tag_name> 删除一个本地标签
git push origin :refs/tags/<tagname> 删除一个远端标签
git push origin v1.0 发布一个标签到origin
git push --tags 发布本地所有的标签到远端

更新与发布

  1. git remote -v 显示所有的远端分支

    1
    2
    3
    $ git remote -v
    origin git@gitee.com:yajun0310/blog.git (fetch)
    origin git@gitee.com:yajun0310/blog.git (push)
  2. git remote show <remote>显示远端的详细信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    $ git remote show origin
    * remote origin
    Fetch URL: git@gitee.com:yajun0310/blog.git
    Push URL: git@gitee.com:yajun0310/blog.git
    HEAD branch: master
    Remote branches:
    develop tracked
    master tracked
    Local branches configured for 'git pull':
    develop merges with remote develop
    master merges with remote master
    Local refs configured for 'git push':
    develop pushes to develop (up to date)
    master pushes to master (up to date)
  3. git remote add <remote> <url>添加新的远端

    1
    git remote add origin2git@gitee.com:yajun0310/blog.git
  4. git fetch <remote>下载远程端版本,但不合并到head中

    1
    2
    3
    $ git fetch origin master
    From gitee.com:yajun0310/blog
    * branch master -> FETCH_HEAD
  5. git remote pull <remote> <url>下载远程版本,并自动与head版本合并

  6. git pull origin master将远程端版本合并到本地版本中

    1
    2
    3
    4
    $ git pull origin master
    From gitee.com:yajun0310/blog
    * branch master -> FETCH_HEAD
    Already up to date.
  7. git pull --rebase <remote> <branch>以rebase方式将远程分支与本地分支合并

  8. git push <remote> <branch>推送到远程branch分支,不存在branch分支会自动新建branch分支

  9. git push -u <remote> <branch>操作同上,不同的是会自动关联上远程,git push 操作默认推送branch分支

  10. 删除远程分支since:1.5git push <remote> :<branch>since1.7git push <remote> --delete <branch>

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    ** since1.5.0 **
    $ git push origin :test
    remote: Powered by Gitee.com
    To gitee.com:yajun0310/blog.git
    - [deleted] test

    **since 1.7.0**
    $ git push origin --delete test
    remote: Powered by Gitee.com
    To gitee.com:yajun0310/blog.git
    - [deleted] test

合并与重置

  1. git merge <branch> 将分支合并到当前分支中
  2. git rebase <branch>将分支合并到当前分支(衍合)
  3. git rebase -i <branch> 交互式衍合
  4. git rebase --continue 衍合继续
  5. git rebase --abort取消衍合
  6. git mergetool 使用配置好的merge tool 解决冲突
  7. 在编辑器手动解决冲突后。标记为已解决
    1
    2
    git add <resolved-file>
    git rm <resolved-file>

撤销

  1. git reset --hard HEAD 放弃工作目录下的所有修改
  2. git reset HEAD 移除缓存区的所有文件(撤销上次 git add)
  3. git checkout HEAD <file>放弃某个文件的所有本地修改
  4. git reset --hard <commit>将head重置到指定的版本,并抛弃到版本之后的所有修改
  5. git revert <commit>重置一个提交,(通过创建一个截然不同的新提交)
  6. git reset --hard origin/master 用远端分支强制覆盖本地分支
  7. git reset <commit> 将head重置到某一次提交,并将之后的修改标记为未添加到的缓冲区的修改
  8. git reset --keep <commit>将HEAD重制到某一次提交,并保留未提交的本地修改
  9. 删除添加.gitignore文件前错误提交的文件
    1
    2
    3
    git rm -r --cached .
    git add .
    git commit -m "remove aa file"