把本地的代码同步到github

场景

自己使用的一些脚本,放到github可以更方便备份和存取.
在windows / wsl 安装github.
如下场景在Windows11/wsl archlinux, 编辑windows d盘工作目录的脚本,同步到github.

命令

git init
git status
git add .
git commit -m ‘init version’
git config
git branch
git push
ssh-keygen
ssh-addkey
ssh

参考

https://github.com/Git-Commands
https://www.youtube.com/watch?v=tRZGeaHPoaw&t=999s
https://www.youtube.com/watch?v=RGOj5yH7evk&t=1553s

本地管理操作

如下操作是都在本机, 没有连接到github… 对文件的修改,提交,restore, 都在本地(.git)目录.
项目名字为 autodg.

git 文件的状态

本地->staging ->commit.
add , 从本地发放到staging.
commit, 从staging 提交. 提交后别人可见.

cd autodg

git config --global user.name yourname

git config --global user.email yourname@gmail.com

git config --global init.default branch main

git init

this cmd will create .git directory

git status

should show no files.

git add .

把当前目录的文件提交到staging阶段.
一个点,是当前目录的所有文件.

git status

shows all files in staging.

git commit -m ‘initial version’

commit all files. -m 对这次commit加注释

git status

show no files need to be commited.

vi readme.md

create a readme.md files

git status

shows readme.md is untracked.

git add readme.md

or just git add .把readme.md加到staging.

git status

shows readme.md can be committed.

git commit -m ‘added readme.md’

把readme提交.

vi readme.txt

add one line “new line”
修改readme.txt.

git status

shows readme.md is changed.

git diff

show the difference between old and new version.
显示增加的一行

git restore

restore the old version(with new line)
新加的一行被放弃. 恢复到前一个版本.

git log

查看提交的日志
check all the commits.
git log --oneline 显示简要日志
show simple one line comments
git log -p 显示提交的差异
git help log(get more help of log)

git branch bugfix

建立分支版本,比如修复bug的分支.
git branch to check all the branches
git switch bugfix , switch to another bransh.

git merge -m ‘merge comment’ bugfix

修复后跟主版本合并.(merge)
git merge -m ‘merge with bugfix in t2.sh’ bugfix
merge bugfix branch to master

git branch -d bugfix

after bugfix is merged, delete the bugfix branch.
git branch to check it.
删除bugfix这个分支.

git switch -c newbranch

create a new branch “newbranch” and switch to it.
建立一个新分支并切换到它

建立ssh认证

github不支持https密码认证,要用git提交到github,必须建立ssh认证.比如rsa 密钥.
步骤如下 setup ssh authentication.
ssh-keygen

  1. ssh-keygen -t rsa -b 4096 -C “yourname@gmail.com”
    create ssh key. just press return when prompted.
  2. cd ~/.ssh/
    cat id_rsa.pub , copy to pasteboard.
  3. 打开浏览器, 登录到GitHub, setting(在最右边图标点击,最下边) -> ssh key. add sshkey. paste it.
  4. evel “$(ssh-agent -s)”
  5. ssh-add ~/ssh/id_rsa
  6. ssh git@github.com
    ------return -----
    Hi yourname ! You’ve successfully authenticated, but GitHub does not provide shell access.
    Connection to github.com closed.

说明ssh已经建好了.

from local to github

如下把本地的文件上传到github网站.
git remote add origin git@github.com:yourname/autodg.git
git branch -M main
git push -u -f origin main
git push --all (all branches)

排错

错误1

错误信息

[nome@LIYUEFU-T14 src-test]$ git push -u origin main
Username for ‘https://github.com’: yoruname@gmail.com
Password for ‘https://yourname@gmail.com@github.com’:
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for ‘https://github.com/yourname/autodg.git/’

解决

使用ssh链接,而不是https链接. 需要先建立ssh密钥. 参看上面.

git remove origin
删除https的链接
git remote add origin git@github.com:yourname/autodg.git
使用ssh的链接
重新push
############error fix###############

错误2

错误信息

[nome@LIYUEFU-T14 autodg]$ git push -u origin main
To github.com:yourname/autodg.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to ‘github.com:yourname/autodg.git’
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use ‘git pull’ before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push --help’ for details.

解决

使用-f 选项.
[nome@LIYUEFU-T14 autodg]$ git push -f -u origin main
Enumerating objects: 76, done.
Counting objects: 100% (76/76), done.
Delta compression using up to 16 threads
Compressing objects: 100% (71/71), done.
Writing objects: 100% (76/76), 82.41 KiB | 102.00 KiB/s, done.
Total 76 (delta 10), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (10/10), done.
To github.com:yourname/autodg.git

  • e93f093…7c8fc14 main -> main (forced update)
    branch ‘main’ set up to track ‘origin/main’.
  • 29
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值