git常用命令小结
阅读原文时间:2023年07月08日阅读:1

  1. ssh连接方式 公钥生成
    ssh-keygen -t rsa -C "764432054@qq.com"
    在用户家目录下的.ssh目录下生成 id_rsa ,id_rsa.pub 把公钥文件(id_rsa.pub)内容加到github里的sshkey里

  2. 配置

git config --global user.email "you@example.com"
git config --global user.name "Your Name"
获取身份信息
git config --global --get user.email

warning: LF will be replaced by CRLF | fatal: CRLF would be replaced by LF
遇到这两个错误,是因为Git的换行符检查功能。
core.safecrlf
Git提供了一个换行符检查功能(core.safecrlf),可以在提交时检查文件是否混用了不同风格的换行符
这个功能的选项如下:
false - 不做任何检查
warn - 在提交时检查并警告
true - 在提交时检查,如果发现混用则拒绝提交
假如你正在Windows上写程序,又或者你正在和其他人合作,他们在Windows上编程,而你却在其他系统上,在这些情况下,你可能会遇到行尾结束符问题.
这是因为Windows使用回车和换行两个字符来结束一行,而Mac和Linux只使用换行一个字符。虽然这是小问题,但它会极大地扰乱跨平台协作。

Git可以在你提交时自动地把行结束符CRLF转换成LF,而在签出代码时把LF转换成CRLF。
用core.autocrlf来打开此项功能,如果是在Windows系统上,把它设置成true,这样当签出代码时,LF会被转换成CRLF:

$ git config --global core.autocrlf true
Linux或Mac系统使用LF作为行结束符,因此你不想 Git 在签出文件时进行自动的转换;
当一个以CRLF为行结束符的文件不小心被引入时你肯定想进行修正,把core.autocrlf设置成input来告诉 Git 在提交时把CRLF转换成LF,签出时不转换:

$ git config --global core.autocrlf input
这样会在Windows系统上的签出文件中保留CRLF,会在Mac和Linux系统上,包括仓库中保留LF。

如果你是Windows程序员,且正在开发仅运行在Windows上的项目,可以设置false取消此功能,把回车符记录在库中:

$ git config --global core.autocrlf false


  1. 设置发布模式

git config --global push.default simple/matching
Matching
'matching'参数是 Git .x 的默认行为,其意是如果你执行 git push 但没有指定分支,它将 push 所有你本地的分支到远程仓库中对应匹配的分支。
Simple
而 Git .x 默认的是 simple,意味着执行 git push 没有指定分支时,只有当前分支会被 push 到你使用 git pull 获取的代码。


  1. 一些常用操作
    git add filename git add . 表示当前目录
    git commit -m '注释' -o filename 
    git push

git checkout -- …" 丢弃工作区的改动(工作目录里文件变动了,还未add 撤销修改) 用版本库里的版本替换工作区的版本
git reset HEAD …" 撤出暂存区(add 了,但还未commit文件,抵消已经add的操作)

git rm file (如果工作区或者暂存区有修改,会报err,一个文件在版本库里,在工作区做了修改(未add到暂存区或者已经add到暂存区),直接git rm 不允许)
git commit -m 'delete file' -o file

git rm用于删除一个文件,如果一个文件已经被提交到版本库,不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容

也可以这样删除
rm file
git add file
git commit -m 'delete file' -o file

git reflog
查看所有历史 包括回退历史 
git log只是查看 从开始到现在的版本历史(不包括回退的) $ git log --pretty=oneline 
git reset --hard c5dd3a558005fb12f8 回退到某一版本


  1. 分支操作
    查看分支
    git branch -a(所有分支包括远程分支)
    git checkout -b dev
    创建并切换到dev分支
    相当于
    git branch dev
    git checkout dev

在dev分支做了修改后
切换到 master 分支
git checkout dev

把线上的分支同步到本地(本地没此分支) 
比如远程有分支 b1 本地没有
git checkout -b origin/b1 同步远程分支到本地

git checkout -b 本地分支名 origin/远程分支名  (本地该分支名分支必须不存在时)

git merge dev 合并dev到当前分支
注意--no-ff参数,表示禁用Fast forward:
git merge --no-ff -m "merge with no-ff" dev
合并后的历史有分支,能看出来曾经做过合并,而fast forward合并就看不出来曾经做过合并

本地分支重命名 git branch -m oldbranchname newbranchname

远程分支重命名 (假设本地分支和远程对应分支名称相同)

a. 重命名远程分支对应的本地分支

git branch -m old-local-branch-name new-local-branch-name

b. 删除远程分支

git push origin :old-local-branch-name

c. 上传新命名的本地分支

git push origin new-local-branch-name: new-local-branch-name

查看分支合并情况
git log --graph --pretty=oneline 提交的完整编号
操作的简易编号显示模式
git log --graph --pretty=oneline --abbrev-commit

临时要修改Bug时,为保证当前工作区看上去是干净的
把当前未完成工作现场储藏起来
git stash
查看储藏的工作区
git stash list
stash@{0}: WIP on b1: 38b2a71 modify p.php

恢复储藏的工作区 
git stash apply stash@{0} (恢复后stash里的内容并没有删除) 需要用 git stash drop 来删除
git stash pop (恢复储藏的工作区,并删除stash里的内容)

6.列出对应的远程库
git remote -v
origin https://github.com/hkui/test.git (fetch)
origin https://github.com/hkui/test.git (push)
7.将本地分支 推送到远程
在本地新建分支 如(b1) 推送到远程( 远程没有就新建)
git push origin b1(本地):b1(远程) 
设置本地与远程之间分支间的通道后就无需 每次都把2边的分支名都写全了
hkui2015@hkui MINGW32 ~/Desktop/osc (b1)
$ git push
fatal: The current branch b1 has no upstream branch.
To push the current branch and set the remote as upstream, use

git push --set-upstream origin b1
设置当前本地分支与远程 分支的关联
git push --set-upstream origin b1(远程分支名存在或者不存在)
删除远程分支
git push origin --delete b1(远程分支)
删除本地分支(全部发布到远程仓库或者合并到主分支后才能被删除)
git branch -d branchname
git branch -D branchname (没合并过的分支强力删除)

标签操作

git tag [commit编号] 默认打在当前最新提交的commit上
git tag 查看所有标签

$ git log --pretty=oneline --abbrev-commit
c097905 add dev1.txt dev11.txt on branch dev1
a651ed5 Merge branch 'b2'
0f7c6c7 Merge branch 'b1'
99171bd 修改忽略配置文件
d379649 添加自定义忽略文件
c568160 删除了b1.txt
modify
9833ae7 add a line on b2 branch
0e07e62 modify b1.txt
1a0cfa7 modify b1
24549b6 modify files
modify files
f46e95b add a line on b2.txt
51ef312 add b2.txt on branch b1
55cedea add a line on b1.txt
38fb557 add b1.txt on b1 branch
058c2f7 add a.php
cec4d7c Update README.md
cfbb500 Update README.md
04ec1f4 Initial commit

对某一次提交打标签 比如 对 "修改忽略配置文件"  这次commit打标签

git tag p1.0 99171bd
标签不是按时间顺序列出,而是按字母排序的
可用git show 查看标签信息

$ git show p1.
commit 99171bde2eddbf67b8860024504bc14556de2978
Author: hkui <@qq.com>
Date: Fri Dec :: +

    修改忽略配置文件

diff --git a/.gitignore b/.gitignore  
index e69de29..ef752b0  
--- a/.gitignore  
+++ b/.gitignore  
@@ -, + @@  
+conf.php  
\\ No newline at end of file

创建带标签说明的tag
$ git tag -a -m "comment 说明" 3628164 (commit操作编号)
还可以通过-s用私钥签名一个标签
$ git tag -s tagname -m "signed version 0.2 released" fec145a
删除标签
$ git tag -d tagname
推送某个标签到远程
git push origin
推送所有还没推送的标签到远程
$ git push origin --tags
如果标签已经推送到远程,要删除远程标签就麻烦一点,先从本地删除

$ git tag -d p1.
Deleted tag 'p1.0' (was 99171bd)

然后,从远程删除。删除命令也是push,但是格式如下:

$ **git push origin :refs****/tags/p1.0**  
To https://git.oschina.net/hk/code.git  
- \[deleted\]         p1.

问题

fatal: the remote end hung up unexpectedly
发生在push命令中,有可能是push的文件过大导致
解决方法:
windows:
在 .git/config 文件中加入

[http]
postBuffer = 524288000

linux:
git config http.postBuffer 524288000