关于git新建仓库和更新仓库

新建仓库:

1、git init

输入命令后项目目录会有一个隐藏的.git文件夹

上传所有代码到本地仓库

2、git add . --all (注意add后面有个点,与add之间有一空格)

这样就把代码上传到本地仓库了

3、 git commit -m "initial commit"
(git branch test(创建分支)
git checkout  test (切换分支

在gitee上新建项目,复制https://gitee.com/xxx/xx.git地址

关联本地仓库并上传代码

4、git remote add origin [email protected]:Pandora417/Travel-qunar.git
(指本地仓库和远程仓库建立连接。如果已建立连接了,那这一步可以不要)

5、git push origin master(test)

报错:

To https://github.com/Pandora417/test.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to ‘https://github.com/Pandora417/test.git’
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push --help’ for details.

执行命令:

6、git pull origin master --allow-unrelated-histories
git 在pull或者合并分支的时候有时会遇到这个界面。(会弹出一个奇怪的页面)
可以不管(直接下面3,4步),如果要输入解释的话就需要:

1.按键盘字母 i 进入insert模式

2.修改最上面那行黄色合并信息,可以不修改

3.按键盘左上角"Esc"

4.输入":wq",注意是冒号+wq,按回车键即可
7、git push origin master
(之后就可以成功的pull,push了)

提交到远程报错,如下:

src refspec master does not match any

原因:

本地仓库为空

解决方法:使用如下命令 添加文件;

$ git add add.php addok.php conn.php del.php edit.php editok.php ftpsql.sql index.php

$ git commit -m “init files”
之后在push过程中出现如下错误:

$ git push -u origin master

Warning: Permanently added the RSA host key for IP address
‘xx.xx.xxx.xxx’ to the list of known hosts. To
[email protected]:hahaha/ftpmanage.git ! [rejected] master ->
master (fetch first) error: failed to push some refs to
[email protected]:hahahah/ftpmanage.git’ hint: Updates were rejected
because the remote contains work that you do hint: not have locally.
This is usually caused by another repository pushing hint: to the same
ref. You may want to first integrate the remote changes hint: (e.g.,
‘git pull …’) before pushing again. hint: See the ‘Note about
fast-forwards’ in ‘git push --help’ for details.

提示使用 git pull 之后在 push

使用如下命令解决:
这里有个坑需要注意一下,就是在上面创建远程仓库的时候,如果你勾选了Initialize this repository with a README(就是创建仓库的时候自动给你创建一个README文件)
这是由于你新创建的那个仓库里面的README文件不在本地仓库目录中,这时我们可以通过以下命令先将内容合并以下:

$ git pull --rebase origin master

warning: no common commits remote: Counting objects: 3, done. remote:
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking
objects: 100% (3/3), done. From github.com:hahah/ftpmanage * branch
master -> FETCH_HEAD * [new branch] master ->
origin/master First, rewinding head to replay your work on top of
it… Applying: init files

继续push,$ git push -u origin master
成功。

克隆到本地

1、本地创建文件夹
2、git init
3、git clone https://github.com/Pandora417/test.git

更新仓库

1、未修改项目前,查看项目状态,命令:git status。如下图:
在这里插入图片描述
由图可以看出来,项目刚更新下来,没有文件更改。

注:查看当前状态,必须进入到项目文件中,而不是本地仓库中。

2、修改项目部分文件,再次看项目状态。如下图在这里插入图片描述
由图上红色文字可以看出,“README.md”被更改。

3、将文件修改提交到本地暂存区,命令:

git add file

file 为修改文件名。如下图所示:
在这里插入图片描述
由图可以看出,修改文件添加到本地暂存区后,颜色变了。

注:每次修改后的文件,都必须添加到本地暂存区后,才能更新到项目上。

4、提交当前工作空间的修改内容,命令:git commit -m“修改”,引号里面是提交信息,自己可以填写其他内容。如下图所示:
在这里插入图片描述
由图可以看出,提交后再看项目状态,提示没有内容可以提交。

注:提交的时候必须用-m来输入一条提交信息,类似于svn的commit。

5、将项目更新到github或服务器,命令:git push。如下图:
在这里插入图片描述
简而言之,add url ;commit ;push.三步走

删除仓库

如果想删掉整个仓库,服务器或者GitHub的setting中delete就好

有时候会想把gitlab上的文件删除,但是本地仓库里的文件想保留下来该怎么办,只要用三条命令就能完成了

git rm --cached file/-r dir
git commit -m “del xxx”
git push

回滚并恢复上一次版本

git log
git reset --hard d03f4a88e72433462f7b319ea9242fb804de8eec(这个是commit后面的那个)

原创文章 181 获赞 19 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Pandora_417/article/details/95938640