初始化版本命令: git init
换行符处理
git config --global core.autocrlf true
git config --global core.autocrlf input
git config --global core.autocrlf false
git config --global core.safecrlf true
git config --global core.safecrlf false
git config --global core.safecrlf warn
设置忽略的名字:
- 设置所有人忽略的文件:
- 在根目录新建
.gitignore
文件,命令行执行echo *.jpg>.gitignore
// >左右无空格 - 将文件提交值版本库参与管理
- 设置只需要自己忽略的文件:
- 修改
.git/info/exclude
文件,使用正则表达式
添加文件到版本库:
- 添加单个文件:
git add some fileName.txt
- 添加所有文件:
git add *.txt
- 添加所有txt文件:
git add .
添加新文件到版本库
- 添加单个文件
git add somefile.txt;
- 添加所有文件:
git add .
- 添加所有txt文件:
git add *.txt;
日常操作
提交:git commit -m "some Msg";
- 提交所有修改:
git commit -m "some Msg" -a;
- 提交单个文件:
git commit -m "add one txt" readme.txt;
- 增补提交:
git commit -C head -a-ament;
//不会产生新的提交记录
撤销修改
- 撤销尚未提交的修改
- 撤销1,2个文件:
git checkout head readme.txt todo.txt;
- 撤销是所有txt文件:
git checkout head *.txt;
- 撤销所有文件:
git checkout head;
- 撤销提交修改
- 反转最近一次提交:
git revert --no commit head;
// 相当于提交最近一次提交的反操作 - 复位_取消暂存:
git reset head;
/git reset head <fileName>;
- 复位_复位到head之前版本:
git reset --head head^^;
// 不会再版本库中留下痕迹
分支
- 列出本地分支:
git branch;
- 列出所有分支:
git branch -a;
- 基于当前分支末梢,创建新分支:
git branch <branchName>;
- 检出分支:
git checkout <branchName>;
- 基于当前分支末梢,创建新分支并检出分支:
git checkout -b <branchName>;
- 基于某次提交,分支或标签创建分支:
git branch empty bfe570e5;
::git branch empty2 empty;
// 多用于查看历史断面 - 合并分支
- 普通合并_合并并提交:
git merge <branchName>;
// 如果冲突,不提交,若撤销git checkout head;
- 普通合并_合并但不提交:
git merge --no-commit;
// 有时会失效 - 压缩合并_并提交:
git merge --squash <branchName>;
- 压缩合并_但不提交:
git merge --squash --no-commit <branchName>;
// 当不同人,在一个分支上提交很多次,开发完成后,可以压缩合成为一个提交 - 拣选合并:
git cherry-pick --no-commit sdf45s8wd;
// 当要合并的提交比当前提交高两个以上版本,会冲突
- 重命名分支:
git branch -m <oldBranchName> <newBranchName>;
// 不会覆盖已存在同名分支 - 重命名分支:
git branch -M <oldBranchName> <newBranchName>;
// 会覆盖已存在同名分支 - 删除分支:
git branch -d delBranchName;
// 如果分支没合并,删除失败 - 删除分支:
git branch -D delBranchName;
// 强制删除分支
解决冲突
- 冲突很少时: 直接编辑冲突文件后,提交返回
- 冲突很多时:
git merge tool;
//
- 会生成4个文件(backup base local memote):
- 自动调用冲突解决工具
- 解决后,手动删除
.orig
文件(冲突解决前,文件备份) - 提交
标签
- 为当前分支最近一次提交创建分支
git tag 1.0;
// 标签无法重命名 - 为Contacts分支最近一次提交创建标签:
git tag constacts/1.0.1 constacts;
- 为某次历史提交创建标签:
git tag 1.0.2 s89sdf4d;
- 显示标签列表:
git tag;
- 检出标签:
git checkout 1.0.1;
// 查看标签断面,但不能提交 - 由标签创建分支:
git branch b1.1 1.0.1;
- 删除标签:
git tag -d 1.0.1;
查看状态
git log;
gitk;
// 查看当前分支历史记录gitk <branchName>;
//查看某一分支历史记录gitk --all;
// 查看所有分支git branch -v;
// 每个分支上最后一次提交
git archive --formate=zip head>name.zip;
// 导出版本库git archive --formate=zip --prefix=nb1.00/head>nb.zip;
Remote
初始化
- 克隆版本库:
git clone <url>;
克隆后记得加四个配置:
remote.origin.fetch=+refs/heads/............;
remote.origin.url = d:\developmentSoftware.......;
branch.master.remote=origin;
branch.master.merge=refs/heads/master;
- 别名: 添加远程版本库别名:
git remote add <别名> <远程库URL>;
添加别名后记得加两个config:
remote.origin.url=d:\developmentSoftware.......;
remote.origin,fetch=+refs/heads/*:refs/remotes/origin/*;
- 删除别名及相关分支:
git remote rm <别名>;
- 创建一个无本地分支的库:
git init -bare;
//当需要一个中央仓库时,可以如此建立
日常操作
- 列出远程分支:
git branch -r;
- 删除远程库中已不存在的分支:
git remote prune origin;
- 获取但不合并:
git fetch <远程版本库>;
// 如 git fetch origin;
或git fetch d:developmentSoftware;
- 获取合并到当前本地分支:
git pull;
// 等价于git pull origin
需要配置以下内容:
branch.master.remote=origin
和branch.master.merge=refs/heads/master;
- 推入远程库:
git push origin master;
github
- 启动git bash控制台;
- 如果以前生成过SSH Key,需先备份
cd ~/.ssh; mkdir key_backup; cp id_rsa* key_backup;
// ~/.ssh就是c:\users\xxxxx\.ssh;
- 生成SSH Key:
ssh-keygen -t rsa -C "xxxxxxx@qq.com";
// 生成好的SSH Key放到.ssh文件种 - 将SSH Key 添加到GitHub账户中: 点击链接,将key放到文本框中:
Account Settings | SSH Public Keys | Add another public key
- 第一次连接gitHub
ssh git@github.com;
git全局配置
git config --global user.name "zhangsan";
git config --global user.email "xxxx@qq.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-mete on;
与set convert-meta off;
- 解决git log命令中文注释乱码: 在
git\etc\profile
中追加一行export LESSCHARSET=utf8
- 解决gitk显示中文乱码:
git config --global i18n.commitencoding utf-8;
与git config --global i18n.logoutencoding utf-8;
即可。