什么是 Git
git 是一个分布式版本控制系统,维护的就是一个commitID树,分别保存着不同状态下的代码。 所以你对代码的任何修改,最终都会保存在本地的 .git 文件夹下
新增分支
git checkout -b newBranch
git push origin newBranch
删除分支
// 删除本地分支,如果本地还有未合并的代码,则不能删除
git branch -d oldBranch
// 强制删除本地分支
git branch -D oldBranch
// 删除远程分支
git push origin -d oldBranch
修改未 push 的 commit 信息
git commit --amend 可以对上一次的提交做修改
添加某一文件全部内容
git add header-footer/xx/pcgit add file/xx
取消删除跟踪文件
git checkout . && git clean -xdf : 请勿随便使用 git clean -xdf
取消删除最后一个提交/更改
git reset HEAD~1 : 此命令将恢复/删除最后一个提交/更改,然后可以再次 pull 再推送,可用其来解决冲突(保存文件,执行命令,git pull , 放置文件)
回滚记录
// 会将提交记录回滚,代码不回滚,用来合并 commit
git reset abc123
// 会将提交记录和代码全部回滚
git reset –hard abc123
// 将部分代码文件回滚
git checkout – files
stash 储藏
stash 储藏,只能保存修改,不能储藏新建文件
使用方法:
git stash save ‘some ‘ 保存信息—可进行切换分支
git stash list 查看所有列表
git stash apply 不会删除进行恢复
git stash pop 恢复删除
git stash clear 全部删除
合并 commit
rebase
- git rebase -i commitId (这个 commitid 是跟随之后的id, 也就是 合并的分支的前一个分支– 分支 123 ,那么久是git log 的 第三个 commitid)
- 然后修改下方的 id pick 为 squash
- 删除老的commit 信息, 输入新的 commit 信息
- :wq over
reset
使用 git log 找到起始 commitID
git reset commitID,切记不要用 –hard 参数
重新 git add && git commit
git push -f origin branchName,因为会有冲突,所以需要强制覆盖远端分支,请务必谨慎。
合并到 master 中,然后更新远端 master。
reflog 找回丢失的提交
变基时干掉过的某次提交,你只要在本地工作副本中提交过,那么,提交就会被添加到引用日志 reflog 中,你依然可以通过 git reflog 显示当前分支的所有活动的列表
运行:
- git reflog
- git checkout
想回到的地方 - 复制信息
- git checkout HEAD