Git

104 阅读1分钟

初始化配置

http不支持操作private类型的仓库

#本地生成ssh key,在git平台上添加ssh key
ssh-keygen -t rsa -C "your_email@example.com"

#测试是否添加成功
ssh -T gitee.com

#配置用户名和email,记录在commit中
git config --global user.name "your_name"
git config --global user.email "your_email"

init仓库

# 初始化
git init

# 映射origin和远程仓库地址
git remote add orgin git@gitee.com:hoeing/test-git.git

# 将映射origin删除
git remote remove origin

# 显示所有别名映射
git remote -v

clone仓库

# 直接克隆远程仓库
git clone git@gitee.com:hoeing/test-git.git

提交流程

本地与远程分支对应时,checkout,pull,push都变得极其简单

# 查看分支
git branch

# 切换分支
git checkout master

# 添加文件夹到暂存区
git add ./

# 将暂存区的文件提交到分支
git commit -m 'FirstCommit.'

# 拉取对应远程分支的代码
git pull

# 推送代码到对应的远程分支
git push

常规操作

# 推送本地分支local到远程分支remote
git push origin localhost_branch:remote_branch

# 拉取远程分支remote到本地分支
git pull origin remote_branch

# 强制提交
git commit -n

# 强制推送
git push -f

# 将本分支复制并切换
git checkout -B new_branch

# 删除本地分支
git branch -D new_branch

# 删除远程分支
git push origin --delete remote_branch

进阶操作

# 查看提交日志
git log

# 摘取某次commit,通过新增commit实现,需手动commit,记录到新log
git cherry-pick <commitHash>

# 回退到某个提交,log也会被回退
git reset --hard <commitHash>

# 撤销某个提交,通过新增commit实现,需要手动commit,记录到新的log
git revert -n <commitHash>