git基本使用

配置

command 说明
git config –global user.name YourName
git config –global user.email YourEmail
git config credential.helper store –file=git_credentails 记住密码 (win10 默认可以记住)
git config –global core.autocrlf true
git config –global core.autocrlf input
git config –global core.autocrlf false

创建仓库

command 说明
git init
touch .gitigonre 创建忽略清单
git init –bare 创建远程仓库 文件夹以 .git 结尾

获取代码

Remote → Local

command 说明
git clone Repo
git pull
git pull Repo RemoteBranch
git pull Repo RemoteBranch:LocalBranch

添加代码

工作区 → 暂存区

command 说明
git add PathName/FileName
git add . 保存新的添加和修改,但是不包括删除
git add -u 保存修改和删除,但是不包括新建文件
git add * 全部提交
git add -A

提交代码

暂存区 → 分支

command 说明
git commit -m ‘’

推送代码

Local → Remote

command 说明
git push
git push Repo LocalBranch
git pust
git push Repo LocalBranch:RemoteBranch

状态 & 记录

command 说明
git status
git log
git log –graph
git reflog 所有操作记录,包括 reset
git diff FileName 工作区(work dict)和暂存区(stage)的比较
git diff –cached 暂存区(stage)和分支(master)的比较
git stash 保存当前状态
git stash list 查看所有以保存的状态
git stash pop 恢复状态并删除
git stash apply 恢复状态,状态不会被删除
git stash drop 删除状态

取消修改

command 说明
git checkout – PathName/FileName

取消添加

暂存区 → 工作区

command 说明
git reset HEAD PathName/FileName 把暂存区的文件移动到工作区

取消提交

分支 → 暂存区

command 说明
git reset –hard head_id 已经commit的代码会丢失
git reset –soft head_id 已经commit的代码会回到暂存区
git reset –mixed head_id 没试
git revert head_id & git push 回滚已经push到远程仓库的代码

注意:git revert是用一次新的commit来回滚之前的commit,git reset是直接删除指定的commit,看似达到的效果是一样的,其实完全不同。

  1. 上面我们说的如果你已经push到线上代码库, reset 删除指定commit以后,你git push可能导致一大堆冲突.但是revert 并不会
  2. 如果在日后现有分支和历史分支需要合并的时候,reset 恢复部分的代码依然会出现在历史分支里.但是revert 方向提交的commit 并不会出现在历史分支里
  3. reset 是在正常的commit历史中,删除了指定的commit,这时 HEAD 是向后移动了,而 revert 是在正常的commit历史中再commit一次,只不过是反向提交,他的 HEAD 是一直向前的

远程

command 说明
git remote add origin RemoteRepo
git remote add Name RemoteRepo
git remote set-url origin NewRemoteRepo
git remote -v
git remote rm Name

分支

command 说明
git branch BranchName 创建分支
git checkout BranchName 切换分支
git checkout -b BranchName 创建并切换分支
git merge BranchName 合并到当前分支
git branch , git branch -a , git branch -r 查看分支
git branch -m OldBranchName NewBranchName 重命名分支
git push orign BranchName:BranchName 创建远程分支
git branch -d BranchName 删除本地分支
git branch -D BranchName 强制删除没有合并的本地分支
git push orign :RemoteBranchName 删除远程分支
git push origin –delete RemoteBranchName 删除远程分支
git fetch -p 修剪远程分支

创建分支后,只要没有执行commit操作,所做的修改不属于任何一个分支

标签

command 说明
git tag TagName
git tag -a TagName -m ‘’
git tag 查看标签
git tag -l TagNameLike 查看制定标签
git ls-remote –tags 查看远程标签
git push origin TagName
git push origin –tags 推送所有标签
git tag -d TagName 删除本地标签
git push origin –delete tag TagName 删除远程标签
git push origin :TagName 删除远程标签

Git 里面的 origin 到底代表啥意思?

image

多分支开发

SSH

ssh-keygen -t rsa # 创建密钥
ssh 用户名@主机地址 # ssh连接

密钥保存在C:\Users\用户名\.ssh\id_rsa.pub文件内

请我喝杯咖啡
请我喝杯咖啡