Git常用总结

初始化版本命令: git init
换行符处理
# 提交时转换为 LF,检出时根据当前系统决定转换为 CRLF,或者不转换
git config --global core.autocrlf true

# 提交时转换为 LF,检出时不转换
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
设置忽略的名字:
  1. 设置所有人忽略的文件:
  • 在根目录新建.gitignore文件,命令行执行echo *.jpg>.gitignore// >左右无空格
  • 将文件提交值版本库参与管理
  1. 设置只需要自己忽略的文件:
  • 修改.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. 撤销尚未提交的修改
  • 撤销1,2个文件: git checkout head readme.txt todo.txt;
  • 撤销是所有txt文件: git checkout head *.txt;
  • 撤销所有文件: git checkout head;
  1. 撤销提交修改
  • 反转最近一次提交: 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; // 多用于查看历史断面
  • 合并分支
  1. 普通合并_合并并提交: git merge <branchName>;// 如果冲突,不提交,若撤销git checkout head;
  2. 普通合并_合并但不提交: git merge --no-commit;// 有时会失效
  3. 压缩合并_并提交: git merge --squash <branchName>;
  4. 压缩合并_但不提交: git merge --squash --no-commit <branchName>;// 当不同人,在一个分支上提交很多次,开发完成后,可以压缩合成为一个提交
  5. 拣选合并: 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; //
  1. 会生成4个文件(backup base local memote):
  2. 自动调用冲突解决工具
  3. 解决后,手动删除.orig文件(冲突解决前,文件备份)
  4. 提交
标签
  • 创建标签:
  1. 为当前分支最近一次提交创建分支 git tag 1.0; // 标签无法重命名
  2. 为Contacts分支最近一次提交创建标签: git tag constacts/1.0.1 constacts;
  3. 为某次历史提交创建标签: 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 status;
  • 历史记录:
  1. git log;
  2. gitk; // 查看当前分支历史记录
  3. gitk <branchName>; //查看某一分支历史记录
  4. gitk --all; // 查看所有分支
  5. git branch -v; // 每个分支上最后一次提交
  • 其他: 导出版本库
  1. git archive --formate=zip head>name.zip; // 导出版本库
  2. git archive --formate=zip --prefix=nb1.00/head>nb.zip;

Remote

初始化
  • 克隆版本库: git clone <url>;克隆后记得加四个配置:
  1. remote.origin.fetch=+refs/heads/............;
  2. remote.origin.url = d:\developmentSoftware.......;
  3. branch.master.remote=origin;
  4. branch.master.merge=refs/heads/master;
  • 别名: 添加远程版本库别名: git remote add <别名> <远程库URL>;添加别名后记得加两个config:
  1. remote.origin.url=d:\developmentSoftware.......;
  2. remote.origin,fetch=+refs/heads/*:refs/remotes/origin/*;
  • 删除别名及相关分支: git remote rm <别名>;
  • 创建一个无本地分支的库: git init -bare; //当需要一个中央仓库时,可以如此建立
日常操作
  • 分支
  1. 列出远程分支: git branch -r;
  2. 删除远程库中已不存在的分支: git remote prune origin;
  • 从远程库中获取
  1. 获取但不合并: git fetch <远程版本库>; // 如 git fetch origin;git fetch d:developmentSoftware;
  2. 获取合并到当前本地分支: git pull; // 等价于git pull origin需要配置以下内容:
  • branch.master.remote=originbranch.master.merge=refs/heads/master;
  • 推入远程库:git push origin master;
github
  • 生成ssh Key
  1. 启动git bash控制台;
  2. 如果以前生成过SSH Key,需先备份cd ~/.ssh; mkdir key_backup; cp id_rsa* key_backup; // ~/.ssh就是c:\users\xxxxx\.ssh;
  3. 生成SSH Key: ssh-keygen -t rsa -C "xxxxxxx@qq.com"; // 生成好的SSH Key放到.ssh文件种
  4. 将SSH Key 添加到GitHub账户中: 点击链接,将key放到文本框中:Account Settings | SSH Public Keys | Add another public key
  5. 第一次连接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";
解决中文乱码问题
  1. 解决bash控制台ls命令中文乱码: 在git\etc\git-completion.bash文件中追加一行alias ls='ls --show-control-chars --color=auto';
  2. 解决bash控制台git commit无法输入中文注释: 修改git\etc\inputrc文件,set output-mete on;set convert-meta off;
  3. 解决git log命令中文注释乱码: 在git\etc\profile中追加一行export LESSCHARSET=utf8
  4. 解决gitk显示中文乱码: git config --global i18n.commitencoding utf-8;git config --global i18n.logoutencoding utf-8;即可。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值