Mac下github初始化及代码提交

本文详细记录了在Mac上配置Git,创建SSH Key,验证连接,设置用户名和邮箱,添加远程仓库,处理git push错误,以及删除文件的全过程。通过这些步骤,可以成功地将本地仓库提交到GitHub。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我的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 
### 如何在Mac上将代码上传到GitHub教程 对于希望学习如何在Mac上将代码上传至GitHub的新手而言,理解基本的Git命令以及GitHub的工作流程至关重要。由于Git主要通过命令行界面操作,这可能对一些现代计算机用户构成挑战[^2]。 #### 准备工作 确保已安装Git并配置好GitHub账户。可以通过终端验证Git版本来确认是否已经成功安装: ```bash git --version ``` 如果尚未安装,则需先完成此步骤。 #### 创建本地仓库 假设已经在电脑上有想要上传的项目文件夹,在该目录下初始化一个新的Git库: ```bash cd path/to/your/project git init ``` 这条指令会在当前路径创建一个新Git仓库。 #### 添加远程仓库地址 接着设置与GitHub上的目标存储库关联的URL。通常形式如下所示: ```bash git remote add origin https://github.com/username/repository-name.git ``` 这里的`origin`是一个默认名称用于指代主服务器端副本;而后面的链接则是个人GitHub页面中的具体位置。 #### 提交更改 现在可以准备推送之前所做的任何修改了。首先添加所有待提交的内容进入暂存区: ```bash git add . ``` 之后记录这些改动,并附带描述性的消息说明此次更新的目的: ```bash git commit -m "Initial commit" ``` #### 推送数据至上游分支 最后一步就是执行实际的数据传输动作——即把本地变更推送到远端仓库里去: ```bash git push -u origin master ``` 上述命令会将master分支里的最新状态同步给指定的目标源(`origin`)。首次推送时加上-u参数可简化未来同名分支间的交互过程[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值