git使用记录
工作区(写代码)—–git add——> 暂存区(临时存储) ———git commit———>本地库(历史版本)
初始化本地库
git init
查看本地库状态(红色的话代表在本地没有进行任何操作)
git status
查看版本信息 git reflog
Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) $ git reflog 7f31511 (HEAD -> master) HEAD@{0}: commit: second 1a159fc HEAD@{1}: commit (initial): hello |
git log 查看版本详细信息(按Q退出)
Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) $ git log commit 7f3151135bd45c405bfd5cc4c4d1243ac13f0c82 (HEAD -> master) Author: Wang <123456789@qq.com> Date: Sat Sep 17 22:01:01 2022 +0800 second commit 1a159fc59e65c70e92f9c9123ca76453d75a1fa6 Author: Wangi <123456789@qq.com> Date: Sat Sep 17 21:57:13 2022 +0800 hello |
git add .
把当前所有修改的文件包含进来(git status红色的内容全部变成绿色)
git add . 执行报如下错误
git add . warning: You ran 'git add' with neither '-A (--all)' or '--ignore-removal', whose behaviour will change in Git 2.0 with respect to paths you removed. Paths like 'public/h5/h5.zip' that are removed from your working tree are ignored with this version of Git. * 'git add --ignore-removal <pathspec>', which is the current default, ignores paths you removed from your working tree. * 'git add --all <pathspec>' will let you also record the removals. |
原因是因为有删除的文件,没有跟新到本地的仓库而导致的。执行 git add -A即可
git rm -r –cached filename删除暂存区和版本库的文件,但保留了工作区的文件。如下操作:
Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) $ git add hello.txt warning: LF will be replaced by CRLF in hello.txt. The file will have its original line endings in your working directory Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) $ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: hello.txt Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) $ git rm --cached hello.txt rm 'hello.txt' Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) $ git add . warning: LF will be replaced by CRLF in hello.txt. The file will have its original line endings in your working directory Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) $ git commit -m "hello" [master (root-commit) 1a159fc] hello 1 file changed, 2 insertions(+) create mode 100644 hello.txt Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) $ git status On branch master nothing to commit, working tree clean |
穿梭到某一个版本
git reflog + $ git reset –hard
Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) $ git reflog 7f31511 (HEAD -> master) HEAD@{0}: commit: second 1a159fc HEAD@{1}: commit (initial): hello Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) $ git reset --hard 1a159fc HEAD is now at 1a159fc hello git push -f -u origin master (推送到远程库) Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) $ git reflog 1a159fc (HEAD -> master) HEAD@{0}: reset: moving to 1a159fc 7f31511 HEAD@{1}: commit: second 1a159fc (HEAD -> master) HEAD@{2}: commit (initial): hello |
正常合并分支
Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) $ git merge test Updating 1a159fc..5c928ae Fast-forward hello.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) |
查看当前所有远程地址别名
git remote -v |
git关联远程库
git push -u origin master 如果当前分支与多个主机存在追踪关系,那么这个时候-u选项会指定一个默认主机,这样后面就可以不加任何参数使用git push,此命令将本地的master分支推送到origin主机,同时指定origin为默认主机,后面就可以不加任何参数使用git push了。
git remote add 别名 远程地址 |
Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) $ git branch -m main # 重命名分支 Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (main) $ git remote add origin https://gitee.com/WangShouwei/ceshigit.git #添加远程库 Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (main) $ git push -u origin "main" warning: ----------------- SECURITY WARNING ---------------- warning: | TLS certificate verification has been disabled! | warning: --------------------------------------------------- warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information. warning: ----------------- SECURITY WARNING ---------------- warning: | TLS certificate verification has been disabled! | warning: --------------------------------------------------- warning: HTTPS connections may not be secure. See https://aka.ms/gcmcore-tlsverify for more information. Enumerating objects: 24, done. Counting objects: 100% (24/24), done. Delta compression using up to 4 threads Compressing objects: 100% (12/12), done. Writing objects: 100% (24/24), 1.83 KiB | 469.00 KiB/s, done. Total 24 (delta 3), reused 0 (delta 0), pack-reused 0 remote: Powered by GITEE.COM [GNK-6.4] remote: Create a pull request for 'main' on Gitee by visiting: remote: https://gitee.com/WangShouwei/ceshigit/pull/new/WangShouwei:main...WangShouwei:master To https://gitee.com/WangShouwei/ceshigit.git * [new branch] main -> main Branch 'main' set up to track remote branch 'main' from 'origin'. |
报错unable to access ‘https://github.com/nhn/raphael.git/’: OpenSSL SSL_read: Connection was reset, errno 10054
npm ERR! Error while executing: npm ERR! E:\install\Git\mingw64\bin\git.EXE ls-remote -h -t https://github.com/nhn/raphael.git npm ERR! npm ERR! fatal: unable to access 'https://github.com/nhn/raphael.git/': OpenSSL SSL_read: Connection was reset, errno 10054 npm ERR! npm ERR! exited with error code: 128 |
解决方案
git config –global http.sslverify “false”
git 分支查看与切换
新建分支
Administrator@WIN-I70ME5UDN05 MINGW64 /d/frontend/vue-zhongshan-cloud (master) $ git branch shuiwu |
查看所有分支(*号的表示当前使用分支)
Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) $ git branch -a * master test |
查看所有分支
Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) $ git branch -v * master 1a159fc hello test 1a159fc hello |
分支名重命名
Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) $ git branch -m main |
切换分支
Administrator@WIN-I70ME5UDN05 MINGW64 /d/frontend/vue-zhongshan-cloud (master) $ git checkout master Already on 'master' Your branch is based on 'origin/master', but the upstream is gone. (use "git branch --unset-upstream" to fixup) |
当前分支状态
Administrator@WIN-I70ME5UDN05 MINGW64 /d/frontend/vue-zhongshan-cloud (master) $ git status On branch master Your branch is based on 'origin/master', but the upstream is gone. (use "git branch --unset-upstream" to fixup) nothing to commit, working tree clean |
.git\HEAD文件 描述git当前指向分支
ref: refs/heads/test
.git\refs\heads\test文件 当前分支指向版本
5c928ae5e8cdfeb0a0336e9eeb5759c75d6188a7
合并分支冲突示例
Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) $ git merge test Auto-merging hello.txt CONFLICT (content): Merge conflict in hello.txt Automatic merge failed; fix conflicts and then commit the result. Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master|MERGING) $ vim hello.txt Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master|MERGING) $ git status On branch master You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both modified: hello.txt no changes added to commit (use "git add" and/or "git commit -a") # 修改冲突后提交 Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master|MERGING) $ git add . Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master|MERGING) $ git commit -m "hello.txt合并" [master 86a3310] hello.txt合并 Administrator@WIN-I70ME5UDN05 MINGW64 ~/Desktop/git (master) |
Git如何合并分支到主干及合并主干到分支参考
https://www.cnblogs.com/loytime/p/13530163.html
Git 全局设置:
git config --global user.name "WangShouwei" git config --global user.email "412198579@qq.com" |
查看git用户签名信息
C:\Users\Administrator\.gitconfig
创建 git 仓库:
mkdir shuiwu cd shuiwu git init touch README.md git add README.md git commit -m "first commit" git remote add origin https://gitee.com/WangShouwei/shuiwu.git git push -u origin master |
已有仓库?
cd existing_git_repo git remote add origin https://gitee.com/WangShouwei/shuiwu.git git push -u origin master |
如果项目之前已经建立了,但是现在想要不上传某个文件,而上面的步骤不起作用时,就要进行以下步骤
首先在.gitignore文件中输入不想上传的文件或者文件夹名字。
然后打开git bash,然后在里面输入以下命令:
git rm -r --cached . # 发现有不该提交的文件已经提交后,仅仅在.gitignore中加入忽略是不行的。这个时候需要执行 git rm -r --cached git add . git commit -m '上传文件' git push |
如果只是忽略本地变更
git update-index --assume-unchanged ./config/database.php #忽略跟踪 git update-index --no-assume-unchanged ./config/database.php #恢复跟踪 |
有时,我们可能希望在仓库中保留一个文件,但不再希望跟踪其更改。我们可以使用带有 –skip-worktree 选项的 git update-index 命令来实现这一点。
命令 git update-index 的 –skip-worktree 选项使 Git 假装文件的版本是最新的,而是从索引版本中读取。这对于配置文件特别有用。
我们可能在仓库中有一些具有默认值或生产值的配置文件,我们可能会根据需要对其进行一些更改,但不想提交这些更改
。用于命令 git update-index 的 –skip-worktree 选项对于此类目的非常方便。
git update-index --skip-worktree [FILE] |
取消 skip-worktree
git update-index --no-skip-worktree [FILE] |
添加被忽略的文件到远程仓库中,如一些配置文件database.php 已被忽略掉,又想上传到git中,可以使用以下方法强制添加
git add -f . 添加所有文件,包括被.gitignore忽视的文件 |
git pull时报错 Your local changes to the following files would be overwritten by merge
error: Your local changes to the following files would be overwritten by merge:
application/database.php
Please commit your changes or stash them before you merge.
Aborting
报错原因
该报错在git pull时出现,一句话解释就是你在本地改动了代码但是还没有提交,此时再拉取最新代码,远程代码和你当前的本地代码发生冲突!(注意有冲突时才会提示,如果没有冲突,则git pull成功,因为git pull实质上就是一个远程分支merge到本地分支过程。
解决方案1 本地代码commit后再pull(推荐)
//先把当前修改的工作区内容提交了 git add . git commit //拉取最新代码,这里就相当于两个已提交分支的合并了,有冲突解决冲突,没冲突就pull成功 git pull //解决完冲突后,继续完成自己的本地代码 //完成以后,add/commit/push三连到远程库 git add git commit git push ***这样就实现了开发过程中,将远程最新改动合到了我本地,然后我基于最新代码接着开发 ***该方法的缺点是会多出一条额外提交记录(pull之前的这次提交) |
解决方案2 git stash
//备份当前的工作区的内容,让工作区变为和上次提交的内容一致。同时,将当前的工作区内容保存到Git栈中 git stash //拉取最新代码(依然有可能有冲突,如果有就手动解决冲突) git pull //从Git栈中读取最近一次保存的内容,恢复工作区的相关内容 //这样就相当于是基于远程库最新版本修改了 git stash pop ***注意,最后恢复暂存区的时候可能与最新代码有冲突,此时仍需要手动解决冲突 ***该方法虽然没有多余提交记录,但稍微麻烦一点 |