Git 和 K8s 入门
Git 命令流程
1.目标文件夹右键vscode打开,vscode内终端打开Git Bash
没有.git文件夹:git init 初始化
2.修改文件:资源管理器里修改文件名变绿,源代码管理-更改栏新增修改文件
工作区(workspace) — git add —> 暂存区(index) — git commit -m —> 本地仓库 — git push —> 远程仓库
3.git add (filename) / git add -A:修改文件从源代码管理的更改栏转入暂存更改栏,从工作区转入暂存区
源代码管理-暂存更改栏内文件名末尾的-,点击后文件从暂存更改栏转入更改栏
源代码管理-暂存更改栏末尾的-,点击后所有文件从暂存更改栏转入更改栏
4.git commit -m “提交信息”:修改文件离开暂存区到本地仓库,源代码管理的更改栏和暂存更改栏清空,资源管理器内修改文件名变白
5.git log --stat:查看提交历史
6.git checkout (filename):修改文件未git add,撤销源代码管理-更改/工作区的改动
7.git reset HEAD^/HEAD^1:修改文件已git commit,撤回提交,修改内容返回更改栏/工作区
8.源代码管理- FILE HISTORY:对比所有提交信息
本地 git clone 配置
法一:SSH
1
2
$ ssh-keygen
$ cat ~/.ssh/id_rsa.pub
复制公钥,添加到 https://github.com/settings/ssh/new 后git clone选择SSH即可
法二:HTTPS
https://github.com/settings/tokens 生成 personal access token 并及时保存
git clone 选择HTTPS方式要求输入用户名及 personal access token
为了避免每次拉取重复输入密码,git config --global credential.helper store 后输入用户名及 personal access token 会被保存,以后无需密码
还要全局修改邮箱 git config --global user.email "yuyao.jiang22@gmail.com" 和用户名 git config --global user.name "yuy4o"。当前仓库修改无需加 --global
修改后 全局:~/.gitconfig / C:\Users\Administrator\.gitconfig,当前项目:<PROJECT PATH>\.git\config 文件中增加以下字段
1
2
3
4
5
[user]
email = yuyao.jiang22@gmail.com
name = yuy4o
[credential]
helper = store
Git 命令和可视化图形对应
git init -> 源代码管理-初始化仓库
git add -> 源代码管理-更改栏内文件名末尾的+,点击后文件从更改栏转入暂存更改栏
源代码管理-更改栏末尾的+,点击后所有文件从更改栏转入暂存更改栏
git commit -> 源代码管理-消息栏输入提交信息,点击提交按钮
git log --stat ->安装拓展GitLens — Git supercharged,源代码管理-COMMITS栏
git checkout -> 源代码管理-更改栏内文件名 / 更改栏末尾的放弃更改键
git reset HEAD^ ->源代码管理-COMMITS栏,右键undo commit,修改内容返回暂存更改栏,点击-,返回更改栏
分支命令
git branch:列举所有本地分支
git branch -a:列举本地分支和远程分支
git branch -D (branchname):删除分支
git checkout -b (branchname):以当前分支为基础新建分支
git checkout (branchname):切换分支
源代码管理-COMMITS栏末尾切换分支+刷新按钮:查看每个分支的commit
git merge (branchname):先回到主分支,再合并分支
源代码管理-合并更改栏:点击+, 文件转入暂存更改栏
源代码管理-暂存更改栏:点击stash changes,文件转入源代码管理-STASHES,右键文件选择apply changes,文件返回源代码管理-更改栏
git merge --abort:git merge文件传入源代码管理-合并更改栏后撤销合并
上传其他远程仓库 (repo2) 流程
1.删除.git文件夹,git init:初始化仓库
2.git remote add (repo2 HEAD branchname) (repo2 link):配置远程仓库
3.git branch -M (remote branchname):自定义上传到repo2的remote branchname
4.git push -u (repo2 HEAD branchname) (remote branchname) 等效于 git push --set-upstream (repo2 HEAD branchname) (remote branchname):将本地分支(remote branchname)上传至repo2新分支(remote branchname)
Git LFS
brew install git-lfs 安装git-lfs
git lfs install 开启lfs功能
git lfs track 大文件追踪,如git lfs track "*.png" 追踪所有后缀为png的文件。git lfs track 73c5245e75b21d04b438b8384163061e4673fa92 例如 "remote: warning: File 73c5245e75b21d04b438b8384163061e4673fa92 is 50.46 MB; this is larger than GitHub's recommended maximum file size of 50.00 MB"
git lfs track 查看现有的文件追踪
提交代码需要add 并提交 gitattributes 文件至仓库. 它保存了文件的追踪记录。1.提交后运行 git lfs ls-files 可以显示当前跟踪的文件列表 2.将代码 push 到远程仓库后,LFS 跟踪的文件会以『Git LFS』的形式显示 3.clone 时,使用git clone 或 git lfs clone 均可
其他Git命令
git remote rename origin (NAME):remotes/origin/... 变成 remotes/NAME/...
git diff:对比本地和远程变化,see changes made from previous commit
git clone -b (remote branchname) (repo link):克隆指定远程分支
git clone --bare (repo link):git clone 克隆所有分支
git checkout -b (local branchname) (origin/remote branchname):复制远程分支
git checkout -t (origin/remote branchname):复制和远程分支同名的本地分支
git fetch/pull (NAME/default = origin) (remote branchname):(local branchname):远程分支拉到本地分支
git push (NAME/default = origin) (local branchname):(remote branchname):本地分支推到远程分支
git push (NAME/default = origin) –d (remote branchname):删除远程分支
git config core.ignorecase false:工作路径下运行,关闭git忽略大小写的默认设定。针对git无法检测到文件夹字母大小写的更改
ssh -T git@github.com:链接到 github 的远程 ssh
发现一
git rebase 报错原因:工作区有unstaged changes或暂存区有uncommitted changes,导致本地和远程文件不一致
如果改动在工作区,git rebase/git pull —rebase报错:error: cannot rebase: You have unstaged changes. error: Please commit or stash them.
如果改动在暂存区,git rebase/git pull —rebase报错:error: cannot pull with rebase: Your index contains uncommitted changes. error: cannot rebase: Your index contains uncommitted changes. error: please commit or stash them.
上述两种情况git pull都显示Already up to date,但不会更新远程到本地
解决:git stash:移除工作区的unstaged changes并保存到源代码管理-STASHES里,此时本地和远程文件一致
git rebase
git stash pop:把源代码管理-STASHES里保存的改动释放回工作区
git stash drop 丢弃储存
发现二
如何撤销git add (改动或删除的文件)操作,从暂存区回到工作区:
解决:git add + git commit 相比直接git commit -a 是分阶段的staged
先git restore --staged (filename):to unstage,再 git restore (filename): to discard changes in working directory
发现三
以上将本地分支上传到repo2的新分支后,还想将同一本地分支上传到repo2的HEAD分支,会报错(HEAD分支是仓库的首个分支/主分支,默认main/master, 此处happy)
解决:该本地分支与首次上传的新建分支关联,与repo2的其他分支无关。例如本地happy2分支关联远程happy2分支,不用考虑如何把本地happy2分支上传到repo2的HEAD分支如happy上。可以在本地happy分支上git merge happy2,再把本地happy1分支推到repo2的HEAD分支/happy分支


Comments powered by Disqus.