cat ~/.ssh/id_ed25519.pub # Then select and copy the contents of the id_ed25519.pub file # displayed in the terminal to your clipboard
测试 SSH 连接
1 2
ssh -T git@github.com # Attempts to ssh to GitHub
可能会看到类似如下的 warning
1 2 3
> The authenticity of host 'github.com (IP ADDRESS)' can't be established. > RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8. > Are you sure you want to continue connecting (yes/no)?
kex_exchange_identification: Connection closed by remote host Connection closed by 198.18.0.63 port 22 fatal: Could not read from remote repository.
⚠️ 此时不能 push,如果远程仓库有本地仓库没有的文件,此时直接 push
会出错。
1 2 3 4 5 6 7 8 9 10
❯ git push -u origin main Enter passphrase for key '/Users/chan/.ssh/id_ed25519': To github.com:FacundoChan/Booking-System-Go.git ! [rejected] main -> main (fetch first) error: failed to push some refs to 'github.com:FacundoChan/Booking-System-Go.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
接着就是获取更新某个分支:
1 2 3 4 5 6 7 8 9 10
# 从远程仓库中获取某个分支的更新,再与本地指定的分支进行自动merge git pull origin main
# 上述指令相当于执行了以下指令 git fetch origin main # 更新到本地仓库 git checkout main git merge origin/main
# Print out the list of files and directories which will be removed (dry run) git clean -n -d # -n --dry-run: Don’t actually remove anything, just show what would be done. # Specify -d to have it recurse into such directories as well
# Delete the files from the repository git clean -f
# 列出历史提交记录 git log [--oneline] [--reverse] git show d601b90 # or d60 git show HEAD~2 # HEAD往前2个版本 git show 1dcc30 git show HEAD~1:bin/app.bin # 查看具体文件内容
git fetch git diff remote/branch # git fetch updates your tracking branches from the remote. # git diff will compare the remote branch with your local branch.
删除远程分支
不小心在 GitHub 删除了分支,但本地 remote/origin/..还有的情况:
git remote prune origin will remove all such stale
branches. That's probably what you'd want in most cases, but if you want
to just remove that particular remote-tracking branch, you should
do:
1
git branch -d -r origin/coolbranch
(The -r is easy to forget...)
-r in this case will "List or delete (if used with
-d) the remote-tracking branches." according to the Git
documentation found here: https://git-scm.com/docs/git-branch
X Y Meaning ------------------------------------------------- [ AMD] not updated M [ MTD] updated in index T [ MTD] type changed in index A [ MTD] added to index D deleted from index R [ MTD] renamed in index C [ MTD] copied in index [ MTARC] index and work tree matches [ MTARC] M work tree changed since index [ MTARC] T type changed in work tree since index [ MTARC] D deleted in work tree R renamed in work tree C copied in work tree ------------------------------------------------- D D unmerged, both deleted A U unmerged, added by us U D unmerged, deleted by them U A unmerged, added by them D U unmerged, deleted by us A A unmerged, both added U U unmerged, both modified ------------------------------------------------- ? ? untracked ! ! ignored -------------------------------------------------
版本回滚
1 2 3 4 5 6 7 8
# 回滚到具体版本 git reset --hard commit_id
# 回退上个版本 git reset --hard HEAD^
# 回退上上个版本 git reset --hard HEAD^^
删除后续 commits 并更新 main
分支
1 2 3 4 5
git branch backup-main git checkout backup-main git reset --hard 594a5d1 git branch -D main git branch -m backup-main main
cs144@cs144vm:~/sponge/build$ ssh -T git@github.com git@github.com: Permission denied (publickey). cs144@cs144vm:~/sponge/build$ ssh-add ~/.ssh/id_cs144.pub Could not open a connection to your authentication agent. cs144@cs144vm:~/sponge/build$ eval `ssh-agent -s` # run it first Agent pid 3323 cs144@cs144vm:~/sponge/build$ ssh-add ~/.ssh/id_cs144.pub # then execute `ssh-add` @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: UNPROTECTED PRIVATE KEY FILE! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Permissions 0644 for'/home/cs144/.ssh/id_cs144.pub' are too open. It is required that your private key files are NOT accessible by others. This private key will be ignored.
Push to a valid branch: If you find that the branch name in the
remote repository is different from what you expected, you will need to
adjust your push command accordingly. For example, if the branch name is
"main" in the remote repository, but your local branch is named
"master," you can push to the remote "main" branch using:
bash
Copy
1
git push my6.824 master:main
Make sure to replace "master" with the correct local branch name if
it's different in your case.