Git 操作指南

一些概念
HEAD 指向当前分支上最近的一次提交
相对引用
HEAD^向上移动1个提交记录
HEAD~3向上移动多个提交记录
git 问题处理
每次 push 都要输入账号密码
git config --global credential.helper store缓存账号密码cat ~/.gitconfig查看配置结果- 配置 ssh
修改了 github 的账号,需要修改本地仓库的配置
git remote -v查看当前连接的远程仓库git remote rm origin移除 origingit remote add origin git@github.com:simonwong/react-universal-template.git重新关联git push --set-upstream origin master设置上游
- 覆盖上一次commit
git commit --amendgit commit --amend --reset-author
- fork 后,如何与原仓库同步
git remote -v查看远程仓库的路径,如果没有upstream(上游代码库),继续git remote add upstream [git 地址]向本地添加上游代码库- 查看本地有没有需改,该 stash stash ,该 push push
git fetch upstream更新上游仓库- 切到你要的分支 master 或者 feature 啥的,然后
git merge upstream/master(其实我有点担心会有 merge 节点,试了一次没有,如果有 merge 节点,可以试试 reabse) git push
批量删除分支
git branch |grep 'xxx' |xargs git branch -D其中
git branch |grep 'xxx'可以查看所有匹配到 xxx 的分支。|xargs是向后传前面的返回结果
ssh 配置流程
- 命令行输入
ssh-keygen -t ed25519 -C "your_email@example.com" - 确定目录文件名、确定密码(可以不用密码,不然每次都要输入)
- 在 gitlab、gitHub 上输入你生成的公钥(xxxx.pub)
- 需要的话,在
~/.ssh/cconfig中配置服务器 host 以及 密钥路径 (~/.shh/xxx)
- 命令行输入
同步 remote 被删除了的分支
git remote prune origin或者git fetch --prune区别参考 What are the differences between git remote prune, git prune, git fetch --prune, etc
常用操作
远程仓库a
git remote add origin git@server-name:path/repo-name.git 关联远程仓库
git push -u origin master 第一次关联后的推送
git push -u origin HEAD 关联分支
git push origin <分支名> --force commit 撤回后,推送。用来撤回线上的 commit push
查看
git log显示最近的提交历史
git log --pretty=oneline简化历史显示
git reflog记录了每一次命令
在命令行输入git reflog,会出现丢失的commit信息列
找到自己需要的commit行, git checkout -b recovery q1dw23d
git checkout master git merge recovery 切回主分支
分支
git checkout -b newBranch 创建并切换新分支
git remote update origin -p 更新本地显示的远程分支
git push origin --delete branch 删除线上分支
git push origin --follow-tags tag 跟随分支一起 push
合并
git merge --no-ff theBranch 合并分支,并且保留原分支的提交历史
git rebase theBranch 线性的合并分支,theBranch变成了当前分支的父提交节点
分离HEAD
HEAD是一个对当前检出记录的符号引用分离
HEAD就是让其指向了某个具体的提交记录而不是分支名
git checkout commit_id从分支中分离HEAD并让它指向一个提交记录
- 相对引用
在一个分支或
HEAD开始计算,
^向上移动1个提交记录
~<num>向上移动多个提交记录
git checkout branch^ 相对引用到branch分支的上一个提交
git branch -f master HEAD~3 强制将master分支的提交移动到HEAD的上3个提交
修改 commit
git commit --amend
git commit --amend --author "name <email@email.com>" 修改作者
撤销变更
git checkout -- file_name 撤回更改
git reset --hard 1094a
git reset HEAD~2
版本回退指定commit id的分支。或者HEAD的上几个位置。
但是reset后前面的变更,依然处于未加入暂缓区状态
git revert HEAD向下新增加一个提交,提交的内容为要撤回的提交的内容
git reset --soft HEAD^ 撤回 commit 且保留更改内容
变基
git rebase -i asdas asdas 提交之前的都合并
pick 97fe73f feat: update
pick 62573b8 feat: add
# 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 <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# 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>]
pick 选择这个 commit 作为最新的代码
squash 将这个 commit 压缩
然后 git rebase --contine
贮藏
git stash 将变更贮藏
git stash list 贮藏列表
git stash apply 应用贮藏
git stash drop stash${1} 移除
tag 操作
git tag查看taggit tag -a v1.1 -m "version1.1"创建taggit show v1.1查看tag 信息git push origin v1.1推送tag