安装好git,首先就要进行全局变量设置
git config - - global username "username" 在github/码云的用户名
git config - - global email "[email protected]" 注册github的邮箱
初始化git仓库,可以新建文件夹,或在已有项目的根目录下执行初始化命令,会出现.git文件夹
git init
SSH Key
生成SSH Key:ssh-keygen –t rsa –C "你的邮箱@xx.com"
生成Key时弹出选项,回车选择默认即可。
Key保存位置:/root/.ssh
登陆GitHub或码云,创建添加ssh公钥,其内容为/root/.ssh/id_rsa.pub
中文本
已经有了本地库和远程库,二者实现同步
创建文件 .gitignore 告诉git提交的时候就可以忽略的文件类型
touch .gitignore
查看命令可以让我们时刻掌握仓库当前的状态,输出告诉我们,那些文件被修改过了,但还没有准备提交的修改,那些是已经提交过得。
git status
查看文件的不同,清楚的知道文件做了那些修改
git diff readme.txt
告诉git把文件添加到暂存区中 git add 可以使用多次,也可以直接使用 git add . 把所有文件一次全添加到暂存区中
git add filename | git add .
把修改提交到版本库中
git commit -m "提交概要,以便后来查看"
关联github 或码云的远程库(两种连接方式1.https 2.ssh前提要添加ssh公钥)这个远程库是自己创建的要与本地git仓库关联的远程git库,最好同名
git remote add origin [email protected]:youusername/learngit.git
查看以关联的远程库信息(如果在关联远程库时提示 fatal: remote origin already exists.)使用此命令查看
git remote -v
删除之前存在的远程库信息
git remote rm origin
把本地库推送到远程仓库master分支上
git push -u origin master
在初次提交本地仓库到远程库时
$ git push -u origin master
To gitee.com:jiajava/mmall.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:jiajava/mmall.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
显示提交失败,说是你可能是第一次想整合远程的分支的变化,让在使用git push命令之前,先使用git pull 把远程分支上的文件拉取过来在本地整合后,在进行推送
git pull 把远程分支上的文件拉取过来在本地整合后,在进行推送
git pull
执行完git pull 后,在次执行 git push 报了个错误
$ git push -u origin master
To gitee.com:jiajava/mmall.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:jiajava/mmall.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
提示我们创建的当前的分支,没有远程分支新,因为远程分支上刚开始创建里面没啥文件,这里解决办法就是进行强置推送,覆盖远程的master分支
强置推送到远程仓库上
git push -u -f origin master
分支的命令
git branch 查看当前分支
git branch -r 查看远程分支
git checkout v1.0 切换到 v1.0 分支上
git checkout -b v1.0 origin/master 在主干master上创建名字为v1.0的分支并切换到新的分支上
git push origin HEAD -u 把新建分支推送到远程git上