git 常用命令及问题

一、常用命令

git add filename

git add .

git commit -m "messge"

git commit --amend 修改最近一次的提交

git push origin HEAD:refs/for/master

git clone url

git checkout branchname 切换分支

git branch -r 查看远程仓库分支列表

git branch branchname 新建分支

git branch -v 查看分支列表

git pull 拉取远程仓库数据,并与本地指定的分支合并

git fetch 从远程仓库取来数据,但并不自动merge

git merge branchname 合并指定分支到当前分支

git pull 等同于 git fetch + git merge

git pull --rebase 等同于 git fetch + git rebase

git reset 回退版本,可以回退到指定的之前某次提交的版本,不涉及远程仓库

git reset --hard 彻底回退

git rebase branchname 变基,在指定分支上重新应用当前分支上的新提交,指定分支上会产生新提交,目的:维持线性的提交历史

git status 查看工作树状态

git log 查看提交日志

git stash 暂存未提交的修改

git stash apply 把最近的stash应用到工作区域

git stash pop 删除最近的stash

git stash list 查看stash列表

git stash drop 删除指定的stash

git config cmd

  1. git init:创建空仓库/重新初始化已存在仓库

  2. git config --local user.name zhaohualei 设置 git name

git config --local user.email zhaohualei@hellobike.com 设置 git email

git config --list 检查已有的配置信息

二、 git 问题

  1. Q: error: You have not concluded your merge (MERGE_HEAD exists).

A: 1)git merge --abort 放弃合并

2)pull 之前本地先提交代码

3)用 git pull --rebase

  1. Q: 修改最近一次的提交

A: 1) git add .

2) git commit --amend

3) git push origin HEAD:refs/for/master

  1. Q: ERROR: missing Change-Id in commit message footer

A: 1) gitdir=(git rev-parse --git-dir); scp -p -P 29418 zhaohualei@git.cheyaoshicorp.com:hooks/commit-msg {gitdir}/hooks/

  1. git commit --amend

  2. git push origin HEAD:refs/for/master

B: 1) 备份本地修改的内容

  1. git fetch --all

  2. git reset --hard origin/master

  3. git pull

  4. 备份内容替换本地内容,而后正常操作提交代码

  1. Q: CONFLICT (content): Merge conflict in README.md, Automatic merge failed; fix conflicts and then commit the result.

A: 1) git status 查看文件状态

2)vi filename 修改冲突文件

  1. git add filename 再重新提交

  2. git commit -m "conflict description"

  3. git push origin HEAD:refs/heads/master

  1. Q: 分支合并 注意事项

A: 分支都提交之后,再合并

  1. Q: git rebase -i 合并多个提交为 一个提交

A: git rebase -i 选择要修改的提交 git rebase -i HEAD~2

编辑 pick 为 squash,但保留第一次提交为pick,squash:是这个提交会被合并前一个提交

解决冲突后, git add .

git rebase --continue

git push -f origin

  1. Q: git stash、git add 区别

A: add 是为了 commit

stash 是为了方便操作,例如:切换分支时,保持工作区域的干净

  1. Q: git merge、git rebase 区别

A: merge 从分支会多出一次commit,且产生了新的节点;rebase 从分支未产生新的commit,且没有形成新的节点

merge 提交是非线性的; rebase 提交是线性的

相关推荐
A_Lonely_Cat7 小时前
记一次 GitHub 幽灵协作者大清洗:强制重写 Git 历史与穿透 CDN 缓存实践
git·github
和你看星星2 天前
Git rerere:让重复冲突只解决一次
git
嘻嘻仙人6 天前
Ubuntu中 git上传自己的项目和二次上传一般流程
git·github
Patrick_Wilson6 天前
Squash Merge 的血缘陷阱:为什么删掉的代码又活了过来
前端·git·程序员
沉浸学习的匿名网友6 天前
什么是 .gitignore?为什么每个 Git 项目几乎都离不开它?
前端·git
深海鱼在掘金7 天前
Git 完全指南 —— 第3章:理解工作区、暂存区、版本库三个核心
git
江华森7 天前
Git 基础筑基:从原理到团队协作的全栈实战
git
JakeJiang7 天前
Git 必备命令指南:从日常高频到项目开发实战
git
叫我少年8 天前
Windows 中安装 git
git
深海鱼在掘金13 天前
Git 完全指南 —— 第1章:Git 概览与版本控制演进
git