git的使用
常用命令
-
git init
新建本地git,在需要建立git的文件夹中打开cmd或powershell,键入
git init
,该文件夹中将自动新建一个.git的文件夹,git就是通过这个文件夹来进行该仓库的版本控制的. -
删除本地git
直接删除项目中的.git隐藏文件夹即可.
-
git add .
添加全部已经修改的文件.向本地git提交所有文件,准备commit.该命令效果等同于
git add -A
-
git commit -m '提交说明'
将修改后的文件提交到本地仓库,其中的
-m
是message的意思,git要求每次提交都必须加上提交说明,不然就报错. -
git remote add origin xxxx(远程仓库地址)
连接到远程仓库
-
git push -u origin master
创建一个 upStream (上传流),并将本地代码通过这个 upStream 推送到 别名为 origin 的仓库中的 master 分支上.-u ,就是创建 upStream 上传流,如果没有这个上传流就无法将代码推送到 github;同时,这个 upStream 只需要在初次推送代码的时候创建,以后就不用创建了
常见问题
-
推送远程仓失败
-
错误码
error: src refspec master does not match any.
error: failed to push some refs to 'https://github.com/aszzp/python-web.git' -
解决办法
本地git上没有待提交的更改,所以先add,再commit以下就可以push了.
-
-
推送远程仓失败
-
错误码
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/aszzp/python-web.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没有它的历史).删除仓库,重新建立就好了
-