远程仓库可使用Github、Gitee,或自建Gitlab、Gogs服务器,这里使用Github。
# 配置本地用户的用户名邮箱(保存在用户.gitconfig文件)
$ git config --global user.name "Your Name"
$ git config --global user.email "email@example.com"
# 查看配置的用户名和邮箱
$ git config --global user.name
$ git config --global user.email
# 生成SSH公钥用于加密(保存在用户.ssh目录)
$ ssh-keygen -t rsa -C "youremail@example.com"
# 1、关联远程仓库:
$ git remote add origin git@github.com:Your Name/Your prj.git
# 2、把master分支改名为main分支
$ git branch -M main
# 3、第一次推送到main分支
$ git push -u origin main
# 4、第二次以后的push
$ git push # 推送到首次push -u 默认的主机和分支
$ git push origin main # 推送到指定主机和分支
由于远程库是空的,第一次推送main分支时,加上了-u参数,Git不但会把本地的main分支内容推送的远程新的main分支,还会把本地的main分支和远程的main分支关联起来,在以后的推送或者拉取时就可以简化命令。
$ git clone git@github.com:Your Name/Your prj.git
$ git clone git@github.com:Your Name/Your prj.git . # 把项目包含的文件克隆到当前文件夹
clone会把.git目录一并带回,成为了一个可管理的本地仓库
命令
说明
git stash
暂时存储最后一次提交后的变化,放入一个栈中
git stash pop
从栈中取出刚才保存的变化,并合并
使用场景:上次的提交版本出现bug需要修复,但当前的工作区和暂存区已经修改了。可以先把修改的内容暂存起来,恢复到上一次提交,bug修复后再取出上次修改并合并内容。
手机扫一扫
移动阅读更方便
你可能感兴趣的文章