git 命令学习
git 的基本操作
git init
一般用来初始化仓库,使用当前目录作为Git仓库,在github创建远程仓库后,会给出以下提示来引导
…or create a new repository on the command line
echo "# test" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/xxx/test.git
git push -u origin main
…or push an existing repository from the command line
git remote add origin https://github.com/xxx/test.git
git branch -M main
git push -u origin main
git pull
用于从远程获取代码合并到本地的版本
git pull
其实就是 git fetch
和git merge FETCH_HEAD
的简写。 命令格式如下:
git pull <远程主机名> <远程分支名>:<本地分支名>
一般写为
更新:
git pull
git pull origin
带有分支,即将远程主机的master分支拉取过来,与本地的brantest分支合并:
git pull origin master:brantest
如果远程分支和当前分支合并,可以忽略本地分支
git pull origin master
git push
同pull类似,命令格式为:
git push <远程主机名> <本地分支名>:<远程分支名>
如果本地版本与远程版本有差异,但又要强制推送可以使用 --force
参数:
git push --force origin master
删除主机但分支可以使用 --delete
参数,以下命令表示删除 origin 主机的 master 分支:
git push origin --delete master
git clone
git clone
拷贝一个 Git 仓库到本地,让自己能够查看该项目,或者进行修改。
命令格式(url为https链接):
git clone [url]
可在仓库点击code查看,点击剪切板标志复制
git 代理设置
git设置代理
git config --global http.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080
git取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy
git遇到的问题——“Please make sure you have the correct access rights and the repository exists.”
解决方案:
-
查询一下git config的配置信息,打开Git Base Here
分别输入
git config --list git config --global user.name ‘username’ git config --global user.email 'xxx@xxx.com'
-
删除.ssh文件夹下所有文件
-
git输入命令
ssh-kengen -t rsa -C"xxx@xxx.com"
之后,即可直接回车,结束后找到在.ssh目录下的id_rsa.pub文件,并用记事本打开,复制全部内容
-
登录github,进入ssh设置,点击New SSH Key(如果已有,选择删除DELETE SSH),填写Title,并在Key处粘贴复制的内容。
-
在git中输入命令:
ssh -T git@github.com
随后,输入命令:yes,回车,解决问题。