git常用命令

常用命令

ꔷ git init 初始化

ꔷ git add [文件名] 添加修改代码到缓存

ꔷ git commit -m "" 注释

ꔷ git status 被修改文件

ꔷ git diff 查看修改内容

ꔷ git show commit_id 查看某次修改

ꔷ git log 查看提交历史,看要回推到那个版本

ꔷ git reflow 查看命令历史,看要回退到那个版本

ꔷ git pull 拉取最新的改动并merge

ꔷ git push (-u) 提交至远程仓库

ꔷ git reset --hard 回退代码。回退到某个commit_id

ꔷ git checkout -b [分支名] 新建分支

撤销修改

ꔷ git checkout -- readme.txt 撤销工作区修改(尚未add,尚未commit)

ꔷ git reset HEAD readme.txt, 再git checkout -- readme.txt 撤销暂存区修改(已经add,尚未commit)

ꔷ git reset ---hard HEAD^(版本回退) 撤销版本库修改(已经commit,尚未推送到远程)

删除文件

ꔷ git add test.txt, git commit -m "add test.txt"

ꔷ rm test.txt 从工作区删除该文件

ꔷ git rm test.txt,git commit -m "remove test.txt" 从版本库中删除该文件,并提交, 假设工作区开始就删错了,可以用版本库的覆盖工作区的

ꔷ git checkout -- test.txt

分支管理

ꔷ git branch 查看分支

ꔷ git branch [分支名] 创建分支

ꔷ git checkout [分支名] 切换分支

ꔷ git checkout -b [分支名] 创建+切换分支

ꔷ git merge [分支名] 合并某分支到当前分支

ꔷ git branch -d [分支名] 删除分支

ꔷ git branch -D [分支名] 删除未被合并分支

常用小技巧 - 常见错误的解决办法

ꔷ CONFLICT (content): Merge conflict in : 这表示在合并分支时遇到了冲突。

解决方法包括手动编辑冲突并执行 git add 标记为已解决,然后执行 git commit 完成合并。

ꔷ error: failed to push some refs to : 这表示推送到远程仓库时发生了错误。通常是由于远程仓库的内容已经发生了变化。

解决方法是先执行 git pull 拉取远程仓库的更新,解决冲突(如果有),然后再尝试推送。

ꔷ fatal: not a git repository (or any of the parent directories): .git: 这表示当前目录不是一个 Git 仓库,或者在当前目录的任何父目录中都找不到 .git 文件夹。

解决方法是确保当前目录下存在 .git 文件夹,或者进入到一个包含 Git 仓库的目录中。

ꔷ Your branch is ahead of 'origin/master' by 1 commit: 这表示本地分支比远程分支超前了一个提交。

解决方法是执行 git push 将本地提交推送到远程仓库。

ꔷ error: Your local changes to the following files would be overwritten by merge: 这表示在合并时,有文件会被覆盖掉本地的更改。

解决方法是先提交本地更改,或者使用 git stash 将本地更改暂存起来,然后执行合并操作。

ꔷ Permission denied (publickey): 这表示 SSH 密钥认证失败。

解决方法包括确保正确配置了 SSH 密钥,并且公钥已经添加到了 Git 托管服务(如 GitHub、GitLab 等)的帐户中。

ꔷ fatal: repository not found: 这表示 Git 找不到指定的远程仓库。

解决方法包括检查远程仓库的 URL 是否正确,并且确保仓库确实存在。

相关推荐
盗骊2 小时前
Git rebase
git
芝麻馅汤圆儿2 小时前
Git Bash
git
六件套是我3 小时前
在idea中git切换分支,但是我的文件没add,没commit
git
Pi_Qiu_4 小时前
Rsync自动化备份平台建设实战
git·自动化·github
parade岁月1 天前
Git黑科技:让你的配置文件"隐身",再也不用担心误提交!
git
无名咸鱼1 天前
Git与CI/CD相关知识点总结
git·ci/cd
先天打工圣体的男人1 天前
Linux环境gitlab多种部署方式及具体使用
运维·git·gitlab
文慧的科技江湖2 天前
git配置proxy
git
小獾哥2 天前
Centos8系统在安装Git包时,报错:“没有任何匹配: git”
大数据·git·elasticsearch
Clownseven2 天前
如何搭建私有Git服务器?Gitea安装与配置超详细教程 (替代GitHub/Gitee)
服务器·git·gitea