1.版本回退:
git reset --hard commit_id 回退到某个commit
git reset --hard HEAD~1 回退到上个版本,HEAD指针指向当前版本
tips:
git reflog 查看命令历史
git log 查看提交历史
2.撤销修改
git checkout .|filename 丢弃工作区的修改, 与版本库或暂存区内容保持一致
git reset HEAD filename 撤销暂存区的修改,重新放回工作区
3. 保存进度
git stash save 'stash message' 保存进度
git stash apply stash@{0} | git stash pop 恢复进度
git stash list 查看进度列表
git stash clear 删除所有存储的进度
4.比较
git diff 比较工作区和暂存区的差异
git diff --cached 比较暂存区和版本库的差异
5.合并多次提交
git rebase -i HEAD~n|commit_id
6.查看和修改用户名和邮箱
git config user.name | git config user.email
git config --global user.name "your_name" | git config --global user.email "your email"
7. 生成ssh公钥后添加,git客户端可通过ssh协议访问公司代码库
ssh-keygen -t rsa 生成公钥
cat ~/.ssh/id_rsa.pub 查看公钥
8. 关联代码库
git remote add origin https://github.com/kythen/detect.git
git push -u origin master
9. 标签tag
git push origin :refs/tags/xxx // 删除远程tag
10. 获取其他分支的某些文件
git checkout source_branch file1 file2 file3 // 会直接提交到暂存区
11. 合并某个分支的单个commit
git cherry-pick commit_id
12. 关联远程分支
git branch -u origin/xx
13. 新建分支
git checkout -b target_branch source_branch
14. 解决冲突
git rebase -i target_branch git status 查看冲突、解决冲突 git add . git rebase --continue git push -f