记录在linux系统中如何使用git命令
链接github
通过ssh来连接github
ssh-keygen -t rsa -C "your_email@youremail.com"//生成公私钥对,不需要输入其他的一路回车
在隐藏目录.ssh中找到 .ssh/id_rsa.pub ,复制其中的内容到github上的setting选择SSH and GPG keys选择New SSH key,title 自定义 将复制来的内容填入key中
ssh -T git@github.com//测试是否连接成功
操作
创建一个目录存放code,命名为git-code,进入git-code
- git init 初始化git仓库
- git add . 把当前的所有文件加入git仓库(或者某个文件)
- git commit -m “这里写提交注释” 提交到本地仓库
- git remote add origin 你的远程库地址 // 把本地库与远程库关联
- git push -u origin master // 第一次推送时
- git push origin master // 第一次推送后,直接使用该命令即可推送修改
- git pull origin master //提交有问题可以先拉取远程文件
- git log --stat //查看某次提交后所有改动的文件,不包含内容
- git revert ‘commitId’ //回滚
错误
- warning: CRLF will be replaced by LF in XXX . The file will have its original line endings in your working directory.
因为CRLF是Window下的换行符,而LF是Linux下的换行符,使用git的时候会自动转换CRLF为LF所以会警告,解决办法:
git config --global core.autocrlf false//关掉git的自动转换功能
- 当push代码时报错如:
! [rejected] master -> master (non-fast forward)
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 pull origin master --allow-unrelated-histories //--allow-unrelated-histories 把两段不相干的 分支进行强行合并
ps:当文件不重要或比较少的时候可以这样使用,当文件比较重要,或者差异比较大的时候,没有试过会不会造成一些隐患
本文详细介绍了在Linux系统中如何使用Git命令与GitHub进行连接,包括通过SSH方式生成公私钥对、配置GitHub账号、初始化本地仓库、推送代码到远程仓库等步骤。同时,文章还提供了常见错误的解决方案,如CRLF与LF换行符冲突、推送代码失败等问题。
1127

被折叠的 条评论
为什么被折叠?



