Table of Contents

Git 常用命令


收集常用Git命令。

存取操作命令


git clone  git://192.168.0.1/xinsi/repo.git
git clone -b dev http://192.168.0.1/xinsi/repo.git
git add -u
git commit -m "Some message"
git push
git pull

分支(branch)操作命令


git branch
git branch -a
git branch -v
git branch -r
git checkout -b dev
git branch --set-upstream-to origin/远程分支名  本地分支名
git branch --set-upstream-to=origin/<branch> you_branch
git branch --set-upstream-to origin/远程分支名  本地分支名
git branch -m old_branch new_branch
git checkout dev
git push --set-upstream origin dev
git branch -d <BranchName>
git push origin --delete dev
git checkout master
git merge dev
git checkout -b 本地分支名 origin/远程分支名

创建新分支

git checkout -b dev    # Create local branch "dev"
git push origin dev  # Push branch "dev" to remote

检出远程分支

git branch -a     # List all branchs on local and remote
git checkout dev  # Checkout branch "dev"

放弃本地修改


git checkout .
git stash
git stash pop
git reset --hard HASH
git reset --soft HASH
git fetch --all
git reset --hard origin/master
其中最后的master可以修改为其他branch

撤消操作


git commit -m 'initial commit'
git add forgotten_file
git commit --amend
// files
git reset HEAD <filename>

// all
git reset --hard
git pull

提交历史


git log

存储密码


清除密码

git config --system --unset credential.helper
or
git config --global credential.helper reset

保存用户名和密码

git config --global credential.helper store

设置默认用户名称和邮箱

git config --global user.name "your name"
git config --global user.email "hour mail box"

代码打包


git archive -o ../xxx.zip HEAD

显示中文


git config --global core.quotepath false

在 git log 时中文依然不能显示,首先试试用

git --no-pager log 

能不能显示中文,如果可以,则设置pager为more:

git config --global core.pager more 

以及,其他的一些解决办法: 进入你的项目根目录

1.设置git gui的界面编码

git config --global gui.encoding utf-8

2.设置 commit log 提交时使用 utf-8 编码,可避免服务器上乱码,同时与linux上的提交保持一致!

git config --global i18n.commitencoding utf-8
git config --global i18n.logoutputencoding utf-8

注:

windows系统默认编码为gbk,可改成gbk

如果系统设置了:

export LANG=zh_CN.UTF-8

则日志输出编码设置为utf-8

git config --global i18n.logoutputencoding utf-8

3.在 /etc/profile 中添加:

export LESSCHARSET=utf-8

在试一下问题解决了!

✔ 强制覆盖本地文件


git fetch --all

git reset --hard origin/master          # 如果在主分支上
or
git reset --hard origin/<branch_name>   # 如果在非主分支上

说明:
git fetch从远程下载最新的,而不尝试合并或rebase任何东西。
然后git reset将主分支重置为您刚刚获取的内容。 --hard选项更改工作树中的所有文件以匹配origin/master中的文件。

标签

列出标签
git tag
新建标签
git tag 标签名 -m "信息"

✔ 创建新分支

git branch -b dev    # Create local branch "dev"
git push origin dev  # Push branch "dev" to remote

✔检出远程分支

git checkout dev

✔修改Commit信息

commit后未push,修改信息命令:
git commit --amend

KEY

ssh-keygen -t rsa -C "youremail@example.com"

关掉自动转换功能

针对警告:
warning: LF will be replaced by CRLF in xxxxxxxx.
The file will have its original line endings in your working directory.

git config core.autocrlf false (仅对当前git仓库有效)
git config --global core.autocrlf false (全局有效)

临时保存

备份当前的工作区的内容
git stash save "comment"

查看临时列表
git stash list

查看修改
git stash show stash{0}

清除所有缓存的stash
git stash clear

CRLF与LF的转换

提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true   

提交时转换为LF,检出时不转换
git config --global core.autocrlf input   

提交检出均不转换
git config --global core.autocrlf false

临时保存和恢复工作区

git stash

git stash list
git stash pop = git stash apply + git stash drop

1. sub module

1.1 相关文件

记录所有子模块的文件

[submodule "app/pm2"]
        path = app/pm2
        url = git@codeup.aliyun.com:xs/webapp/app.pm2.git

[submodule “apps/pm2”]

        url = git@codeup.aliyun.com:xs/webapp/app.pm2.git
        active = true
        
 * .git/modules/<color #ed1c24>app/pm2</color>
 子模块仓库的副本,存放子模块仓库本身的.git目录文件

指向主仓库记录子模块的路径

gitdir: ../../.git/modules/<color #ed1c24>app/pm2</color>

1.2 查看所有子模块

git submodule

1.3 克隆一个带子模块的仓库

  1. 下载主项目: git clone <remote-URL>
  2. git submodule init
  3. git submodule update
  4. 更新子模块: (在子模块目录) git pull origin master

注:

1.4 主仓库添加子模块(已存在的git仓库)

git sumodule add <remote-URL> [name]
git add name
git commit -m "add submodule"
git push origin master

1.5 主仓库添加子模块(将已有的目录设为子模块)

git checkout v2.0
cd sub-module
git init
git add sub.txt
git commit -m "version 1.0 of sub-module"
git remote add origin https://github.com/bitmingw/sub-module.git
git push -u origin master
cd .. # main-module/
git rm -r sub-module
git commit -m "remove sub-module directory"
main-module/
git submodule add https://github.com/bitmingw/sub-module.git
git commit -m "add submodule version 1.0"

1.6 删除子模块

 git submodule deinit <Submodule>
git rm --cached <Submodule>
git commit -am "Remove a submodule." 

1.7 向下同步子模块仓库

git submodule init
git submodule update

git submodule update --init --recursive