1.注册账号,github或者gitee
本地配置:
git config --global user.name "your name"
git config --global user.email "your email"
查看配置git config --global --list
2.项目初始化
第一种:去github或者gitee创建项目
git clone https://gitee.com/xxxx/xxxx.git #clone项目到本地
git branch dev #新建分支
git checkout dev
第二种:本地初始化
git init #创建一个目录初始化
git add . #提交到临时仓库
git commit -m "注释" #提交到本地仓库
设置密钥:
本地生成密钥对 :ssh-keygen -t rsa -C "you eamil"
cat ~/.ssh/id_rsa.pub 复制公钥,点击个人头像->设置->ssh公钥添加
git remote add origin https://gitee.com/xxx/xx.git #添加远程仓库地址 git push -u origin "master"
3.基本命令
git status #查看状态
git diff #比对修改
git restore file 或者
git checkout -- readme.txt #将readme.txt文件修改返回到最后一次add状态,即stage状态
git restore --staged <file> 或者
git reset HEAD readme.txt #丢掉stage临时区内容,在运行git checkout -- readme.txt,返回最后一次commit状态
git reset --hard HEAD^ 回退上一版本
git log #查看提交记录
git rebase -i 34we32 #变基操作,合并commit,一般是git log查看commit状态后,合并到哪一个head,使用此命令
如:
git rebase -i 1468b5ce #变基操作,出现以下页面,编辑顶部commit
pick ba1ab37 添加345 #保留此commit,必须保留1468b5ce 的前一个commit,我这边试下来是这个效果
s 43a43ca 添加rebae5 #合并此commit
#
# Rebase 1468b5c..43a43ca onto 1468b5c (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# keep only this commit's message; -c is same as -C but
# opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified); use -c <commit> to reword the commit message
git merge dev #合并分支
git push #提交到远程仓库