“Git基础及常用命令”的版本间的差异
跳到导航
跳到搜索
(未显示同一用户的38个中间版本) | |||
第1行: | 第1行: | ||
+ | =date= | ||
+ | Sep 2024 | ||
=常用命令= | =常用命令= | ||
+ | |||
+ | [https://liaoxuefeng.com/books/git/conclusion/git-cheat-sheet.pdf Git Cheat Sheet 打印出来吧] | ||
==日常使用== | ==日常使用== | ||
+ | [https://www.runoob.com/w3cnote/git-five-minutes-tutorial.html Git五分钟教程] | ||
<pre> | <pre> | ||
+ | |||
+ | |||
+ | 添加 key 后 clone | ||
+ | |||
+ | #配置 | ||
+ | git config --global user.name "evan886" | ||
+ | git config --global user.email "[email protected]" | ||
+ | |||
+ | |||
#日常用得最多就这几个了 | #日常用得最多就这几个了 | ||
git pull origin master | git pull origin master | ||
第7行: | 第21行: | ||
git commit -m " " | git commit -m " " | ||
git push origin master | git push origin master | ||
+ | |||
+ | note | ||
+ | 当我们修改了很多文件,而不想每一个都add,想commit自动来提交本地修改,我们可以使用-a标识。 | ||
+ | |||
+ | git commit -a -m "Changed some files" | ||
第22行: | 第41行: | ||
git remote add origin [email protected]:evan886/learn-english.git | git remote add origin [email protected]:evan886/learn-english.git | ||
git push -u origin master | git push -u origin master | ||
+ | |||
+ | …or import code from another repository | ||
+ | |||
+ | You can initialize this repository with code from a Subversion, Mercurial, or TFS project. | ||
+ | |||
+ | |||
+ | Git 新加了两个命令: | ||
+ | |||
+ | git switch用于切换分支 | ||
+ | git restore用于恢复原样 | ||
+ | |||
+ | 两者可以取代 git checkout。 | ||
+ | |||
+ | |||
+ | #创建分支命令: | ||
+ | git branch (branchname) | ||
+ | |||
+ | #切换分支命令: | ||
+ | git checkout (branchname) | ||
+ | |||
+ | git branch dev | ||
+ | evan@myxps:~/github/learn-c$ git checkout dev | ||
+ | M co | ||
+ | M turtle/src/3.c | ||
+ | 切换到分支 'dev' | ||
+ | |||
+ | git 退回上一个版本 | ||
+ | git reset --hard HEAD^ | ||
+ | |||
+ | 退回单个文件 | ||
+ | git log p128.groovy | ||
+ | ➜ devops6-shared-library git:(main) git checkout 7a7dfa905067e92f913e593f2cdcfee87ba70538 p128.groovy | ||
+ | |||
</pre> | </pre> | ||
第41行: | 第93行: | ||
$ git tag | $ git tag | ||
v1.0 | v1.0 | ||
+ | |||
+ | ### | ||
+ | git log | ||
+ | ➜ myweb git:(main) git tag 1.0 | ||
+ | ➜ myweb git:(main) git tag | ||
+ | ➜ myweb git:(main) git tag -a v1.0 -m "my version 1.0" | ||
+ | |||
+ | (main) git tag | ||
+ | ➜ myweb git:(main) git show 1.0 | ||
+ | ➜ myweb git:(main) git show v1.0 | ||
+ | |||
</pre> | </pre> | ||
+ | |||
+ | ==.gitignore 文件——如何在 Git 中忽略文件和文件夹== | ||
+ | <pre> | ||
+ | |||
+ | adj110 是我django mount 到xps的目录 | ||
+ | |||
+ | git:(master) cat .gitignore | ||
+ | project/laoqi/adj110 | ||
+ | |||
+ | </pre> | ||
+ | |||
=git将本地代码提交到远程仓库= | =git将本地代码提交到远程仓库= | ||
<pre> | <pre> | ||
第54行: | 第128行: | ||
</pre> | </pre> | ||
[https://blog.csdn.net/nhgxxyy198990/article/details/80433005 git将本地代码提交到远程仓库] | [https://blog.csdn.net/nhgxxyy198990/article/details/80433005 git将本地代码提交到远程仓库] | ||
+ | |||
+ | =分支branch= | ||
+ | <pre> | ||
+ | in) git branch testing #创建 | ||
+ | ➜ myweb git:(main) git branch | ||
+ | * main | ||
+ | testing | ||
+ | (END) | ||
+ | |||
+ | git checkout testing | ||
+ | 切换到分支 'testing' | ||
+ | |||
+ | git branch | ||
+ | main | ||
+ | * testing | ||
+ | (END) | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | =clone 指定分支= | ||
+ | -b, --branch <分支> 检出 <分支> 而不是远程 HEAD | ||
+ | git clone -b day-08 https://github.com/michaelliao/awesome-python3-webapp.git day8 | ||
=git分支的合并= | =git分支的合并= | ||
在git中,可以使用git merge 和git rebase两个命令来进行分支的合并 | 在git中,可以使用git merge 和git rebase两个命令来进行分支的合并 | ||
+ | [https://blog.csdn.net/hudashi/article/details/7668798 git分支的合并] | ||
+ | |||
+ | =合并分支到master/main上 = | ||
+ | <pre> | ||
+ | |||
+ | 现在在dev分支上,可以用下面命令查看当前分支 | ||
+ | git branch | ||
+ | |||
+ | 刚开发完项目,执行了下列命令 | ||
+ | git add . | ||
+ | git commit -m 'dev' | ||
+ | git push -u origin dev | ||
+ | |||
+ | 把dev分支的代码合并到master分支上 该如何? | ||
+ | 首先切换到master分支上 | ||
+ | git checkout master | ||
+ | |||
+ | 多人开发的话 需要把远程master上的代码pull下来 | ||
+ | git pull origin master | ||
+ | |||
+ | |||
+ | 我们把dev分支的代码合并到master上 | ||
+ | git merge dev | ||
+ | |||
+ | 然后查看状态 | ||
+ | git status | ||
+ | |||
+ | |||
+ | On branch master | ||
+ | Your branch is ahead of 'origin/master' by 12 commits. | ||
+ | (use "git push" to publish your local commits) | ||
+ | nothing to commit, working tree clean | ||
+ | |||
+ | 上面的意思就是你有12个commit,需要push到远程master上 | ||
+ | 执行下面命令即可 | ||
+ | |||
+ | git push origin master | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | =解决冲突 = | ||
+ | <pre> | ||
+ | 准备新的feature1分支,继续我们的新分支开发: | ||
+ | |||
+ | $ git switch -c feature1 | ||
+ | Switched to a new branch 'feature1' | ||
+ | 修改readme.txt最后一行,改为: | ||
+ | |||
+ | Creating a new branch is quick AND simple. | ||
+ | 在feature1分支上提交: | ||
+ | |||
+ | $ git add readme.txt | ||
+ | |||
+ | $ git commit -m "AND simple" | ||
+ | [feature1 14096d0] AND simple | ||
+ | 1 file changed, 1 insertion(+), 1 deletion(-) | ||
+ | 切换到master分支: | ||
+ | |||
+ | $ git switch master | ||
+ | Switched to branch 'master' | ||
+ | Your branch is ahead of 'origin/master' by 1 commit. | ||
+ | (use "git push" to publish your local commits) | ||
+ | Git还会自动提示我们当前master分支比远程的master分支要超前1个提交。 | ||
+ | |||
+ | 在master分支上把readme.txt文件的最后一行改为: | ||
+ | |||
+ | Creating a new branch is quick & simple. | ||
+ | 提交: | ||
+ | |||
+ | $ git add readme.txt | ||
+ | $ git commit -m "& simple" | ||
+ | [master 5dc6824] & simple | ||
+ | 1 file changed, 1 insertion(+), 1 deletion(-) | ||
+ | 现在,master分支和feature1分支各自都分别有新的提交,变成了这样: | ||
+ | |||
+ | git-br-feature1 图片没显示 | ||
+ | |||
+ | 这种情况下,Git无法执行“快速合并”,只能试图把各自的修改合并起来,但这种合并就可能会有冲突,我们试试看: | ||
+ | |||
+ | $ git merge feature1 | ||
+ | Auto-merging readme.txt | ||
+ | CONFLICT (content): Merge conflict in readme.txt | ||
+ | Automatic merge failed; fix conflicts and then commit the result. | ||
+ | 果然冲突了!Git告诉我们,readme.txt文件存在冲突,必须手动解决冲突后再提交。git status也可以告诉我们冲突的文件: | ||
+ | |||
+ | $ git status | ||
+ | On branch master | ||
+ | Your branch is ahead of 'origin/master' by 2 commits. | ||
+ | (use "git push" to publish your local commits) | ||
+ | |||
+ | 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: readme.txt | ||
+ | |||
+ | no changes added to commit (use "git add" and/or "git commit -a") | ||
+ | 我们可以直接查看readme.txt的内容: | ||
+ | |||
+ | Git is a distributed version control system. | ||
+ | Git is free software distributed under the GPL. | ||
+ | Git has a mutable index called stage. | ||
+ | Git tracks changes of files. | ||
+ | <<<<<<< HEAD | ||
+ | Creating a new branch is quick & simple. | ||
+ | ======= | ||
+ | Creating a new branch is quick AND simple. | ||
+ | >>>>>>> feature1 | ||
+ | Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容,我们修改如下后保存: | ||
+ | |||
+ | Creating a new branch is quick and simple. | ||
+ | 再提交: | ||
+ | |||
+ | $ git add readme.txt | ||
+ | $ git commit -m "conflict fixed" | ||
+ | [master cf810e4] conflict fixed | ||
+ | 现在,master分支和feature1分支变成了下图所示: | ||
+ | |||
+ | git-br-conflict-merged | ||
+ | |||
+ | 用带参数的git log也可以看到分支的合并情况: | ||
+ | |||
+ | $ git log --graph --pretty=oneline --abbrev-commit | ||
+ | * cf810e4 (HEAD -> master) conflict fixed | ||
+ | |\ | ||
+ | | * 14096d0 (feature1) AND simple | ||
+ | * | 5dc6824 & simple | ||
+ | |/ | ||
+ | * b17d20e branch test | ||
+ | * d46f35e (origin/master) remove test.txt | ||
+ | * b84166e add test.txt | ||
+ | * 519219b git tracks changes | ||
+ | * e43a48b understand how stage works | ||
+ | * 1094adb append GPL | ||
+ | * e475afc add distributed | ||
+ | * eaadf4e wrote a readme file | ||
+ | 最后,删除feature1分支: | ||
+ | |||
+ | $ git branch -d feature1 | ||
+ | Deleted branch feature1 (was 14096d0). | ||
+ | 工作完成。 | ||
+ | |||
+ | 小结 | ||
+ | 当Git无法自动合并分支时,就必须首先解决冲突。解决冲突后,再提交,合并完成。 | ||
+ | |||
+ | 解决冲突就是把Git合并失败的文件手动编辑为我们希望的内容,再提交。 | ||
+ | |||
+ | 用git log --graph命令可以看到分支合并图。 | ||
+ | |||
+ | </pre> | ||
+ | https://www.liaoxuefeng.com/wiki/896043488029600/900004111093344 | ||
+ | |||
+ | == Rebase== | ||
+ | |||
+ | https://liaoxuefeng.com/books/git/branch/rebase/index.html | ||
+ | =usage first= | ||
+ | <pre> | ||
+ | 添加key后 | ||
+ | |||
+ | |||
+ | #配置 | ||
+ | git config --global user.name "evan886" | ||
+ | git config --global user.email "[email protected]" | ||
+ | </pre> | ||
+ | |||
+ | = error: 推送一些引用到 'https://github.com/xxx/xxx' 失败= | ||
+ | <pre> | ||
+ | error: 推送一些引用到 'https://github.com/xxx/xxx' 失败 | ||
+ | |||
+ | 提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支。 | ||
+ | |||
+ | 提示:再次推送前,先与远程变更合并(如 'git pull ...')。详见 | ||
+ | |||
+ | 提示:'git push --help' 中的 'Note about fast-forwards' 小节。 | ||
+ | |||
+ | 原因; | ||
+ | |||
+ | 这个问题是远程仓库与本地不同步引起的 | ||
+ | |||
+ | 解决方案: | ||
+ | 因为当前分支的最新提交落后于其对应的远程分支,所以我们先从远程库fetch到更新再和本地库合并,之后就可以git push操作了。 | ||
+ | |||
+ | git fetch origin | ||
+ | git merge origin/main | ||
+ | </pre> | ||
+ | =usage from gitlab= | ||
+ | <pre> | ||
+ | Git global setup | ||
+ | git config --global user.name "Administrator" | ||
+ | git config --global user.email "[email protected]" | ||
+ | |||
+ | Create a new repository | ||
+ | git clone [email protected]:root/monitor.git | ||
+ | cd monitor | ||
+ | git switch -c main | ||
+ | touch README.md | ||
+ | git add README.md | ||
+ | git commit -m "add README" | ||
+ | git push -u origin main | ||
+ | |||
+ | Push an existing folder | ||
+ | cd existing_folder | ||
+ | git init --initial-branch=main | ||
+ | git remote add origin [email protected]:root/monitor.git | ||
+ | git add . | ||
+ | git commit -m "Initial commit" | ||
+ | git push -u origin main | ||
+ | |||
+ | Push an existing Git repository | ||
+ | cd existing_repo | ||
+ | git remote rename origin old-origin | ||
+ | git remote add origin [email protected]:root/monitor.git | ||
+ | git push -u origin --all | ||
+ | git push -u origin --tags | ||
+ | </pre> | ||
+ | |||
+ | =git 基础安装使用= | ||
+ | |||
+ | ==git 服务器搭建== | ||
+ | <pre> | ||
+ | |||
+ | |||
+ | |||
+ | 服务器上 | ||
+ | |||
+ | apt-get install libcurl4-gnutls-dev libexpat1-dev gettext \ | ||
+ | libz-dev libssl-dev | ||
+ | |||
+ | apt-get install git | ||
+ | |||
+ | git --version | ||
+ | git version 2.20.1 | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | 使用我们指定目录作为Git仓库。 | ||
+ | |||
+ | git init newrepo | ||
+ | useradd git #也可以让这个用户不可以登录,为了安全嘛 | ||
+ | |||
+ | |||
+ | #试过 这个是成功的 | ||
+ | |||
+ | mkdir testrepo | ||
+ | |||
+ | root@cailuw-test:/data# chown -R git.git testrepo/ | ||
+ | root@cailuw-test:/data# cd testrepo/ | ||
+ | root@cailuw-test:/data/testrepo# git init --bare test.git | ||
+ | Initialized empty Git repository in /data/testrepo/test.git/ | ||
+ | |||
+ | 以上命令Git创建一个空仓库,服务器上的Git仓库通常都以.git结尾。然后,把仓库所属用户改为git: | ||
+ | root@cailuw-test:/data/testrepo# chown -R git.git test.git/ | ||
+ | |||
+ | #添加key | ||
+ | mkdir /home/git/.ssh -p | ||
+ | cp /root/.ssh/authorized_keys /home/git/.ssh/ | ||
+ | |||
+ | chown -R git.git /home/git/.ssh/authorized_keys | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | client 在你的 客户端机器上 | ||
+ | |||
+ | |||
+ | ssh -T git账号名@服务器IP #客户端验证连接 | ||
+ | ssh -T [email protected] | ||
+ | |||
+ | git clone [email protected]:/data/testrepo/test.git | ||
+ | 正克隆到 'test'... | ||
+ | warning: 您似乎克隆了一个空仓库。 | ||
+ | |||
+ | |||
+ | #配置 | ||
+ | git config --global user.name "evan886" | ||
+ | git config --global user.email "[email protected]" | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | [https://www.runoob.com/git/git-server.html Git 服务器搭建] | ||
+ | [http://blog.linuxchina.net/2015/04/11/%e5%9c%a8centosrhel%e4%b8%8a%e6%ba%90%e7%a0%81%e5%ae%89%e8%a3%85git/ 在CentOS/RHEL上源码或者 yum 安装Git 以及 linux and win client 使用 ] | ||
+ | |||
+ | [https://blog.csdn.net/github_36878269/article/details/80967388 Git本地远程仓库的搭建(局域网内也可提交)] | ||
+ | |||
+ | [https://blog.csdn.net/meltsnow/article/details/95949485?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param Git本地仓库的搭建及使用] | ||
+ | |||
+ | [https://blog.csdn.net/qq_33598419/article/details/94392074?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param | ||
+ | 使用Git搭建自己的私有/个人Git仓库 | ||
+ | ] | ||
+ | |||
+ | http://blog.linuxchina.net/2015/06/18/how-to-use-git-%e8%bd%ac/ | ||
+ | |||
=git usage on win= | =git usage on win= | ||
<pre> | <pre> | ||
第100行: | 第493行: | ||
</pre> | </pre> | ||
− | =Git自动账号密码填充= | + | =Git自动账号密码填充 git pull记住用户名和密码= |
<pre> | <pre> | ||
+ | 2021 | ||
+ | 进入项目目录 | ||
+ | |||
+ | git config --global credential.helper store | ||
+ | |||
+ | 然后会生成一个本地文件用于记录用户名和密码,这个文件我们无需关心 | ||
+ | |||
+ | 再次git pull一下,会让输入用户名和密码。这次输入之后以后就不会每次输入了。 | ||
+ | |||
+ | 如果要清除用户名和密码 | ||
+ | |||
+ | 运行一下命令缓存输入的用户名和密码 | ||
+ | |||
+ | git config --global credential.helper wincred | ||
+ | |||
+ | 清除掉缓存在git中的用户名和密码 | ||
+ | |||
+ | git credential-manager uninstall | ||
+ | |||
+ | |||
+ | |||
1、每次操作都需要输入用户名和密码感觉很繁琐,解决方法,在本地的工程文件夹的.git下打开config文件添加: | 1、每次操作都需要输入用户名和密码感觉很繁琐,解决方法,在本地的工程文件夹的.git下打开config文件添加: | ||
第117行: | 第531行: | ||
#这样只针对单个project | #这样只针对单个project | ||
[remote "origin"] | [remote "origin"] | ||
− | url = http://huangweiqing:[email protected]/zt_tech/_interface.git | + | url = http://huangweiqing:[email protected]/zt_tech/_interface.git |
+ | |||
+ | </pre> | ||
+ | |||
+ | =2024= | ||
+ | <pre> | ||
+ | github/myweb | ||
+ | |||
+ | 不存在 直接创建 git init devops | ||
+ | |||
+ | cd myweb | ||
+ | git init | ||
+ | |||
+ | |||
+ | (使用 "git restore --staged <文件>..." 以取消暂存) | ||
+ | |||
+ | ➜ myweb git:(main) ✗ git status | ||
+ | 位于分支 main | ||
+ | 您的分支基于 'origin/main',但此上游分支已经不存在。 | ||
+ | (使用 "git branch --unset-upstream" 来修复) | ||
+ | |||
+ | 尚未暂存以备提交的变更: | ||
+ | (使用 "git add <文件>..." 更新要提交的内容) | ||
+ | (使用 "git restore <文件>..." 丢弃工作区的改动) | ||
+ | 修改: README | ||
+ | |||
+ | 修改尚未加入提交(使用 "git add" 和/或 "git commit -a") | ||
+ | ➜ myweb git:(main) ✗ cat README | ||
+ | git tesf | ||
+ | pirnt("hello") | ||
+ | |||
+ | #回到以前的版本 注意小心 | ||
+ | git checkout -- | ||
+ | </pre> | ||
+ | https://www.bilibili.com/video/BV11q4y1s74P?spm_id_from=333.788.videopod.episodes&vd_source=e3e41ea2b1d70e0e3a6a0372ee88d714&p=21 | ||
=see also= | =see also= | ||
+ | [https://zhuanlan.zhihu.com/p/273304706 Github 太狠了,居然把 "master" 干掉了!] | ||
+ | |||
[https://www.liaoxuefeng.com/wiki/896043488029600 廖Git教程] | [https://www.liaoxuefeng.com/wiki/896043488029600 廖Git教程] | ||
第146行: | 第596行: | ||
[https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001375840202368c74be33fbd884e71b570f2cc3c0d1dcf000 git 解决冲突] | [https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001375840202368c74be33fbd884e71b570f2cc3c0d1dcf000 git 解决冲突] | ||
+ | |||
+ | [https://blog.csdn.net/junli_chen/article/details/52623350?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param git创建一个自己的本地仓库] | ||
[[category:git]] | [[category:git]] | ||
[[category:ops]] | [[category:ops]] |
2024年11月7日 (四) 13:03的最新版本
目录
date
Sep 2024
常用命令
日常使用
添加 key 后 clone #配置 git config --global user.name "evan886" git config --global user.email "[email protected]" #日常用得最多就这几个了 git pull origin master git add youfile git commit -m " " git push origin master note 当我们修改了很多文件,而不想每一个都add,想commit自动来提交本地修改,我们可以使用-a标识。 git commit -a -m "Changed some files" 有时提交不成 冲突了 请用 git rm -f filename …or create a new repository on the command line echo "# learn-english" >> README.md git init git add README.md git commit -m "first commit" git remote add origin [email protected]:evan886/learn-english.git git push -u origin master …or push an existing repository from the command line git remote add origin [email protected]:evan886/learn-english.git git push -u origin master …or import code from another repository You can initialize this repository with code from a Subversion, Mercurial, or TFS project. Git 新加了两个命令: git switch用于切换分支 git restore用于恢复原样 两者可以取代 git checkout。 #创建分支命令: git branch (branchname) #切换分支命令: git checkout (branchname) git branch dev evan@myxps:~/github/learn-c$ git checkout dev M co M turtle/src/3.c 切换到分支 'dev' git 退回上一个版本 git reset --hard HEAD^ 退回单个文件 git log p128.groovy ➜ devops6-shared-library git:(main) git checkout 7a7dfa905067e92f913e593f2cdcfee87ba70538 p128.groovy
打标签
在Git中打标签非常简单,首先,切换到需要打标签的分支上: $ git branch * dev master $ git checkout master Switched to branch 'master' 然后,敲命令git tag <name>就可以打一个新标签: $ git tag v1.0 可以用命令git tag查看所有标签: $ git tag v1.0 ### git log ➜ myweb git:(main) git tag 1.0 ➜ myweb git:(main) git tag ➜ myweb git:(main) git tag -a v1.0 -m "my version 1.0" (main) git tag ➜ myweb git:(main) git show 1.0 ➜ myweb git:(main) git show v1.0
.gitignore 文件——如何在 Git 中忽略文件和文件夹
adj110 是我django mount 到xps的目录 git:(master) cat .gitignore project/laoqi/adj110
git将本地代码提交到远程仓库
#以我的i3wm配置文件作例子 # create a new repository on the command line echo "# myi3" >> README.md git init git add README.md git commit -m "first commit" git remote add origin [email protected]:evan886/myi3.git git push -u origin master
分支branch
in) git branch testing #创建 ➜ myweb git:(main) git branch * main testing (END) git checkout testing 切换到分支 'testing' git branch main * testing (END)
clone 指定分支
-b, --branch <分支> 检出 <分支> 而不是远程 HEAD git clone -b day-08 https://github.com/michaelliao/awesome-python3-webapp.git day8
git分支的合并
在git中,可以使用git merge 和git rebase两个命令来进行分支的合并
合并分支到master/main上
现在在dev分支上,可以用下面命令查看当前分支 git branch 刚开发完项目,执行了下列命令 git add . git commit -m 'dev' git push -u origin dev 把dev分支的代码合并到master分支上 该如何? 首先切换到master分支上 git checkout master 多人开发的话 需要把远程master上的代码pull下来 git pull origin master 我们把dev分支的代码合并到master上 git merge dev 然后查看状态 git status On branch master Your branch is ahead of 'origin/master' by 12 commits. (use "git push" to publish your local commits) nothing to commit, working tree clean 上面的意思就是你有12个commit,需要push到远程master上 执行下面命令即可 git push origin master
解决冲突
准备新的feature1分支,继续我们的新分支开发: $ git switch -c feature1 Switched to a new branch 'feature1' 修改readme.txt最后一行,改为: Creating a new branch is quick AND simple. 在feature1分支上提交: $ git add readme.txt $ git commit -m "AND simple" [feature1 14096d0] AND simple 1 file changed, 1 insertion(+), 1 deletion(-) 切换到master分支: $ git switch master Switched to branch 'master' Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) Git还会自动提示我们当前master分支比远程的master分支要超前1个提交。 在master分支上把readme.txt文件的最后一行改为: Creating a new branch is quick & simple. 提交: $ git add readme.txt $ git commit -m "& simple" [master 5dc6824] & simple 1 file changed, 1 insertion(+), 1 deletion(-) 现在,master分支和feature1分支各自都分别有新的提交,变成了这样: git-br-feature1 图片没显示 这种情况下,Git无法执行“快速合并”,只能试图把各自的修改合并起来,但这种合并就可能会有冲突,我们试试看: $ git merge feature1 Auto-merging readme.txt CONFLICT (content): Merge conflict in readme.txt Automatic merge failed; fix conflicts and then commit the result. 果然冲突了!Git告诉我们,readme.txt文件存在冲突,必须手动解决冲突后再提交。git status也可以告诉我们冲突的文件: $ git status On branch master Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits) 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: readme.txt no changes added to commit (use "git add" and/or "git commit -a") 我们可以直接查看readme.txt的内容: Git is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes of files. <<<<<<< HEAD Creating a new branch is quick & simple. ======= Creating a new branch is quick AND simple. >>>>>>> feature1 Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容,我们修改如下后保存: Creating a new branch is quick and simple. 再提交: $ git add readme.txt $ git commit -m "conflict fixed" [master cf810e4] conflict fixed 现在,master分支和feature1分支变成了下图所示: git-br-conflict-merged 用带参数的git log也可以看到分支的合并情况: $ git log --graph --pretty=oneline --abbrev-commit * cf810e4 (HEAD -> master) conflict fixed |\ | * 14096d0 (feature1) AND simple * | 5dc6824 & simple |/ * b17d20e branch test * d46f35e (origin/master) remove test.txt * b84166e add test.txt * 519219b git tracks changes * e43a48b understand how stage works * 1094adb append GPL * e475afc add distributed * eaadf4e wrote a readme file 最后,删除feature1分支: $ git branch -d feature1 Deleted branch feature1 (was 14096d0). 工作完成。 小结 当Git无法自动合并分支时,就必须首先解决冲突。解决冲突后,再提交,合并完成。 解决冲突就是把Git合并失败的文件手动编辑为我们希望的内容,再提交。 用git log --graph命令可以看到分支合并图。
https://www.liaoxuefeng.com/wiki/896043488029600/900004111093344
Rebase
https://liaoxuefeng.com/books/git/branch/rebase/index.html
usage first
添加key后 #配置 git config --global user.name "evan886" git config --global user.email "[email protected]"
error: 推送一些引用到 'https://github.com/xxx/xxx' 失败
error: 推送一些引用到 'https://github.com/xxx/xxx' 失败 提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支。 提示:再次推送前,先与远程变更合并(如 'git pull ...')。详见 提示:'git push --help' 中的 'Note about fast-forwards' 小节。 原因; 这个问题是远程仓库与本地不同步引起的 解决方案: 因为当前分支的最新提交落后于其对应的远程分支,所以我们先从远程库fetch到更新再和本地库合并,之后就可以git push操作了。 git fetch origin git merge origin/main
usage from gitlab
Git global setup git config --global user.name "Administrator" git config --global user.email "[email protected]" Create a new repository git clone [email protected]:root/monitor.git cd monitor git switch -c main touch README.md git add README.md git commit -m "add README" git push -u origin main Push an existing folder cd existing_folder git init --initial-branch=main git remote add origin [email protected]:root/monitor.git git add . git commit -m "Initial commit" git push -u origin main Push an existing Git repository cd existing_repo git remote rename origin old-origin git remote add origin [email protected]:root/monitor.git git push -u origin --all git push -u origin --tags
git 基础安装使用
git 服务器搭建
服务器上 apt-get install libcurl4-gnutls-dev libexpat1-dev gettext \ libz-dev libssl-dev apt-get install git git --version git version 2.20.1 使用我们指定目录作为Git仓库。 git init newrepo useradd git #也可以让这个用户不可以登录,为了安全嘛 #试过 这个是成功的 mkdir testrepo root@cailuw-test:/data# chown -R git.git testrepo/ root@cailuw-test:/data# cd testrepo/ root@cailuw-test:/data/testrepo# git init --bare test.git Initialized empty Git repository in /data/testrepo/test.git/ 以上命令Git创建一个空仓库,服务器上的Git仓库通常都以.git结尾。然后,把仓库所属用户改为git: root@cailuw-test:/data/testrepo# chown -R git.git test.git/ #添加key mkdir /home/git/.ssh -p cp /root/.ssh/authorized_keys /home/git/.ssh/ chown -R git.git /home/git/.ssh/authorized_keys client 在你的 客户端机器上 ssh -T git账号名@服务器IP #客户端验证连接 ssh -T [email protected] git clone [email protected]:/data/testrepo/test.git 正克隆到 'test'... warning: 您似乎克隆了一个空仓库。 #配置 git config --global user.name "evan886" git config --global user.email "[email protected]"
Git 服务器搭建 在CentOS/RHEL上源码或者 yum 安装Git 以及 linux and win client 使用
http://blog.linuxchina.net/2015/06/18/how-to-use-git-%e8%bd%ac/
git usage on win
#git init git config --global --edit 配置为你的用户和用户名 # This is Git's per-user configuration file. [user] # Please adapt and uncomment the following lines: name = evan email = [email protected] After doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 日常用得最多就这几个了 git pull origin master git add youfile git commit -m " " git push origin master [Win-1.lxtx_fengjw] ➤ git push Username for 'http://gitlab.net': Password for 'http://[email protected]': To http://gitlab.net/lxtx-backend-docs/docs.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'http://gitlab.net/lxtx-backend-docs/docs.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.
Git自动账号密码填充 git pull记住用户名和密码
2021 进入项目目录 git config --global credential.helper store 然后会生成一个本地文件用于记录用户名和密码,这个文件我们无需关心 再次git pull一下,会让输入用户名和密码。这次输入之后以后就不会每次输入了。 如果要清除用户名和密码 运行一下命令缓存输入的用户名和密码 git config --global credential.helper wincred 清除掉缓存在git中的用户名和密码 git credential-manager uninstall 1、每次操作都需要输入用户名和密码感觉很繁琐,解决方法,在本地的工程文件夹的.git下打开config文件添加: [credential] helper = store或者在git bash 中执行 git config --global credential.helper store再输入一次用户名密码后就可以保存住了。 2、不行就用以下方法: 先用Git拉一次东西,拉的时候会提醒你输入帐号的密码 输入正确的帐号和密码后,等东西拉完以后输入 git config --global credential.helper store #这样只针对单个project [remote "origin"] url = http://huangweiqing:[email protected]/zt_tech/_interface.git
2024
github/myweb 不存在 直接创建 git init devops cd myweb git init (使用 "git restore --staged <文件>..." 以取消暂存) ➜ myweb git:(main) ✗ git status 位于分支 main 您的分支基于 'origin/main',但此上游分支已经不存在。 (使用 "git branch --unset-upstream" 来修复) 尚未暂存以备提交的变更: (使用 "git add <文件>..." 更新要提交的内容) (使用 "git restore <文件>..." 丢弃工作区的改动) 修改: README 修改尚未加入提交(使用 "git add" 和/或 "git commit -a") ➜ myweb git:(main) ✗ cat README git tesf pirnt("hello") #回到以前的版本 注意小心 git checkout --