git日常使用

  • 名词解释

git add    把文件添加到暂存区
git commit 把暂存区的所有内容提交到当前分支上
git push   把当前分支的修改推送到远程分支
git pull   把远程分支的修改下拉到当前分支
git status 查看当前分支状态
git log    查看当前分支状态提交日志
git diff <文件名> 查看指定文件修改内容
  • 本地仓库

# 用户标识
git config --global user.name lr
git config --global user.email i@lr.cool

# 初始化仓库
git init

# 创建测试文件lr.txt
echo 'hello world'> lr.txt

# 添加进暂存区
git add lr.txt

# 提交到仓库
git commit -m "lr提交"
  • 远程仓库

# 本地关联远程仓库
git remote add origin https://github.com/xxxxx/xxxxx

# 克隆远程仓库
git clone https://github.com/xxxxx/xxxxx

# 推送本地修改到远程仓库
git push origin master

# 本地拉取远程命令
git pull origin

# SSH keys(使用SSH公钥认证, 推送远程分支无需输入密码)
SSH公钥路径: 
    linux: ~/.ssh/id_rsa.pub
    windows: C:/Users/<用户名>/.ssh/id_rsa.pub
如果没有需要运行命令创建: ssh-keygen

复制id_rsa.pub公钥内容到git账号设置新加SSH keys即可
  • 撤销修改

# git add 后的修改撤销不了

# 撤销单个文件修改
git checkout -- lr.txt

# 撤销所有文件修改
git checkout -- .
  • 版本回退

# 查看要回退的版本id
git log

# 回退本地版本
 git reset --hard d4f213fc242e574130dcc2fac2db3bc4df304288

# 回退远程版本[强制推送到远程版本]
git push -f
  • 分支管理

https://git-scm.com/book/zh/v1/Git-分支-分支的新建与合并

# 建立分支
git branch <分支名>

# 切换分支
git checkout <分支名>

# 合并分支
git pull origin master  # dev
git checkout master     # dev
git merge <dev分支名>    # master

# 删除分支
git branch -d <分支名>

# 推送分支到远程仓库
git push origin <分支名>

# 删除远程分支
git push origin -d <分支名>
原文地址:https://www.cnblogs.com/cooolr/p/git.html