配置
git config --global user.name "Your Name" git config --global user.email "email@example.com"
生成非对称钥匙
#创建SSH key(主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件): ssh-keygen -t rsa -C "youremail@example.com" #登陆GitHub,“Account settings”——“SSH Keys” #点“Add SSH Key”,填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容。
输出
Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in id_rsa.gitee. Your public key has been saved in id_rsa.gitee.pub. The key fingerprint is: SHA256:lmjU8A4k+r6liYJmENBPM/7Frx3XDg98VeWvIQ9dLyw xxxxx@xxxxx.com The key‘s randomart image is: +---[RSA 2048]----+ | . . o o| |. ..+o + ..| |. .+ oo.o +| |. .o. +o. o .+| | . ..o.S. .E.=.o| |. . ... o ==o+ | |o . . o o *o | |oo . = . . o | |o.. + | +----[SHA256]-----+
查看相关id_rsa.pub后缀的钥匙后,复制cat输出,填写到对应代码存储库服务的ssh设置中
如果是新的工程项目
mkdir learngit
cd learngit
git init
如果是需要继续的项目
git clone xxxxxxxxxxxx就行
如果不需要历史库,可以使用浅克隆,shallow clone
由于clone的内容非常少,因此速度就大大提升了。查看git log
也只有最后一次递交记录而已。
# 浅克隆经常在一些大型仓库中很有用——不用花费大量时间去clone一个完整的仓库,仅仅checkout出来某个分支(如master)的最新N次递交:
git clone --depth 1 https://github.com/openwrt/openwrt.git
关联远程库
git remote add origin git@github.com:******/#####.git
添加至仓库(又名版本库)
git add . git commit -m "wrote update feature comment"
信息查看
git status #当前状态 git log git log --pretty=oneline #列表查看
git log --all --grep=‘homepage‘ # 在所有提交日志中搜索包含「homepage」的提交
git log --author="Maxence" # 获取某人的提交日志
添加推送
git push -u origin master(第二次就不用-u了) 推送到其他分支(并命名) git push origin dev
查看远程库信息
git remote #详细 git remote -v
版本回滚
#回滚上一个版本, git reset --hard HEAD^
#利用commit的id 回滚 git reset --hard 3628164
#HEAD //表示当前版本
#HEAD^ 和 git reset // 上一个版本
#HEAD^^ //上上一个版本
#HEAD~10 //前10个版本
参考日志 git reflog
git reflog <subcommand> <options> # Reference logs(参考日志),或者叫做"reflogs",记录了分支的tips(提示信息?)或者其他参考在本地仓库被更新的时间
git log是显示当前的HEAD和它的祖先的,递归是沿着当前指针的父亲,父亲的父亲,…,这样的原则。
git reflog根本不遍历HEAD的祖先。它是HEAD所指向的一个顺序的提交列表:它的undo历史。reflog并不是repo(仓库)的一部分,它单独存储,而且不包含在pushes,fetches或者clones里面,它纯属是本地的。
分支管理
查看分支:git branch -a 创建分支:git branch <name>
删除本地分支 git branch -d <name>
删除远程分支 git push origin --delete <name> 切换到其他分支:git checkout <name> 创建+切换分支:git checkout -b <name> 合并某分支到当前分支:git merge <name>
.gitgnore 使git忽略掉目标文件
原文:https://www.cnblogs.com/clemente/p/13288640.html