学习Git,需要推荐一下廖雪峰老师的Git教程:
https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
另外还有两篇git的文章挺好的:
https://www.yiibai.com/git/git_handling_conflicts.html
https://blog.csdn.net/xiaokang123456kao/article/details/709212590.git基本操作
git init ##创建git仓库git add xxx ##将本地文件xxx的修改提交到暂存区git commit -m "mx" ##将暂存区的所有修改修改提交到仓库当中git reset --hard HEAR^ ##本地目录恢复到上一个版本git reset --hard xxx ##恢复本地目录至版本xxxgit log ##查看提交日记git status ##显示工作目录和暂存区的状态git diff ##比较当前文件和暂存区文件的差异git diff HEAR HEAR^eg:
samuel@franz_z$ mkdir learngit
samuel@franz_z$ cd learngit/samuel@learngit$ ls -a. .. hobbyssamuel@learngit$ cat hobbys ##查看hobbys文件内容#HOBBYS LISTrunningswimmingsamuel@learngit$ git init ##创建git仓库初始化空的 Git 仓库于 /home/samuel/work/franz_z/learngit/.git/samuel@learngit$ ls -a ##learngit目录下多了.git目录
. .. .git hobbyssamuel@learngit$ git add hobbys ##将hobbys文件提交到暂存区
samuel@learngit$ git commit -m "init learngit project" ##将learngit下修改提交到仓库[master (根提交) abe2f4f] init learngit project 1 file changed, 3 insertions(+) create mode 100644 hobbyssamuel@learngit$ git log ##查看版本commit abe2f4f3dc56942c22a98bf2a07380bae8e263ceAuthor: samauel <samauel.zhou@honeywell.com>Date: Mon Oct 29 14:03:55 2018 +0800 init learngit project修改hobbys文件后继续提交samuel@learngit$ echo "playing football" >> hobbyssamuel@learngit$ cat hobbys#HOBBYS LISTrunningswimmingplaying footballsamuel@learngit$ git add hobbys
samuel@learngit$ git commit -m "add a new hobby"[master 4a56f9a] add a new hobby 1 file changed, 1 insertion(+)samuel@learngit$ git logcommit 4a56f9a14a29542b2d8cf92c317ba198595a5321Author: samauel <samauel.zhou@honeywell.com>Date: Mon Oct 29 14:09:54 2018 +0800 add a new hobbycommit abe2f4f3dc56942c22a98bf2a07380bae8e263ceAuthor: samauel <samauel.zhou@honeywell.com>Date: Mon Oct 29 14:03:55 2018 +0800 init learngit project1.查看远程仓库
git remote #查看远程仓库(简单描述)git remote -v #查看远程仓库(详尽描述)2.添加远程仓库
git remote add origin git@github.com:Franzzt/xxx.git ##添加名为origin的远程仓库3.移除远程仓库
git remote rm origin4.从仓库中获取源码
git clone 仓库地址git checkout -b dev origin/dev ##checkou远程分支dev,在本地起名为dev,并切换到分支dev仓库中获取源码分为两种方式:1.获取所有的项目源码,git clone git@github.com:Franzzt/learngit.git 下载整个项目的源码2.获取单个分支的源码
git clone -b origin/dev git@github.com:Franzzt/learngit.git 只下载分支dev的源码不同的是下载整个项目的源码之后可以在整个项目的所有分支中来回切换,而下载单个分支的动作之后则无法
在切换到其他分支5.本地更新推送至远程仓库
git push <远程主机名> <本地分支名>:<远程分支名>git push -u origin master ##推送主分支master到仓库origingit push -u origin feature-A ##推送分支feature-A到仓库feature-Agit push 推送当前分支到仓库6.远程仓库更新至本地
git pull <远程主机名> <远程分支名>:<本地分支名> ##取回远程库更新,并与本地分支合并git pull origin devgit fetch ##将远程的所有分支跟新到本地
git fetch <远程主机名> <分支名> ##取回远程特定分支的更新git fetch origingit merge origin/next7.分支操作
git branch ##查看本地分支git branch -r ##查看远程分支git branch -a ##查看所有分支git branch feature ##以当前所在分支创建新分支feature
git checkout feature ##切换到分支featuregit checkout -b feature ##创建分支feature并切换到分支featuregit merge feature ##将分支feature内容合并到当前所在分支
git branch -d feature ##删除本地分支feature
git push origin --delete feature ##删除远程仓库分支featuregit push origin dev ##推送分支dev至远程仓库
git branch --set-upstream-to=origin/dev dev # 建立本地分支和远程分支的关联假如本地有很多分支,哪些分支需要推送到远程仓库中呢?一般是这样的,
master分支是主分支,要时刻与远程同步,一般我们发布最新版本就用master分支。develop分支是开发分支,团队中所有人都在这个分支上开发,所以也需要与远程同步。bug分支一般只在本地使用来修复bug,一般不需推送远程仓库中。feature分支是否需要推送到远程,要看是不是有几个人合作开发新功能,如果你是一个开发,那就留在本地吧。release分支一般是系统管理,推送或抓取的分支一般与开发人员无关。other分支按需求分配。多人协作
1.github上创建仓库(仓库地址:git@giithub.com:Franzzt/xxx.git)2.git init3.git remote add origin git@giithub.com:Franzzt/xxx.git git remote(查看)4.git add tset.txt5.git commit -m "first file"6.git push -u origin master7.git checkout -b devlop8.git add text.txt9.git commit -m "add text.txt file to branch devlop"10.git push -u origin devlop11.git checkout master12.git merge devlop13.git fetch origin master14.git diff15.git add16.git commit -m17.git push origin master18.git pull origin master19.git git branch --set-upstream-to=origin/devlop20.modify file/18.git fetch origin master19.git diff20.git merge1.从github上获取项目源码
samuel@franz_z$ git clone git@github.com:Franzzt/learngit.git正克隆到 'learngit'...remote: Enumerating objects: 12, done.remote: Counting objects: 100% (12/12), done.remote: Compressing objects: 100% (8/8), done.remote: Total 12 (delta 1), reused 5 (delta 0), pack-reused 0接收对象中: 100% (12/12), 完成.处理 delta 中: 100% (1/1), 完成.检查连接... 完成。samuel@franz_z$ lsbackup learngit linux-imx uboot-imx2.创建新文件或修改文件后提交
samuel@learngit$ vim gitlearn.txtsamuel@learngit$ cat gitlearn.txtHello worldJuset learngitsamuel@learngit$ git add gitlearn.txtsamuel@learngit$ git commit -m "create a new file learngit"[master 6c47d1b] create a new file learngit 1 file changed, 2 insertions(+) create mode 100644 gitlearn.txt 3.推送本地目录更新至远程仓库 samuel@learngit$ git push origin master对象计数中: 3, 完成.Delta compression using up to 4 threads.压缩对象中: 100% (2/2), 完成.写入对象中: 100% (3/3), 317 bytes | 0 bytes/s, 完成.Total 3 (delta 0), reused 0 (delta 0)To git@github.com:Franzzt/learngit.git 0c584e5..6c47d1b master -> master4.创建分支修改文件以及合并
samuel@learngit$ git checkout -b devlop切换到一个新分支 'devlop'samuel@learngit$ git branch* devlop mastersamuel@learngit$ git add learngit.txtsamuel@learngit$ git commit -m "add a new file learngit"[devlop 6149333] add a new file learngit 1 file changed, 2 insertions(+) create mode 100644 learngit.txtsamuel@learngit$ lsgitlearn.txt learngit.txt README.mdsamuel@learngit$ git checkout master
切换到分支 'master'您的分支与上游分支 'origin/master' 一致。samuel@learngit$ lsgitlearn.txt README.mdsamuel@learngit$ git merge devlop更新 6c47d1b..6149333Fast-forward learngit.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 learngit.txtsamuel@learngit$ lsgitlearn.txt learngit.txt README.mdsamuel@learngit$ git checkout devlop
切换到分支 'devlop'samuel@learngit$ vim learngit.txtsamuel@learngit$samuel@learngit$ git add learngit.txtsamuel@learngit$ git commit -m "modify file learngit"[devlop 83b22bc] modify file learngit 1 file changed, 1 insertion(+)samuel@learngit$ git checkout master切换到分支 'master'您的分支领先 'origin/master' 共 1 个提交。 (使用 "git push" 来发布您的本地提交)samuel@learngit$ vim learngit.txtsamuel@learngit$ git add learngit.txtsamuel@learngit$ git commit -m "modify file learngit"[master c4cbc1e] modify file learngit 1 file changed, 1 insertion(+)samuel@learngit$ git branch devlop* mastersamuel@learngit$ git merge devlop自动合并 learngit.txt冲突(内容):合并冲突于 learngit.txt自动合并失败,修正冲突然后提交修正的结果。samuel@learngit$ git diffdiff --cc learngit.txtindex 215a818,53dc181..0000000--- a/learngit.txt+++ b/learngit.txt@@@ -1,3 -1,3 +1,7 @@@ #Create new file learngit #HAHAHA++<<<<<<< HEAD +Git is a tool very userful++=======+ GIt is very userful++>>>>>>> devlopsamuel@learngit$ vim learngit.txtsamuel@learngit$samuel@learngit$ git add learngit.txtsamuel@learngit$ git commit -m "final modify"[master c003791] final modify5.本地分支提交冲突法1:
samuel@learngit$ git push origin master
To git@github.com:Franzzt/learngit.git ! [rejected] master -> master (non-fast-forward) error: 无法推送一些引用到 'git@github.com:Franzzt/learngit.git' 提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支。 提示:再次推送前,先与远程变更合并(如 'git pull ...')。详见 提示:'git push --help' 中的 'Note about fast-forwards' 小节。 samuel@learngit$ git pull 自动合并 gitlearn.txt 冲突(内容):合并冲突于 gitlearn.txt 自动合并失败,修正冲突然后提交修正的结果。 samuel@learngit$ vim gitlearn.txt samuel@learngit$ samuel@learngit$ git diff diff --cc gitlearn.txt index b7ed434,73b15b1..0000000 --- a/gitlearn.txt +++ b/gitlearn.txt @@@ -1,4 -1,4 +1,8 @@@ Hello world Juset learngit I am learning how to use git ++<<<<<<< HEAD +Help with pronunciation ++======= + Can you help me find a few things ++>>>>>>> 7093398c620fccd49322a735756c22963cb41699 samuel@learngit$ samuel@learngit$ vim learngit.txt samuel@learngit$ vim learngit.txt samuel@learngit$ vim gitlearn.txt samuel@learngit$ git add gitlearn.txt samuel@learngit$ git commit -c "change gitlearn.txt" fatal: 不能查询提交 change gitlearn.txt samuel@learngit$ git commit -m "change gitlearn.txt" [master cc5ba66] change gitlearn.txt samuel@learngit$ git push origin master 对象计数中: 20, 完成. Delta compression using up to 4 threads. 压缩对象中: 100% (19/19), 完成. 写入对象中: 100% (20/20), 2.13 KiB | 0 bytes/s, 完成. Total 20 (delta 4), reused 0 (delta 0) remote: Resolving deltas: 100% (4/4), done. To git@github.com:Franzzt/learngit.git 7093398..cc5ba66 master -> master法2.
samuel@franz_z$ cat bcd
samuel@learngit$ git push origin masterTo git@github.com:Franzzt/learngit.git ! [rejected] master -> master (fetch first) error: 无法推送一些引用到 'git@github.com:Franzzt/learngit.git' 提示:更新被拒绝,因为远程仓库包含您本地尚不存在的提交。这通常是因为另外 提示:一个仓库已向该引用进行了推送。再次推送前,您可能需要先整合远程变更 提示:(如 'git pull ...')。 提示:详见 'git push --help' 中的 'Note about fast-forwards' 小节。 samuel@learngit$ git fetch origin master remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (3/3), done. remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 展开对象中: 100% (3/3), 完成. 来自 github.com:Franzzt/learngit * branch master -> FETCH_HEAD cc5ba66..3c47049 master -> origin/master samuel@learngit$ git diff samuel@learngit$ git fetch samuel@learngit$ git diff samuel@learngit$ ls gitlearn.txt learngit.txt README.md samuel@learngit$ vim gitlearn.txt samuel@learngit$ samuel@learngit$ samuel@learngit$ git fetch origin master:master fatal: 拒绝获取到非纯仓库的当前分支 refs/heads/master samuel@learngit$ git diff samuel@learngit$ samuel@learngit$ git diff gitlearn.txt samuel@learngit$ samuel@learngit$ samuel@learngit$ git fetch origin/master fatal: 'origin/master' does not appear to be a git repository fatal: Could not read from remote repository.Please make sure you have the correct access rights
and the repository exists. samuel@learngit$ samuel@learngit$ samuel@learngit$ git fetch origin master 来自 github.com:Franzzt/learngit * branch master -> FETCH_HEAD samuel@learngit$ git diff samuel@learngit$ samuel@learngit$ vim gitlearn.txt samuel@learngit$ samuel@learngit$ vim gitlearn.txt samuel@learngit$ samuel@learngit$ git status 位于分支 master 您的分支和 'origin/master' 出现了偏离, 并且分别有 1 和 1 处不同的提交。 (使用 "git pull" 来合并远程分支) 无文件要提交,干净的工作区 samuel@learngit$ git log commit ba27af8ce62d12a16d21bc51b2f266e723a0b9a0 Author: samauel <samauel.zhou@honeywell.com> Date: Mon Oct 29 17:15:30 2018 +0800modify gitlearn.txt
commit cc5ba66e86644b9d12c6d751f201beabdac54f15
Merge: f52ac0b 7093398 Author: samauel <samauel.zhou@honeywell.com> Date: Mon Oct 29 17:13:25 2018 +0800change gitlearn.txt
commit f52ac0be922b23c73eeafa99b81ac6b75a9b6288
Author: samauel <samauel.zhou@honeywell.com> Date: Mon Oct 29 17:10:19 2018 +0800modify gitlearngit.txt
commit 7093398c620fccd49322a735756c22963cb41699
Author: Franzzt <40494721+Franzzt@users.noreply.github.com> Date: Mon Oct 29 17:06:13 2018 +0800samuel@learngit$ git fetch origin
samuel@learngit$ git merge master Already up-to-date. samuel@learngit$ ls gitlearn.txt learngit.txt README.md samuel@learngit$ vim gitlearn.txt samuel@learngit$ git merge origin/master 自动合并 gitlearn.txt 冲突(内容):合并冲突于 gitlearn.txt 自动合并失败,修正冲突然后提交修正的结果。 samuel@learngit$ git diff diff --cc gitlearn.txt index 1e0cbe0,4c58b0f..0000000 --- a/gitlearn.txt +++ b/gitlearn.txt @@@ -2,4 -2,4 +2,8 @@@ Hello worl Juset learngit I am learning how to use git Do you have any children ++<<<<<<< HEAD +Where do you parents live ++======= + Going to the library ++>>>>>>> origin/master samuel@learngit$ vim gitlearn.txt samuel@learngit$ samuel@learngit$ git add gitlearn.txt samuel@learngit$ git commit -m "modify gitlearn.txt" [master dfd3be0] modify gitlearn.txt samuel@learngit$ git push origin master 对象计数中: 6, 完成. Delta compression using up to 4 threads. 压缩对象中: 100% (6/6), 完成. 写入对象中: 100% (6/6), 730 bytes | 0 bytes/s, 完成. Total 6 (delta 2), reused 0 (delta 0) remote: Resolving deltas: 100% (2/2), completed with 1 local object. To git@github.com:Franzzt/learngit.git 3c47049..dfd3be0 master -> master samuel@learngit$