git 操作遇到的问题与解决方法

时间:2023-03-10 07:02:05
git 操作遇到的问题与解决方法
一、使用git在本地创建一个项目的过程,Git 上传本地文件到github
$ makdir ~/hello-world //创建一个项目hello-world 
$ cd ~/hello-world //打开这个项目
$ git init //初始化
$ git add . //提交所有文件
$ touch README $ git add README //更新README文件
$ git commit -m ‘first commit’ //提交更新,并注释信息“first commit”
$ git remote add origin git@github.com:xxxx/hello-world.git //连接远程github项目
$ git push -u origin master //将本地项目更新到github项目上去

二、操作笔记

删除文件:

rm filename 本地删除

-> git commit -m “remarks info”  提交,彻底删除,版本库删除

-> git checkout filename               撤销,恢复

远程仓库:本地Git仓库和github仓库关联

github账号

创建SSH Key

查看是否有id_rsa和id_rsa.pub: cd ~/.ssh/   ls -l

不存在-> ssh-keygen -t rsa –C “youremail@example.com”

分支

查看分支:git branch

创建分支:git branch branch-name

切换分支:git checkout branch-name

创建+切换分支:git checkout –b branch-name

合并某分支到当前分支:

(1) git merge branch-name (默认,fast forward  模式下,删除分支后,会丢掉分支信息)

(2) git merge –no-ff -m “注释” branch-name (禁用 fast forward )

删除分支:git branch –d branch-name

推送分支到远程库:git push origin branch-name

查看远程库信息 : git remote / git remote -v (详细信息)

查看当前状态:git status

查看历史信息:git log / git reflog

查看分支树形图: git log —graph —pretty=oneline —abbrev-commit

版本回退:

git reset —hard 版本号

git reset —hard HEAD^ (^的个数 === num)

git reset —hard HEAD~num (num 前几版本)

git reset HEAD filename 暂存区-> 工作区 HEAD最新版本

git checkout  — filename  丢弃工作区修改

中断此时任务,切换到其他分支工作

git stash 隐藏工作现场

git stash list 列表工作现场

git stash pop = git stash apply 恢复现场 + git stash drop 删除stash暂存任务

三、一些可能遇到的问题解决:
(1)如果输入$ git remote add origin git@github.com:djqiang(github帐号名)/gitdemo(项目名).git
提示出错信息:fatal: remote origin already exists.
解决办法如下:
1、先输入$ git remote rm origin
2、再输入$ git remote add origin git@github.com:djqiang/gitdemo.git 就不会报错了!
3、如果输入$ git remote rm origin 还是报错的话,error: Could not remove config section ‘remote.origin’. 我们需要修改gitconfig文件的内容
4、找到你的github的安装路径,我的是C:\Users\ASUS\AppData\Local\GitHub\PortableGit_ca477551eeb4aea0e4ae9fcd3358bd96720bb5c8\etc
5、找到一个名为gitconfig的文件,打开它把里面的[remote "origin"]那一行删掉就好了!
(2)如果输入$ ssh -T git@github.com
出现错误提示:Permission denied (publickey).因为新生成的key不能加入ssh就会导致连接不上github。
解决办法如下:
1、先输入$ ssh-agent,再输入$ ssh-add ~/.ssh/id_key,这样就可以了。
2、如果还是不行的话,输入ssh-add ~/.ssh/id_key 命令后出现报错Could not open a connection to your authentication agent.解决方法是key用Git Gui的ssh工具生成,这样生成的时候key就直接保存在ssh中了,不需要再ssh-add命令加入了,其它的user,token等配置都用命令行来做。
3、最好检查一下在你复制id_rsa.pub文件的内容时有没有产生多余的空格或空行,有些编辑器会帮你添加这些的。

(3)如果输入$ git push origin master
提示出错信息:error:failed to push som refs to …….
解决办法如下:
1、先输入$ git pull origin master //先把远程服务器github上面的文件拉下来
2、再输入$ git push origin master
3、如果出现报错 fatal: Couldn’t find remote ref master或者fatal: ‘origin’ does not appear to be a git repository以及fatal: Could not read from remote repository.
4、则需要重新输入$ git remote add origingit@github.com:djqiang/gitdemo.git (4) 从服务器获取更新
git status 是一个很有用的命令,用来查看本地repository的状态,它会显示文件的新增/修改/删除的状态。

  如果服务端有更新,git status也会有相应的提示。

D:\Dev\Github\bid>git status
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)

nothing to commit, working directory clean

  上面的显示的意思就是,有一个更新还没有反应到我本地来,可能是别人往server上checkin了一点东西。 使用git pull命令拿这些更新到本地来。

D:\Dev\Github\bid>git pull
Updating abf79f6..db7b6e3
Fast-forward
routes.js | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)


  如果服务端和本地文件都做了改动,使用git pull时就会提示冲突:

D:\Dev\Github\bid>git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://github.com/*****/***
db7b6e3..211ff7f master -> origin/master
Updating db7b6e3..211ff7f
error: Your local changes to the following files would be overwritten by merge:
routes.js
Please, commit your changes or stash them before you can merge.
Aborting

  这时候推荐手动检查一下文件的版本,到底需要哪个。有一个git mergetool可以帮助merge code。

  使用git checkout可以丢弃掉本地的改动,然后使用git pull去拿server上的最新更新。

    丢弃一个文件的所有改动:

git checkout xx.js

  然后再使用git pull,就可以拿到server上的版本的代码了。

 (5)本地没有update到最新版本的项目(git上有README.md文件没下载下来)

    本地直接push所以会出错:

    ! [rejected] master -> master (fetch first)

    error: failed to push some refs to 'git@github.com:xxxx/xxxxx.git'

    解决方法: git pull --rebase origin master 再 git push -u origin master

github一般操作流程(新建上传、追加)

##Create a new repository on the command line
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:teamtogether/TestProject.git
git push -u origin master ##Push an existing repository from the command line
git remote add origin git@github.com:teamtogether/TestProject.git
git push -u origin master

 附上一张git操作图:

git 操作遇到的问题与解决方法