GIT 常用命令

来源:互联网 发布:淘宝迷你型三辊研磨机 编辑:程序博客网 时间:2024/05/21 10:15

git

初始配置

  • git --version 查看git版本
  • vim .gitignore 忽略特殊文件 与.git目录同级目录
  • git config --global user.name "John Doe" 提交名字
  • git config --global user.email john@example.com 提交邮箱
  • git config --global color.ui true 开启颜色显示

提交

  • git init 创建版本库
  • git add welcome.txt 将文件添加到暂存区
  • git commit -m "initialized" 将暂存区的数据提交到版本库
  • git commit --amend -m "reCommit" 重新提交

     //提交多行log git commit -m ' 1. log1 2. log2 3. log3 '
  • git reset HEAD benchmarks.rb 取消暂存区中的文件

  • git checkout -- benchmarks.rb 撤销对工作区文件的修改 若修改后没有放到暂存区则与上个版本库一致 若修改后放到了暂存区则和暂存区一致

  • git status 查看文件的当前状态

  • git rm readme.txt 不再纳入版本管理 删除本地文件
  • git rm --cached readme.txt 不再纳入版本管理 但本地文件不删除 移除跟踪但不删除文件

回退版本

  • git reflog 记录回退的每一次命令
  • git reset --hard HEAD^ 回退到上一个版本
  • git reset --hard 哈希值 根据哈希值回退到某个版本

查看提交记录

  • git log 查看提交历史记录 git log命令显示从最近到最远的提交日志
  • git log --pretty=oneline 将每个提交放在一行显示 oneline,short,full 和 fuller

分支

  • git branch 查看分支 当前分支前面会标一个*号
  • git branch master1 创建master1分支
  • git checkout master1 切换到master1分支
  • git checkout -b master1 创建并切换到master1分支
  • git checkout -f master 强制切换到master分支
  • git branch -D master1 删除master1分支
  • git branch -D feature-vulcan 删除还没有合并的分支
    *
  • git add -u

    • git merge master1 将master1分支合并到当前分支
  • git log --graph --pretty=oneline --abbrev-commit 查看分支合并情况

  • git branch --merged 查看已合并到当前分支的分支 上游分支

  • git branch --no-merged 查看尚未合并的分支

比较文件差异 使用默认设置

  • git diff 比较工作区与暂存区的差异
  • git diff HEAD 比较工作区和当前工作分支版本库的差异
  • git diff HEAD -- path 比较工作区和当前工作分支版本库同一目录的差异
  • git diff --cached (或--staged) 比较暂存区和版本库中文件的差异
  • git diff HEAD:xx.cpp HEAD^yy.cpp 比较不同版本库中不同文件的差异

    • git log -p -2 行方面对比 每次提交的差异
  • git log -U1 --word-diff 单词方面对比 每次提交的差异

    我们常用 -p 选项展开显示每次提交的内容差异,用 -2 则仅显示最近的两次更新

  • git log --stat 简要的增改行数统计

标签

  • git tag v1.0 创建标签
  • git tag 查看标签
  • git tag v0.9 6224937 根据哈希值打标签
  • git tag -d publish/1.0.0 删除本地tag

远程仓库

  • git remote 查看远程仓库信息
  • git clone [url] 克隆代码库
  • git remote add [shorname] [url] 添加远程仓库
  • git fetch [shorname] 将远程仓库抓取到本地

分支更新

  • git pull 从远程仓库更新当前分支
  • git pull [shorname] master 获取并合并远程分支到本地master分支

分支推送

  • git push 向远程仓库推送当前分支
  • git push -f 向远程仓库强制推送当前分支
  • git push [shorname] master 向远程仓库推送master分支

分支合并

  • git merge origin daily 将本地分支与远程分支合并
  • git pull origin daily 获取并合并远程分支到本地分支
  • git mergetool 使用工具比较查看冲突

标签管理

  • git push origin --delete tag publish 删除远程tag
  • git push -u origin publish/3.0.0 发布tag

工作现场

  • git stash 保存工作现场
  • git stash list 恢复工作现场

杂项

  • git diff --check 检查多余的白字符
  • git status 查看文件提交情况
  • git mergetool 查看冲突

修改默认的文件对比 文件合并工具

  • git difftool –tool-help 查看有哪些对比工具可以设置命令
  • 修改config文件

    [diff]    tool = bc3[difftool "bc3"]    path = d:/program files/beyond compare 3/bcomp.exe[merge]    tool = bc3[mergetool "bc3"]    path = d:/program files/beyond compare 3/bcomp.exe
  • git difftool HEAD HEAD^ 比较文件差异
  • git mergetool 解决冲突

解决换行符问题

  • git config --global core.autocrlf true Windows
  • git config --global core.autocrlf input Linux or Mac

修改默认编辑器

  • git config core.editor notepad

远程仓库

分支的提交与拉取

  • git push origin newBranch 将本地分支提交到远程仓库
  • git pull origin newBranch 将远程仓库分支拉取到本地

将共享库添加到自己的仓库

    git remote add gitignore https://github.com/github/gitignore    git pull origin gitignore

将最近两次提交合并为一个提交 并强制提交到远程仓库

    git reset --soft HEAD^^    git commit -m "....."    git push --force

添加自己的远程仓库 修改.git/config

  • 为master分支添加远程仓库(master分支)
  • 修改config文件

    [remote "origin"]    url = https://git...    fetch = +refs/heads/*:refs/remotes/origin/*[branch "master"]    remote = origin    merge = refs/heads/master

开源中国

 http://www.oschina.net/p/git 

git教程

 http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013743858312764dca7ad6d0754f76aa562e3789478044000 https://github.com/progit/progit/tree/master/zh

我的头像

更新地址

原创粉丝点击