以下为工作这几年常用的git笔记,可能没条理,都是使用经验,分享给需要的同学
1.git提交
新需求或大的feature修改需要开辟新的分支,首先自己申请一个CB号(http://devjira.a.com:3080/secure/Dashboard.jspa),以此为分支号进行开发,开发测试完毕,merge到master前,需保持branch和master同步一直,参考步骤如下:
git checkout master (切换到master分支)
git pull (获取最新的master分支)
if(has auth to push origin) go d else go g
git merge --squash 你自己的分支名 (有冲突在这步解决)
git commit -m "CB-XXX:XXXXXXXXX"
git push
没有权限的同学看这里
合并commit,未push到远程的commit可以使用 git rebase -i,已经提交的用以下操作:
git checkout -b temp (master分支下)
git merge --squash 你自己的分支名 (有冲突在这步解决)
git commit -m "CB-XXX:XXXXXXXXX"
git branch -D 你自己的分支名
git push origin --delete 你自己的分支名
git checkout -b 你自己的分支名
git branch -d temp
git push
发起合并请求
2.git 常用命令
Cannot rebase: You have unstaged changes.
那说明你有修改过的文件
git stash
git pull --rebase (每次push之前最好这样做一次)
git push ....
之后用git stash pop stash
git push origin(remote-name) master(branch-name) 推送到远程仓库
git remote show origin 查看远程仓库
git remote rename pb paul 远程仓库重命名
git remote rm paul 移除远程仓库
git branch 得到当前分支的列表
git branch -v 查看每一个分支的最后一次提交
git branch --merged 查看哪些分支已经合并到当前分支
git branch -d branch-name 删除分支
git branch 的 -vv 查看设置的所有跟踪分支
3.编码错误
Warning: Your console font probably doesn't support Unicode. If you experience strange characters in
the output, consider switching to a TrueType font such as Consolas!
依次执行以下命令:
git config core.quotepath off
git config --unset i18n.logoutputencoding
git config --unset i18n.commitencoding
4.git基本提交命令
创建分支提交到分支
git branch branch-name
git add filename
git commit -m "注释"
git push origin branch-name
主分支合并提交的分支
git checkout master
git merge branch
git push
5.meld合并工具安装
http://meldmerge.org/下载meld
命令行输入:
git config --global merge.tool meld
git config --global mergetool.meld.path "/j/meld/Meld.exe"
6.git log git rebase -i HEAD~i合并多次提交日志
git checkout master (切换到master分支)
git pull (获取最新的master分支)
if(has auth to push origin) go d else go g
git merge --squash 你自己的分支名 (有冲突在这步解决)
git commit -m "CB-XXX:XXXXXXXXX"
git push
没有权限的同学看这里
合并commit,未push到远程的commit可以使用 git rebase -i,已经提交的用以下操作:
git checkout -b temp
git merge --squash 你自己的分支名 (有冲突在这步解决)
git commit -m "CB-XXX:XXXXXXXXX"
git branch -D 你自己的分支名
git push origin --delete 你自己的分支名 git push origin :master //删除远程分支
git checkout -b 你自己的分支名
git branch -d temp
git push
发起合并请求
查找问题的利器 - Git Bisect
http://gitbook.liuhui998.com/5_4.html
7.git删除远程分支
删除远程分支:git push origin --delete branchName 或者 git push origin :branchName
8.git stash
git stash 直接保存,默认将以hash id做提示信息
git stash save 11111111 以11111111为提示名字保存
git stash apply stash@{1} 可以恢复git stash list嵌套堆栈中的任意一次stash保存
git stash show -p显示对比差异
git stash list
git stash clear 清空所有stash
9.git diff HEAD比较当前文件修改内容
a .知道commit id的话
git show commit-id
b .想要查看某次commit的某个文件进行了哪些修改
git show commit-id filename
10.git学习:
Updates were rejected because the tip of your current branch is behind
http://blog.sina.com.cn/s/blog_13fa009300102w7ae.html
11.取消暂存的文件
git reset HEAD file
git reset的3种模式
git reset --hard HEAD^ 撤销上次提交的同时,清空暂存区 和 工作区
git reset --mixed HEAD^ (默认) 撤销上次的提交,清空暂存区
git reset --soft HEAD^ 撤回提交到暂存区,工作区内容不变
参考:https://www.jianshu.com/p/c2ec5f06cf1a
12.撤销对文件的修改
git checkout -- file
git restore file (git新版本)
13.远程仓库移除或者重命名
git remote rename a b
14.git添加项目到远程仓库
git init
git add file
git commit -m""
git remote set-url origin url(包含.git)
git push origin master
15.git push 错误hint: Updates were rejected because the tip of your current branch is behind
git branch --set-upstream dev origin/
git push -u origin branch
参考:
https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013760174128707b935b0be6fc4fc6ace66c4f15618f8d000
16.git commit 后
查看最后一次提交文件及修改行数 git log -n1 --stat
查看最后一次提交 git log -n1
查看修改内容详情git log -n 1 -p
17.添加多个远程url
假设你现在想要增加3个远程库地址,分别为 :
https://git.oschina.net/shede333/swioslibary.git
https://git.oschina.net/shede333/swscrollbar.git
https://github.com/shede333/CoreAnimationTestSW.git
首先,先增加第一个地址 git remote add origin
然后增加第二个地址 git remote set-url --add origin
增加第三个地址 git remote set-url --add origin
….依次类推
这样就完成了添加多个地址到origin库中了,
以后只要使用git push origin master 就可以一次性push到3各库里面了(使用git push也可)
参考:http://blog.sina.com.cn/s/blog_70c0040c0102vgi9.html
查看远程url:git remote -v
git常用命令
http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html
18.git merge问题
19.使用远程分支覆盖本地分支的修改
git fetch --all
git reset --hard origin/master
git pull
20.配置信息查看
git config --local --list 多ssh配置在单独的项目下使用local,否则使用global
查看用户和email
修改用户名和地址
21.git merge 撤销
a.查看提交日志,复制倒数第二个提交commit
git log --pretty=oneline
b.回退到合并前的commit
git reset --hard 【merge前的版本号】
22.git rm --cached
当我们需要删除暂存区或分支上的文件, 但本地又需要使用, 只是不希望这个文件被版本控制, 可以使用
git rm --cached file_path
git commit -m 'delete remote somefile'
git push
23.移除远程分支 git remote remove origin
24.git hash-object -w filename
git cat-file
25.git log --oneline 日志显示一行
git log branchA..branchB 查看B分支还有多少未合并到A
查看所有的git对象
find .git/objects/ -type f
查看提交树对象
$ git cat-file -p master
tree 64d58218e3f4b995e187fe878e318bda850dc0d6
parent 3107912a6ca67c0487f995e5d0049ea2268c87d6
author zhangsan@163.com zhangsan@163.com 1532393145 +0800
committer zhangsan@163.com zhangsan@163.com 1532393145 +0800
26.git添加当前目录和子目录到.gitignore
如:**/*.iml
27.git两个仓库合并
原先有两个仓库user userapi,后面讲user 都作为作为子模块放入user仓库中
合并后
git remote add cur user 将user仓库添加到父目录user
git fetch cur 拉取cur最新的数据
git merge cur/master --allow-unrelated-histories 允许历史合并
git push
参考:https://github.com/deercoder/0-tech-notes/blob/master/Git/git_merge_local_repos.md
git merge --squash ownBranch
28.git设置换行符
打开git bash,设置core.autocrlf和core.safecrlf(可不设置),建议设置autocrlf为input,safecrlf为true,同时设置你的Eclipse、IDEA等IDE的换行符为LF
。
下面为参数说明,--global表示全局设置
2.1、autocrlf
提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true
提交时转换为LF,检出时不转换
git config --global core.autocrlf input
提交检出均不转换
git config --global core.autocrlf false
2.2、safecrlf
拒绝提交包含混合换行符的文件
git config --global core.safecrlf true
允许提交包含混合换行符的文件
git config --global core.safecrlf false
提交包含混合换行符的文件时给出警告
git config --global core.safecrlf warn
参考:https://www.cnblogs.com/warking/p/5718648.html
29.git强制覆盖本地代码
git强制覆盖:
git fetch --all
git reset --hard origin/master
git pull
恢复覆盖的代码
git reflog查看之前的提交
如:01c3817 HEAD@{1}:
git reset --hard 01c3817 即可还原
30.将代码提交到远程分支
a.git add -A 添加所有的文件
b.git commit -m"msg" 提交
c.git remote add origin remoteurl 添加远程仓库地址
d.git pull --rebase origin master 拉去远程仓库代码并rebase
e.git push --set-upstream origin master 与远程仓库建立关联
f.将代码push到远程仓库
31.ssh密钥生成
进入git bash
ssh-keygen -t rsa -C "mail@163.com"
32.git多仓库配置
- a.清除之前的全局配置
git config --global --unset user.name
git config --global --unset user.email - b.进入git bash
ssh-keygen -t rsa -C "mail@163.com"
指定目录下已经生成了对应的公私钥文件了
将公钥添加到仓库
- c.C:Usersshare.ssh目录下创建config文件
Host master
HostName gitee.com
PreferredAuthentications publickey
IdentityFile /c/Users/share/.ssh/id_rsa_163
User user1
Host slave
HostName gitee.com
PreferredAuthentications publickey
IdentityFile /c/Users/share/.ssh/id_rsa_121
User user2
Host jysh
HostName code.aliyun.com
PreferredAuthentications publickey
IdentityFile /c/Users/share/.ssh/id_rsa
User user3
-
d.测试是否配置成功
ssh -T Host -
e.clone仓库 看下面红色框,这里需要改为host
否则会报下面错误
git commit报如下错误是没有添加user.name user.email
$ git commit -m"add config"
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'share@share_pro1.(none)')
上面错误需要设置username useremail
- f.添加配置
git config user.name "username"
git config user.email "email@163.com"
上述配置默认加了--local
git config --list --local
再次提交
$ git add -A
share@share_pro1 MINGW64 /e/code/gitflow (master)
$ git commit -m"add config"
[master (root-commit) 0f891aa] add config
1 file changed, 1 insertion(+)
create mode 160000 gitflowtest
- g.添加远程仓库
git remote add origin git@slave:code_fun/gitflowtest.git (slave为我配置的config文件的Host)
git push --set-upstream origin master
参考:https://www.cnblogs.com/popfisher/p/5731232.html
https://blog.csdn.net/yuanlaijike/article/details/95650625
33.windows设置文件长度
git config --global core.longpaths true
github ssh
ssh-keygen -t rsa -b 4096 -C "username@email.com"
参考:https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
34.git配置 git config --global --list
core.editor=vim #使用vim编辑器
core.autocrlf=input #提交时转换为LF,检出时不转换
core.safecrlf=true #拒绝提交包含混合换行符的文件
core.longpaths=true #windows设置文件长度
credential.helper=store
merge.tool=meld #合并工具
mergetool.meld.path=J:/meld/Meld.exe #合并工具路径
http.sslverify=false #SSL校验
http.lowspeedlimit=0 #下载速度设置
http.lowspeedtime=999999 #下载时间设置
http.postbuffer=1048576000 #下载文件大小设置
35.只拉去最近一次的commit
git clone --depth 1 <repo_URI>
git常见命令:
git config --global user.name "你的名字" 让你全部的Git仓库绑定你的名字
git config --global user.email "你的邮箱" 让你全部的Git仓库绑定你的邮箱
git init 初始化你的仓库
git add . 把工作区的文件全部提交到暂存区
git add ./
git commit -m "xxx" 把暂存区的所有文件提交到仓库区,暂存区空空荡荡
git remote add origin https://github.com/name/name_cangku.git 把本地仓库与远程仓库连接起来
git push -u origin master 把仓库区的主分支master提交到远程仓库里
git push -u origin <其他分支> 把其他分支提交到远程仓库
git status查看当前仓库的状态
git diff 查看文件修改的具体内容
git log 显示从最近到最远的提交历史
git clone + 仓库地址下载克隆文件
git reset --hard + 版本号 回溯版本,版本号在commit的时候与master跟随在一起
git reflog 显示命令历史
git checkout --
git rm 删除版本库的文件
git branch 查看当前所有分支
git branch <分支名字> 创建分支
git checkout <分支名字> 切换到分支
git merge <分支名字> 合并分支
git branch -d <分支名字> 删除分支,有可能会删除失败,因为Git会保护没有被合并的分支
git branch -D + <分支名字> 强行删除,丢弃没被合并的分支
git log --graph 查看分支合并图
git merge --no-ff <分支名字> 合并分支的时候禁用Fast forward模式,因为这个模式会丢失分支历史信息
git stash 当有其他任务插进来时,把当前工作现场“存储”起来,以后恢复后继续工作
git stash list 查看你刚刚“存放”起来的工作去哪里了
git stash apply 恢复却不删除stash内容
git stash drop 删除stash内容
git stash pop 恢复的同时把stash内容也删了
git remote 查看远程库的信息,会显示origin,远程仓库默认名称为origin
git remote -v 显示更详细的信息
git pull 把最新的提交从远程仓库中抓取下来,在本地合并,和git push相反
git rebase 把分叉的提交历史“整理”成一条直线,看上去更直观
git tag 查看所有标签,可以知道历史版本的tag
git tag
git tag
git show
git tag -a
git tag -d
git push origin
git push origin --tags 一次性推送全部尚未推送到远程的本地标签
git push origin :refs/tags/
git config --global color.ui true 让Git显示颜色,会让命令输出看起来更醒目
git add -f
git check-ignore -v
36.tag
查看标签
git tag / git tag -l -list
如果仅对 v1.8.5感兴趣(模糊查询)
$ git tag -l "v1.8.5*"
拉取所有的tag
git pull origin --tags
创建tag
附注标签
git tag -a v1.0(tagname) -m "my version 1.0"
-m 选项指定了一条将会存储在标签中的信息
轻量标签
git tag v1.0
后期打标签
git tag -a v1.2 commitInfo(提交的校验信息)
查看标签信息
git show v1.0
推送到远程
git push origin v1.0
一次性推送所有tag
git push origin --tags
删除标签
git tag -d v1.0
删除远程标签
git push origin :v1.0
git push origin --delete tag
检出标签
git checkout v1.0
参考:https://git-scm.com/book/zh/v2/Git-基础-打标签
37.git/object下的 .pack过大
引发原因:上传过大文件
解决方法
git gc
git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}')"
git filter-branch --force --index-filter 'git rm -rf --cached --ignore-unmatch 大文件名或者**' --prune-empty --tag-name-flter cat -- --all
真正删除
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
git push origin master --force
让远程仓库变小
git remote prune origin
参考:https://www.jianshu.com/p/fe3023bdc825
38.win10更改git密码
控制面板-->用户账户-->管理凭据修改git密码