Hero Image
Github错误处理

github 问题处理 分离指针 HEAD detached from ff8573b 分离头指针是说Git仓库的HEAD指针没有指向任何分支,而是直接指向一个特定的commit。正常情况下,HEAD应该指向一个确定的分支,但在分离头指针状态下,HEAD指向了某个commit。 以下指令会导致处于分离头指针状态: 直接检出了某个commit 检出远程指针时并没有在本地创建对应的本地分支 在主仓库中的submodule仓库(子模块)一般都处于分离头指针状态,这是因为主仓库对应的是子仓库的某一次提交,在检出时就会检出特定的commit提交,而不是某个分支。 HEAD detached from就是报告了github处于分离头指针状态,如果我们需要将当前的修改保存到main分支,则按照下述指令进行。 # 创建临时分支,保存修改 git branch temp # 切换到主分支 git checkout main # 合并temp 分支 git merge temp # 删除 temp 分支 git branch -d temp # 将修改推送到远程 git push origin main 拉取分支 将云端指定分支拉取到本地 # 获取远程分支信息 git fetch origin # 查看远程分支有哪些分支 git branch -r # 创建指定分支 git checkout -b dev origin/dev 丢弃提交 丢弃某一次commit 在项目开发中,如果进行了一次错误提交,需要删除这次提交所对应的修改,最安全的方式是创建一次新的commit来撤销之前的commit。 # 创建一个新的commit来撤销之前的commit git revert <commit-hash> 拉取仓库错误 拉取仓库错误 ssh: Could not resolve hostname github.com: Temporary failure in name resolution fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. 在wsl中拉取仓库中出现了这个错误,但是在宿主机是可以访问github的,这个问题是DNS原因导致的,可以按照如下步骤解决:

Hero Image
github 使用语法

github 语法流程图 本地初始化仓库 git init git add README.md git commit -m "first commit" git branch -M main git remote add origin git@github.com:user_name/test_init.git git push -u origin main 推送现有仓库到远程仓库 git remote add origin git@github.com:user_name/test_init.git git branch -M main git push -u origin main 克隆远程仓库到本地 git clone git@github.com:user_name/test_init.git 仓库管理 git init # 初始化新的Git仓库 git clone <url> # 克隆远程仓库 git remote add origin <url> # 添加远程仓库 git remote -v # 查看远程仓库 推送与拉取 git push origin <branch> # 推送到远程分支 git push -u origin <branch> # 设置上游分支并推送 git pull origin <branch> # 从远程拉取并合并 git fetch origin # 获取远程更新 git fetch --all # 获取所有远程更新 状态查看 git status # 查看工作区状态 git status -s # 简短格式显示状态 git log # 查看提交历史 git log --oneline -n # 单行显示最近n条提交历史 git log --graph # 图形化显示分支历史 git diff # 查看工作区与暂存区的差异 git diff --staged # 查看暂存区与HEAD的差异 文件操作 git add <file> # 添加文件到暂存区 git add . # 添加所有文件到暂存区 git add *.c # 添加所有.c文件 git rm <file> # 将文件从暂存区和工作区中删除 git rm --cashed <file> # 将文件从暂存区删除,工作区得以保留 git rm -f <file> # 将文件从暂存区和工作区中删除 git mv <old> <new> # 重命名文件 git checkout -- <file> # 从最近的一次提交中恢复文件,撤销工作区的修改,如果该文件曾经提交到 暂存区,则暂存区中的文件不受影响 git restore <file> # 恢复特定文件到暂存区状态 git restore . # 恢复所有文件到暂存区状态 提交管理 git commit -m "消息" # 提交更改 git commit -am "消息" # 添加并提交已跟踪的文件 git commit --amend # 打开编辑器,修改最后一次提交 git commit --amend -m "消息" # 直接修改最后一次提交 git reset HEAD <file> # 工作区不变,将本地仓库和暂存区修改到指定的<commit-hash> git reset --soft <commit-hash> # 暂存区,工作区不变,将本地仓库修改到指定的<commit-hash> git reset --hard HEAD # 完全重置到最后一次提交,暂存区,工作区,本地仓库均变化 分支管理 git branch # 查看本地分支 git branch -a # 查看所有分支,本地和远程 git branch <name> # 创建新分支 git checkout <branch> # 切换到指定分支 git checkout -b <name> # 创建并切换到新分支 git branch -d <name> # 删除分支 git branch -m <old> <new> # 重命名分支 合并与变基 git merge <branch> # 合并当前分支到<branch>分支,在流程图中有交叉 git rebase <branch> # 变基操作,将当前分支合并到<branch>分支,在流程图中没有交叉 git cherry-pick <commit-hash> # 将特定的<commit-hash>提交从一个分支引入到当前分支 标签管理 git tag # 查看所有标签 git tag <name> # 创建标签 git tag -a <name> -m "消息" # 创建带注释的标签 git push origin --tags # 推送所有标签 搜索查看 git show <commit> # 显示指定提交的详细信息 git blame <file> # 查看文件的每一行是谁修改的 git grep "关键词" # 在仓库中搜索关键词 git log -S "关键词" # 搜索包含关键词的提交 清理维护 git clean -n # 预览要清理的文件 git clean -f # 强制清理未跟踪的文件 git gc # 垃圾回收 git prune # 清理孤立的提交 stash操作 git stash # 将工作区,暂存区放置到 stash 区域,会删除工作区,暂存区的修改 git stash list # 查看 stash 列表 git stash pop <commit-hash> # 将stash区域保存的<commit-hash>状态恢复到工作区,暂存区,并删除该stash状态 git stash apply <commit-hash> # 将stash区域保存的<commit-hash>状态恢复到工作区,暂存区,不删除该stash状态