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 [email protected] 设置 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 [email protected]: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 提交是线性的

相关推荐
码农黛兮_465 小时前
Git 常用命令大全
git
一弓虽5 小时前
git 学习
git·学习
疯狂的沙粒13 小时前
如何通过git命令查看项目连接的仓库地址?
大数据·git·elasticsearch
qq_2546177714 小时前
Gerrit+repo管理git仓库,如果本地有新分支不能执行repo sync来同步远程所有修改,会报错
git
π大星星️15 小时前
Git分布式版本控制工具
分布式·git
kingbal15 小时前
IDEA:配置 Git 需要完成 Git 路径设置、账号认证以及仓库关联三个主要步骤
git·idea
司徒小夜1 天前
处理git没做修改,但是文件显示变更的情况
git
m0_749317521 天前
vscode里如何用git
ide·git·vscode
Hello.Reader1 天前
Git 安装全攻略Linux、macOS、Windows 与源码编译
linux·git·macos
王景程1 天前
SELinux是什么以及如何编写SELinux策略
git·github