我的mac是已经安装了git了,这里仅记录一下mac配置git以及提交代码的过程。
##首先在本地创建ssh key
https://archives.docs.gitlab.com/16.1/ee/user/ssh.html#generate-an-ssh-key-pair
-
配置git:
$ ssh-keygen-t rsa -C "youremail"
后面的your_email改为你自己注册git时所用的邮箱,之后会要求确认路径和输入密码,我们这使用默认的一路回车就行。
成功的话会在~/下生成.ssh文件夹,进去,打开id_rsa.pub,复制里面的key。
-
创建SSH key
网页进入github自己账户------------>AccountSettings ----------->左边选择SSH Keys-------->Add SSH Key------------->title填自己喜欢的名字,下面粘贴key。
注意:复制.pub中代码时需要用记事本打开,或者vim命令行模式下“set nonumber”取消行号
Add an SSH key to your GitLab account:
https://archives.docs.gitlab.com/16.1/ee/user/ssh.html#add-an-ssh-key-to-your-gitlab-account
##验证是否成功(https://archives.docs.gitlab.com/16.1/ee/user/ssh.html#verify-that-you-can-connect)
$ ssh -T git@github.com
如果是第一次的会提示是否continue,输入yes就会看到:You’vesuccessfully authenticated, but GitHub does not provide shellaccess 。这就表示已成功连上github。
接下来我们要做的就是把本地仓库传到github上去,在此之前还需要设置username和email,因为github每次commit都会记录他们。
$ git config --global user.name "your name"
$ gitconfig --global user.email "your_email@youremail.com"
##进入要上传的仓库, 添加远程地址:
$ git remote add origin git@github.com:yourName/yourRepo.git
后面的yourName和yourRepo表示你再github的用户名和刚才新建的仓库;
如我新建的仓库Project, git remote add origin git@github.com:dasima/Project.git
加完之后进入.git,打开config,这里会多出一个remote“origin”内容,这就是刚才添加的远程地址,也可以直接修改config来配置远程地址。
其中:origin是库名,可任意取, 如取名为wolaile
##提交上传
添加文件:
$ git add test.cpp
提交修改:
$ git commit-m "first commit"
将本地仓库推送到远程服务器:
$ git push origin master
##报错处理
git push 的时候会报错:
error: failed to push some refs to
然后搜索,网上说需要先git pull:
zhahaoya$ git pull git@github.com:dasima/Project.git
执行了git pull之后又报错:
fatal: refusing to merge unrelated histories
看了stackoverflow, 原来是git在2.9版本后, 属于不同分支的合并,需要加参数–allow-unrelated-histories
http://stackoverflow.com/questions/37937984/git-refusing-to-merge-unrelated-histories
git pull加上参数–allow-unrelated-histories之后,再次提交代码, 初始化提交成功:
$ git pull origin master --allow-unrelated-histories
代码提交成功:
$ git push origin master
##测试
$ git add bm/test
$ git commit -m "add bm"
$ git push origin master
提交成功
这里每次提交代码到远程库(remote branch)git push , 会提示:
git push --set-upstream origin master
如果经常提交, 就在push的时候输入-u参数, 这样下次只需要输入git push即可提交代码到远程库;
参考:http://stackoverflow.com/questions/6089294/why-do-i-need-to-do-set-upstream-all-the-time
这里截取精华部分:
A shortcut, which doesn’t depend on remembering the syntax for git branch --set-upstream 1 is to do:
git push -u origin my_branch
… the first time that you push that branch. You only need to do it once, and that sets up the association between your branch and the one at origin in the same way as git branch --set-upstream does.
Personally, I think it’s a good thing to have to set up that association between your branch and one on the remote explicitly. It’s just a shame that the rules are different for git push and git pull.
删除没用的文件
$ git rm test.cpp
$ git commit -m "comment"
$ git push origin master
If you deleted a file from the working tree, then commit the deletion:
$ git commit -a -m "A file was deleted"
or
$ or git commit -am "A file was deleted"
And push your commit upstream:
$ git push