1、配置用户名和邮箱 1 2 3 4 5 6 7 配置用户名和邮箱 git config --global user.email "you@example.com" git config --global user.name "Your Name" PS:以上信息在本地仓库中仅仅记录信息,不需要真实存在 查看配置信息 git config --global user.name git config --global user.email
2、获取本地仓库 1 2 git init 初始化当前的目录为一个git仓库(该本地仓库仅存在当前目录!!!) ll -a 初始化后当前目录会多一个.git文件夹,-a可以显示隐藏文件
3、创建完成一个文件后,变为untracked未跟踪状态 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 1.创建完成一个文件后,变为untracked未跟踪状态 [yinwei@centos ~]$ touch file01.txt [yinwei@centos ~]$ git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # file01.txt nothing added to commit but untracked files present (use "git add" to track) 2.使用git add提交到暂存区(git add .为添加当前目录中所有文件) [yinwei@centos git_learn]$ git add file01.txt [yinwei@centos git_learn]$ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: file01.txt # ------------------------------------------------------------ 3.使用git commit 提交进入仓库并产生提交记录,随后才可以使用git log查看提交记录 [yinwei@centos git_learn]$ git commit -m "add file01" [master (root-commit) b5c5a80] add file01 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 file01.txt [yinwei@centos git_learn]$ git log commit b5c5a80b98cf80d30f4700633680e4283b61930a Author: yinwei <572783632@qq.com> Date: Mon Mar 28 06:31:11 2022 -0700 add file01 ------------------------------------------------------------ 4.编辑文件file01后,再次进入工作区,此时为unstaged未暂存状态 [yinwei@centos git_learn]$ vi file01.txt (修改文件后) [yinwei@centos git_learn]$ git status # On branch master # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: file01.txt (modified意为修改) # no changes added to commit (use "git add" and/or "git commit -a") 再次git add添加到暂存区 [yinwei@centos git_learn]$ git add . [yinwei@centos git_learn]$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: file01.txt # 再次git commit进入仓库中,查看log会显示出这两次commit的提交记录 [yinwei@centos git_learn]$ git commit -m "update file01" [master 4b611fc] update file01 1 file changed, 1 insertion(+) [yinwei@centos git_learn]$ git log commit 4b611fc79ecc72869005f85e1d063b01d6c09e47 Author: yinwei <572783632@qq.com> Date: Mon Mar 28 06:40:10 2022 -0700 第二次 update file01 第二次 commit b5c5a80b98cf80d30f4700633680e4283b61930a Author: yinwei <572783632@qq.com> Date: Mon Mar 28 06:31:11 2022 -0700 第一次 add file01 第一次 注意顺序是从上到下从最近到之前,越上面越新
命令形式:git log [option]
作用:查看提交记录
options 可叠加后缀
–all 显示所有分支
–pretty=oneline 将提交信息显示为一行
–abbrev-commit 使得输出的commitId更简短
–graph 以图的形式显示
4、版本回退 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 作用:版本切换 命令形式:git reset --hard commitID 回退到某个commit的版本 commitID 可以使用 git-log 或 git log 指令查看 如何查看已经删除的记录? git reflog 这个指令可以看到已经删除的提交记录 --------------进行回退---------------------- [yinwei@centos git_learn]$ git log commit 4b611fc79ecc72869005f85e1d063b01d6c09e47 Author: yinwei <572783632@qq.com> Date: Mon Mar 28 06:40:10 2022 -0700 update file01 commit b5c5a80b98cf80d30f4700633680e4283b61930a Author: yinwei <572783632@qq.com> Date: Mon Mar 28 06:31:11 2022 -0700 add file01 [yinwei@centos git_learn]$ git reset --hard b5c5a80b98cf80d30f4700633680e4283b61930a HEAD is now at b5c5a80 add file01 [yinwei@centos git_learn]$ git log commit b5c5a80b98cf80d30f4700633680e4283b61930a Author: yinwei <572783632@qq.com> Date: Mon Mar 28 06:31:11 2022 -0700 add file01 显示已经回退至刚添加文件的时候 ---------------------注意------------------------------------ 只要还有commitID便可以跳到该版本,只要提交了commit就丢不了,前提是不能删除文件!!!!!!!!!!!!!!!!!! 如果回退后看不到commitID,则使用git reflog可以看到之前已经删除的提交记录 [yinwei@centos git_learn]$ git reflog b5c5a80 HEAD@{0}: reset: moving to b5c5a80b98cf80d30f4700633680e4283b61 4b611fc HEAD@{1}: commit: update file01 b5c5a80 HEAD@{2}: commit (initial): add file01 (END)
设置不需要git管理的文件名单 可以在工作目录中创建一个名为 .gitignore 的文件(文件名称固定 ),列出要忽略的文件模式,如*.a,忽略以.a结尾的文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 [yinwei@centos git_learn]$ touch file02.a [yinwei@centos git_learn]$ touch .gitignore [yinwei@centos git_learn]$ vi .gitignore 在该文件中写入*.a 表示所有以.a结尾的文件不需要git管理 [yinwei@centos git_learn]$ git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # .gitignore 发现此时忽略了file02.a文件(此时file01已commit不在工作区) nothing added to commit but untracked files present (use "git add" to track)
创建、切换、合并分支 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 [yinwei@centos git_learn]$ git branch dev01 [yinwei@centos git_learn]$ git branch dev01 [yinwei@centos git_learn]$ git log --pretty=oneline --abbrev-commit --graph --decorate * 4b611fc (HEAD, master) update file01 * b5c5a80 (dev01) add file01 --------当add和commit了gitignore文件后------------ [yinwei@centos git_learn]$ git log --pretty=oneline --abbrev-commit --graph --decorate * 316fa80 (HEAD, master) add gitignore * 4b611fc update file01 * b5c5a80 (dev01) add file01 -----------------切换到dev01分支-------------------- [yinwei@centos git_learn]$ git checkout dev01 Switched to branch 'dev01' [yinwei@centos git_learn]$ git log --pretty=oneline --abbrev-commit --graph --decorate * b5c5a80 (HEAD, dev01) add file01 此时log显示会只显示dev01有关的记录(如果不添加--all后缀的话),且切换分支后,对应的文件会切换。此时文件夹中不会显示在master分支下commit的.gitignore文件 -----------------切换回master分支-------------------- [yinwei@centos git_learn]$ git checkout master Switched to branch 'master' [yinwei@centos git_learn]$ git log --pretty=oneline --abbrev-commit --graph --decorate * 316fa80 (HEAD, master) add gitignore * 4b611fc update file01 * b5c5a80 (dev01) add file01 切换到master后,文件会重新出现 ------------------------合并分支------------------------- 使用git merge [目标分支名称],将目标分支合并到当前分支。一般是切换到主分支,将其他临时分支合并到主要的如master分支上 [yinwei@centos git_learn]$ git branch 位于master分支时 dev01 * master [yinwei@centos git_learn]$ git merge dev01 Merge made by the 'recursive' strategy. file02.a | 0 file02.txt | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 file02.a create mode 100644 file02.txt (以上为将dev01合并到master分支) [yinwei@centos git_learn]$ git log --pretty=oneline --abbrev-commit --graph --decorate --all * 927fbb9 (HEAD, master) Merge branch 'dev01' |\ | * f6f7583 (dev01) add file02 * | 316fa80 add gitignore * | 4b611fc update file01 |/ * b5c5a80 add file01 (上面log显示最近将dev01合并到了master上,此时所有文件都出现了)
查看本地分支
命令:git branch
创建本地分支
命令:git branch 分支名
切换分支(checkout)
命令:git checkout 分支名
我们还可以直接切换到一个不存在的分支(创建并切换)
命令:git checkout -b 分支名
删除分支
不能删除当前分支,只能删除其他分支
git branch -d b1 删除分支时,需要做各种检查
git branch -D b1 不做任何检查,强制删除
1 2 3 4 5 6 7 8 9 10 [yinwei@centos git_learn]$ git branch -d dev01 Deleted branch dev01 (was f6f7583). [yinwei@centos git_learn]$ git log --pretty=oneline --abbrev-commit --graph --decorate --all * 927fbb9 (HEAD, master, dev) Merge branch 'dev01' |\ | * f6f7583 add file02 * | 316fa80 add gitignore * | 4b611fc update file01 |/ * b5c5a80 add file01
合并分支(merge)
一个分支上的提交可以合并到另一个分支
命令:git merge 分支名称
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 解决合并冲突: 当在两个分支中同时修改了同一个文件的同一行时(不是同一行时会自动合并),合并时会产生冲突 [yinwei@centos git_learn]$ git merge dev Auto-merging file01.txt CONFLICT (content): Merge conflict in file01.txt Automatic merge failed; fix conflicts and then commit the result. 然后打开file01.txt中会显示两个不同分支修改的后合并产生的冲突点 <<<<<<< HEAD updata 1 count=1 ======= updata 1 count=2 >>>>>>> dev 排除冲突后,再次add 和commit [yinwei@centos git_learn]$ git add . [yinwei@centos git_learn]$ git commit [master a54af4e] Merge branch 'dev' [yinwei@centos git_learn]$ git log --pretty=oneline --abbrev-commit --graph --decorate --all * a54af4e (HEAD, master) Merge branch 'dev' |\ | * f419933 (dev) file01 add count=2 * | 0b3211f file01 add count=1 |/ * 927fbb9 Merge branch 'dev01' |\ | * f6f7583 add file02 * | 316fa80 add gitignore * | 4b611fc update file01 |/ * b5c5a80 add file01
添加,推送远程仓库,绑定本地和远程分支 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 [yinwei@centos git_learn]$ git remote add origin git@gitee.com:nekoame/git_learn.git [yinwei@centos git_learn]$ git remote origin [yinwei@centos git_learn]$ git push origin master Counting objects: 22, done. Compressing objects: 100% (14/14), done. Writing objects: 100% (22/22), 1.68 KiB | 0 bytes/s, done. Total 22 (delta 6), reused 0 (delta 0) remote: Powered by GITEE.COM [GNK-6.3] To git@gitee.com:nekoame/git_learn.git * [new branch] master -> master 如果报错,说明原来的仓库的不是新建未上传过的已经有了分支,需要git pull ----------------------------------------------------------- 下面使用git branch -vv 查看本地和远程分支的对应关系,此时没有绑定关系 [yinwei@centos git_learn]$ git branch -vv dev f419933 file01 add count=2 * master a54af4e Merge branch 'dev' ------------------------------------------------------------ 绑定本地和远程的分支,意为将本地的master绑定到远程的master --set-upstream 意为推送到远端的同时并且建立起和远端分支的关联关系。 如果当前分支已经和远端分支关联,则可以省略分支名和远端名。 git push 将master分支推送到已关联的远端分支。 [yinwei@centos git_learn]$ git push --set-upstream origin master:master Branch master set up to track remote branch master from origin. Everything up-to-date [yinwei@centos git_learn]$ git branch -vv dev f419933 file01 add count=2 * master a54af4e [origin/master] Merge branch 'dev'
克隆仓库
如果已经有一个远端仓库,我们可以直接clone到本地。
命令: git clone <仓库路径> [本地目录]
本地目录可以省略,会自动生成一个目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 [yinwei@centos Desktop]$ git clone git@gitee.com:nekoame/git_learn.git git_test02 Cloning into 'git_test02'... remote: Enumerating objects: 22, done. remote: Counting objects: 100% (22/22), done. remote: Compressing objects: 100% (14/14), done. remote: Total 22 (delta 6), reused 0 (delta 0), pack-reused 0 Receiving objects: 100% (22/22), done. Resolving deltas: 100% (6/6), done. [yinwei@centos Desktop]$ cd git_test02/ -------------下面看看clone下来的log和分支已经绑定关系------------- [yinwei@centos git_test02]$ git log --pretty=oneline --abbrev-commit --graph --decorate --all * a54af4e (HEAD, origin/master, origin/HEAD, master) Merge branch 'dev' |\ | * f419933 file01 add count=2 * | 0b3211f file01 add count=1 |/ * 927fbb9 Merge branch 'dev01' |\ | * f6f7583 add file02 * | 316fa80 add gitignore * | 4b611fc update file01 |/ * b5c5a80 add file01 [yinwei@centos git_test02]$ git branch * master [yinwei@centos git_test02]$ git branch -vv * master a54af4e [origin/master] Merge branch 'dev'
从远程仓库中抓取和拉取
远程分支和本地的分支一样,我们可以进行merge操作,只是需要先把远端仓库里的更新都下载到本地,再进行操作。
抓取 命令:git fetch [remote name] [branch name]
抓取指令就是将仓库里的更新都抓取到本地,不会进行合并
如果不指定远端名称和分支名,则抓取所有分支。
相当于只fetch到log记录,
拉取 命令:git pull [remote name] [branch name]
拉取指令就是将远端仓库的修改拉到本地并自动进行合并,等同于 fetch+merge
如果不指定远端名称和分支名,则抓取所有并更新当前分支。
查看、删除远程分支
git branch -a 查看所有本地和远程分支
git push origin –delete 分支名 删除远程分支
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 [yinwei@centos git_test02]$ git branch -a dev03 hoxfix * hoxfix02 master remotes/origin/HEAD -> origin/master remotes/origin/dev03 remotes/origin/hoxfix remotes/origin/hoxfix02 remotes/origin/master remotes/origin/ver [yinwei@centos git_test02]$ git push origin --delete ver remote: Powered by GITEE.COM [GNK-6.3] To git@gitee.com:nekoame/git_learn.git - [deleted] ver [yinwei@centos git_test02]$ git branch -a dev03 hoxfix * hoxfix02 master remotes/origin/HEAD -> origin/master remotes/origin/dev03 remotes/origin/hoxfix remotes/origin/hoxfix02 remotes/origin/master
修改本地分支名称
git branch -m 原分支名 新分支名
没有原分支名则默认为当前分支!!!
获取最新分支情况
备注特殊情况 如果出现下列情况,说明本地绑定了已经在远程仓库删除了的分支。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [yinwei@centos git_learn]$ git branch -vv dev f419933 file01 add count=2 dev03 8ce141f [origin/dev03] add file05 hoxfix fe3c2bd [origin/hoxfix] add hotfix hoxfix02 f9d0f68 [origin/hoxfix02] add hotfix02 master 51e11a2 [origin/master] add master01 * ver01 82ae0d8 [origin/ver] add ver01 [yinwei@centos git_learn]$ git pull Your configuration specifies to merge with the ref 'ver' from the remote, but no such ref was fetched. 此时显示ver01绑定的远程分支ver已经被删除 ---------------------解决办法-------------------------------- [yinwei@centos git_learn]$ git push --set-upstream origin ver01:ver01 重新push绑定 Branch ver01 set up to track remote branch ver01 from origin. Everything up-to-date [yinwei@centos git_learn]$ git branch -vv dev f419933 file01 add count=2 dev03 8ce141f [origin/dev03] add file05 hoxfix fe3c2bd [origin/hoxfix] add hotfix hoxfix02 f9d0f68 [origin/hoxfix02] add hotfix02 master 51e11a2 [origin/master] add master01 * ver01 82ae0d8 [origin/ver01] add ver01