git 常用指令

234 阅读2分钟

更新本地代码

git pull --rebase origin origin_branch

新建分支

git checkout -b new_branch  # 新建并切换分支

切换分支

git checkout branch   # 切换分支
git checkout -        # 切换到上一次分支

查看本地文件状态

git status

暂存代码

git add .

提交代码

git commit -m 'message'

查看提交信息

git log             # 当前分支
git log --all       # 所有分支
git log --oneline

代码推到远端

git push origin origin_branch

合并分支

git merge --no-ff branch

清除本地文件修改

git checkout -- xxx   # 某个文件
git checkout -f       # 所有

临时保存最近修改

git add .     # 没有被 git 管理的文件,需要使用
git stash

查看stash

git stash list

恢复临时保存

git stash pop stash@{stash_id}

git stash冲突

git stash pop 冲突,不会自动删除git stash中的记录需手动清理

git stash drop stash@{stash_id}

删除本地分支

git branch -d local_branch    # 普通删除
git branch -D local_branch    # 强制删除

删除远端分支

git push --delete origin origin_branch

Tip:无法重命名远端分支,需要现删除远端分支,再将本地分支推到远端。

将本地分支推送到远端

git push origin local_branch:origin_branch

拉远端分支

git fetch
git checkout origin_branch

重命名本地分支

git branch -m old_branch new_branch   # 不在 old_branch
git branch -m new_branch      # 在 old_branch

修改最近一次提交信息

git commit --amend -m 'message'

修改git仓库开发者信息

git config --global user.name
git config --global user.email

撤销上一次提交

git reset HEAD^1

回滚

git reset --hard commit_id

取消合并

git merge --abort

查看所有操作记录

git reflog

Tip:搭配回滚可御剑飞行

打标签

git tag v1.2.3    # 当前版本打标签
git push origin --tags    # 标签推到远端

查看git远程关联

git remote -v

关联远程仓库

git remote set-url --add origin origin_url

添加远程仓库

git remote add origin_name origin_url