Git学习

xiaoxiao2021-02-28  122

收藏不错的博客 Git高级教程(二) 远程仓库版本回退方法

环境搭建

工具 Git https://git-scm.com/download/win

git初次运行的配置

用户信息配置
git config --global 更改的配置文件位于用户主目录,之后所有的项目都会默认使用该配置的用户信息,去掉--global 可以配置特定项目的用户信息,相应的配置保存在当前项目的.git/config中。
所有用户信息配置
git config --list 查看已有的配置信息 git config --global user.name <user name> 配置所有项目用户名称 git config --global user.email <user email> 配置所有项目用户电子邮箱 git config user.name 获取用户名称 git config --global user.email 获取用户电子邮箱
配置特定项目的用户信息
git config user.name <user name> 配置该项目用户名称 git config user.email <user email> 配置该项目用户名称 git config user.name 获取该项目用户名称 git config user.email 获取该项目用户名称
配置默认的文本编辑器
当需要输入一些额外的消息的时候,此时会调用一个文本编辑器,默认会使用操作系统指定的文本编辑器,可以通过下面的命令进行设置。 git config --global core.editor <editor>
配置默认的差异分析工具
git diff 使用默认的工具完成代码的差异对比 git difftool --tool-help 查看系统支持的git diff 插件

初始化

git init 初始化git,生成.git文件夹

代码下载

git clone -b <branchName> <.gitAdress> 下载某分支上的代码

git pull 冲突解决

删除某次错误的远程提交

git log 查看提交的commit id,查找要回退的版本的commit id git reset <commit id> 回退到某次提交 git add <files> 将修改的内容加入git库 git commit -m "message" commit git push --force 将本次修改强力推送至服务器

删除某次错误的远程提交,已采纳的方法已经验证可用,注意是二楼哦

git 分支管理

git checkout -b <branchname> 切换到某远程分支 git branch 查看分支 git pull origin <branchname> 将某远程分支pull到本地

git submodule

git submodule是git管理公共类库的一个很好的方式。

git submodule init 初始化子模块 git submodule update 更新子模块,将远程的子模块的信息更新到本地

暂存当前修改并更新远程代码

git stash 保存当前修改,入栈 git pull origin <branch> 更新远程代码 git stash pop 将保存的修改,出栈,加入当前代码库

取消git add的内容

git reset HEAD . //取消所有已经add的内容 git reset HEAD <file> //取消已经add的某个文件

添加submodule

git submodule add <url> //生成或更新.gitmodules 文件 git status

问题和解决方法记录

error: bad signature fatal: index file corrupt

出现问题的原因是git的index文件出错,需要删除.git/index文件,之后执行git reset,重新生成index文件,并且该操作会删除已commit却未push的信息。

rm -f .git/index git reset

<xxxxxdir>modified content

出现该问题的原因是xxxxxdir文件有修改,将直接同步远程的相关文件的代码即可

cd xxxxxdir git diff . git checkout .

fatal: Could not read from remote repository windows

出现该问题是初次使用Git没有进行ssh的公钥配置。windows中公钥位于C:\Users<name>\.ssh\id_rsa.pub,将生成的公钥GitHub配置页面的的SSH Keys中。

ssh-keygen //生成公钥

Cloning into … remote: internal server error42 fatal: protocol error: bad pack header

这个问题是在Git使用过程中最折磨我的,参考,终于可以将相关的代码下载更新了。

git config --global core.compression -1 git clone --depth 1 <url>

Permission denied (publickey).fatal: Could not read from remote repository.

ssh-keygen -t rsa ssh-add ~/.ssh/id_rsa gedit ~/.ssh/id_rsa.pub,copy内容 参看https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/将copy的呢荣粘贴到github的setting中

fatal: refusing to merge unrelated histories

出现该问题是由于两个仓库不同导致的,解决该问题需要在git pull时,添加–allow-unrelated-histories选项。

git pull --allow-unrelated-histories

删除某次提交

git rebase -i commit_id #想要回退前一个提交,将需要的保留的添加pick,不需要保留的drop,之后处理一下冲突 git rebase --continue git push origin branch_name --force #推送到远程库

本地分支版本高于远程

目前遇到的是在发生版本回退或者删除某一提交,另外的一台电脑里面的版本会高于远程分支,执行下面的命令强制将远程覆盖本地。

git reset --hard origin/master

撤销某次commit

git log #查看log信息 git reset <commit_id> #想要撤销的前一个commit id,修改的代码依然存在 git reset --hard <commit_id> #想要撤销的前一个commit id,原修改删除

fatal: Authentication failed for

该问题的博客推荐

git config -–global user.name "xxx" git config –-global user.email "xxx@xxx.com" git.exe fetch -v --progress "origin" git config --system --unset credential.helper

Git合并某个分支的一个commit到另一个分支

解决方法博客推荐

git checkout A git log #找到提交的commit id git checkout B git cherry-pick <commit_id>

查看对某个文件的修改

git查看某个文件的修改历史

cd xxx/xxx/dir #切换到要查看的文件所在的目录 git log --pretty=oneline 文件名 git show <commit_id> #查看相关commit id的修改
转载请注明原文地址: https://www.6miu.com/read-45045.html

最新回复(0)