【四】Git & GitHub:联动操作
创建一个远程仓库
第一步:
第二步:
第三步:
第四步:得到存储库的一些信息
将本地存储库推送至云端
第一步:和云端存储库建立连接
-
git remote add origin https://github.com/acwink/github-basics.git- origin 是 URL的别名
第二步:将主分支重命名未main,因为master有歧视黑人的意思
-
git branch -M main
第三步:创建github token
第四步:本地添加用户凭证
第五步:简单的说是推送某一分支到云端
-
git push origin main
第六步:查看是否推送成功
提示:
-
git branch -a- 此命令可以列出本地和远程分支,本地会存储远程存储库分支的副本
-
git branch -r- 只列出本地分支
有个问题,第一次推送过后我们后面推送是失败的
-
git push # 推送 # 必须使用, 如果过远程仓库不存在该分支则会创建,先在本地创建在push到远程 git push origin 分支名-
fatal: The current branch main has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin main
-
在远程仓库创建分支
第一步:在这里输入要创建的分支名
第二步:将github上创建的分支,拉到本地
-
git ls-remote-
50f93ff53d3624f8c4db9f17a93d552fe03876e4 HEAD 22be85698855dfd9e41a659f0b4dcde2960614ca refs/heads/feature 6c7c25a6180879e5f7f1b6a3a066e761d41f1ed3 refs/heads/feature-remote 50f93ff53d3624f8c4db9f17a93d552fe03876e4 refs/heads/main - 本地保留的远程分支副本中没有feature-remote 因为它是在github上创建的
-
第三步:将分支拉取或拉取并合并
-
git fetch origin- 此命令会把服务器创建的分支拉取下来, 并保存到本地的远程副本中
-
git pull origin 分支-
拉去并合并远程分支到我们本地对应的分支中
-
From https://github.com/acwink/github-basics * branch main -> FETCH_HEAD Already up to date.
-
-
在本地创建可编辑跟踪远程分支的分支
-
git branch --track feature-remote-local(创建的本地分支名) origin/feature-remote(需要跟踪的远程分支名)- 此命令会创建一个跟踪远程分支的本地分支
然后我们在当前分支上添加一个文件并提交。切换到远程分支本地副本能够发现,我们的更改没有在此副本上生效。
我们必须给 feature-remote-local 上传到云端,当然执行的前提是 分支名称要一样,否则执行失败
-
git push- 此命令会将我们的更新带到远程分支上
如果我们在远程提交
-
git pull- 可以同步所有提交到本地
提示
-
git branch -vv- 能够看到分支更多的提示
将远程仓库克隆到本地
第一步: 获取仓库唯一URL
第二步:克隆到本地
-
git clone https://github.com/acwink/github-basics.git
解决问题推送过后和本地远程分支副本没有逻辑联系的问题
推送的时候我们应该使用
-
# git push -u origin 分支名称 git push -u origin feature-upstream-
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 remote: remote: Create a pull request for 'feature-upstream' on GitHub by visiting: remote: https://github.com/acwink/github-basics/pull/new/feature-upstream remote: To https://github.com/acwink/github-basics.git * [new branch] feature-upstream -> feature-upstream # 重点设置当前分支跟踪远程仓库本地副本分支 branch 'feature-upstream' set up to track 'origin/feature-upstream'.
-
删除分支
删除远程跟踪分支
-
删除本地跟踪的远程分支副本
-
git branch --delete --remotes origin/feature(远程分支)
-
-
删除远程的分支, 这个命令本地的远程分支副本也会被删除
-
git push origin --delete feature
-
撤销提交
-
先在本地撤销提交
-
git reset --hard HEAD~1
-
-
将分支push到远程
-
git push --force origin main - 这个命令一定要注意使用,因为可能会覆盖其他人已经完成的修改提交
-