git命令整理

git 命令

全局变量

  • git config --global user.name "jerry"
  • git config --global user.email "jerry@163.com"
  • git config --global color.ui "always"
  • 解决中文乱码问题

    • 解决bash控制台ls命令中文文件乱码:

      在Git\etc\git-completion.bash 文件中追加一行 alias ls="ls --show-control-chars --color=auto"

    • 解决bash控制台 git commit 无法输入中文注释:

      修改Git\etc\inputrc文件

      set output -meta on

      set convert -meta off

    • 解决git log命令中文注释乱码(只bash控制台好用):

      在Git\etc\profile文件中追加一行:export LESSCHARSET=iso8859

    • 解决gitk显示中文注释乱码

      git config --global i18n.commitencoding ISO-8859

      git config --global i18n.logoutputencoding ISO-8859

初始化新版本库

  • git init

    只会在根目录下创建一个名为.git文件夹

设置忽略的文件

  • 设置每个人都要忽略的文件

    • 在根目录新建一个名为.gitignore的文本文件

      在命令行执行 echo *.jpg>.gitignore (注意>号左右不要有空格)

       

    • 将.gitignore文件加入版本库并提交
  • 设置只有自己要忽略的文件

    修改.git/info/exclude文件,可使用正则表达式

添加新文件到版本库

  • 添加单个文件

    git add somefile.txt

  • 添加所有txt文件

    git add *.txt

  • 添加所有文件

    git add

提交

  • git commit -m "add all txt file"

提交

  • 提交所有修改

    git commit -m "some msg" -a

  • 提交单个文件

    git commit -m "some msg" one.txt

  • 增补提交

    git commit -C head -a --amend

撤销修改

  • 撤销尚未提交的修改

    • 撤销1、2个文件

      git checkout head readme.txt foto.txt

    • 撤销所有txt文件

      git checkout head *.txt

    • 撤销所有文件

      git checkout head

  • 撤销提交

    • 反转提交。反转提交但不提交。

      例:反转最近一次提交:git revert --no -commit head

      *相当于提交最近一次提交的反操作

    • 复位

      • 取消暂存

        git reset 或 fit reset head &lt filename &gt

      • 复位到head之前的那个版本

        git reset -head head^^

        不会在版本库中留下痕迹。使用^^或^

分支

  • 列出本地分支

    git branch

  • 列出所有分支

    git branch -a

  • 基于当前分支的末梢创建新分支

    git branch &lt branchname &gt

  • 基于某次提交、分支或标签创建新分支

    • git branch emputy bfe57de0

      *用来查看某个历史断面

    • git branch emputy2 emputy
  • 合并分支

    • 普通合并

      • 合并并提交

        git merge &lt branchname &gt

        *如果发生了冲突,就不会自动提交,如果冲突不多,不想立即解决他们,可以直接使用git checkout head 撤销

      • 合并但不提交

        git merger --no-commit

        实测结果,--no-commit 参数没有生效

    • 压合合并

      • 压合合并后直接提交

        git merge --squash &lt branchname &gt

      • 合合并后但不提交

        git merger --squash --no-commit

        *两个人合作开发一个新功能时,需要在一个分支上提交多次,开发完成之后在压合成一次提交

    • 拣选合并

      • 挑选某次提交合并但不提交

        git cherry-pick --no-commit 5b54b4

        *但是要合并的提交只要比当前高2个版本,就会出现奇怪的冲突问题

  • 重命名分支

    • git branch -m &lt branchname &gt &lt newname &gt

      不会覆盖已存在的同名分支

    • git branch -M &lt branchname &gt &lt newname &gt

      会覆盖已存在的同名分支

  • 删除分支

    • git branch -d &lt branchname &gt

      如果分支没有被合并会删除失败

    • git branch -D &lt branchname &gt

      即使分支没有被合并会删除

解决冲突

  • 冲突很少时

    直接编辑有冲突的文件然后提交即可

  • 冲突比较复杂时

    git merge tool

    • 会生成.BACKUP, .BASE, .LOCAL和.REMOTE四个文件
    • 然后自动调用冲突解决工具
    • 解决之后手动删除.orig文件(冲突解决之前的文件备份)
    • 提交

标签

  • 创建标签

    • 为当前分支最近一次提交创建标签

      git tag 1.0

      *标签没有重命名

    • 为A分支最近一次提交创建标签

      git tag A_1.0 A

      *也可以把标签命名为A/1.0

    • 为某次历史提交创建标签

      git tag 1.0 4ed54x21

  • 显示标签列表

    git tag

  • 检出标签

    git checkout 1.0

    查看标签断面很方便的方法,但是不能提交

  • 由标签创建分支

    • git branch b1.1.1.1
    • git checkout -b b1.1.1.1
  • 删除标签

    git tag -d 1.0

查看状态

  • 当前状态

    git status

  • 历史记录

    • git log
    • gitk

      • gitk

        查看当前分支历史记录

      • gitk &lt branchname &gt

        查看某分支历史记录

      • gitk -all

        查看所有分支

  • 每个分支最后的提交

    git branch -v

其他

导出版本库

  • git archive --format=zip head>nb.zip
  • git archive --format=zip --prefix=nb1.0/ head>nb.zip
posted @ 2013-05-29 18:31  库乐君  阅读(286)  评论(0)    收藏  举报