本文共 6662 字,大约阅读时间需要 22 分钟。
git介绍
与svn不同的是,svn是集中式管理,当自己主机上修改了文件,必须提交到服务器,其他人才能提交,不然冲突git是自己本机也可以作为仓库,也有当做服务器,分布式管理安装:
bash-completion:tab键补全命令[root@centos7-2 ~]# sudo yum install git bash-completion1、配置个人信息
[root@centos7-2 ~]# git config --global user.name "jacker"
[root@centos7-2 ~]# git config --global user.email "jack@163.com"2、查看用户名和email
[root@centos7-2 ~]# cat /root/.gitconfig [user]name = jackeremail = jack@163.com3、创建仓库推送文件
[root@centos7-2 ~]# mkdir /home/gitroot[root@centos7-2 ~]# cd /home/gitroot/初始化操作[root@centos7-2 gitroot]# git init Initialized empty Git repository in /home/gitroot/.git/[root@centos7-2 gitroot]# vim test.txt[root@centos7-2 gitroot]# git add test.txt [root@centos7-2 gitroot]# git commit -m "add a test file test.txt"[master (root-commit) ee26be4] add a test file test.txt1 file changed, 1 insertion(+)create mode 100644 test.txt[root@centos7-2 gitroot]# vim test.txt当更新文件时,没有提交,会提示你add,commit[root@centos7-2 gitroot]# 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: test.txt#no changes added to commit (use "git add" and/or "git commit -a")2、不提交,并清除更改的内容
git checkout -- test.txt3、[root@centos7-2 gitroot]# git status
#On branch masternothing to commit, working directory clean4、只更改不提交
[root@centos7-2 gitroot]# vim test.txt [root@centos7-2 gitroot]# git add test.txt以下是将test.txt给提交了,更新到最新的版本[root@centos7-2 gitroot]# git reset HEAD test.txt[root@centos7-2 gitroot]# cat test.txt
1.test12.test23.test34.test4[root@centos7-2 gitroot]# git log
简写:
[root@centos7-2 gitroot]# git log --pretty=oneline2b01244ccecfc72be09c8314d4050a023d4f8a32 6.txt6b93439a630a×××eda8355bd421a23a3d90a269 5.txt94eac572484320bfe534d92013be17c147907ef5 add a test file test.txtee26be4b4f753d09741b3685224d00902234936e add a test file test.txt恢复至哪个版本--git reset
[root@centos7-2 gitroot]# git reset --hard 94eac
HEAD is now at 94eac57 add a test file test.txt[root@centos7-2 gitroot]# cat test.txt 1.test12.test23.test34.test4恢复到哪个版本后,当前版本就是该版本
文件的删除操作
[root@centos7-2 gitroot]# git rm test.txt[root@centos7-2 gitroot]# git commit -m 'rm test.txt'GitHub官网:github.com
注册账号并激活,然后开始创建主机的仓库!创建完成后,添加key:点击浏览器右上角头像——setting——SSH and GPG keys(选择SSH keys)——在服务器(虚拟机)执行ssh-keygen命令生成密钥对(/root/.ssh/id_rsa-私钥, /root/.ssh/id_rsa.pub-公钥)——将公钥复制到浏览器后点“添加”。以下是输入github的密码
新增资源
查看
[root@centos7-2 home]# git clone git@github.com:sundysj/jack-git.git
编辑
[root@centos7-2 jack-git]# vim test1.txt[root@centos7-2 jack-git]# git add test1.txt
[root@centos7-2 jack-git]# git commit -m 'ad test1'[master 7c80b77] ad test11 file changed, 4 insertions(+)create mode 100644 test1.txt#提交到远程git push[root@centos7-2 jack-git]# git push warning: push.default is unset; its implicit value is changing inGit 2.0 from 'matching' to 'simple'. To squelch this messageand maintain the current behavior after the default changes, use:git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode'current' instead of 'simple' if you sometimes use older versions of Git)Counting objects: 4, done.
Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 271 bytes | 0 bytes/s, done.Total 3 (delta 0), reused 0 (delta 0)To git@github.com:sundysj/jack-git.git430a41c..7c80b77 master -> master现在在github远端上新建一个文件
在客户端上拉去最新的文件
[root@centos7-2 jack-git]# git pull origin master*:表示当前的所在分支
查看分支[root@centos7-2 jack-git]# git branch创建分支
[root@centos7-2 jack-git]# git branch jack切换分支
[root@centos7-2 jack-git]# git checkout jackSwitched to branch 'jack'[root@centos7-2 jack-git]# git branchjack分支上创建文件
[root@centos7-2 jack-git]# vim testjack1.txt[root@centos7-2 jack-git]# git add testjack1.txt[root@centos7-2 jack-git]# git commit -m 'ad tet'将这个分支推送到远程github上
[root@centos7-2 jack-git]# git push --set-upstream origin jack查看github上的分支
1、先切换到master上
[root@centos7-2 jack-git]# git checkout master2、合并
[root@centos7-2 jack-git]# git merge jack3、合并原则
主分支master不变,开发人员就在dev分支上开发后合并到master分支上[root@centos7-2 jack-git]# git branch -d jack
warning: not deleting branch 'jack' that is not yet merged to'refs/remotes/origin/jack', even though it is merged to HEAD.error: The branch 'jack' is not fully merged.If you are sure you want to delete it, run 'git branch -D jack'.说明: -d:删除;-D:强制删除
[root@centos7-2 jack-git]# git branch -D jack
Deleted branch jack (was 06beb7b).You have new mail in /var/spool/mail/root[root@centos7-2 jack-git]# git branchmaster分支是非常重要的,线上发布代码用这个分支,平时开发代码不要在该分支操作;
创建dev分支,专门用作开发,只有到发布到线上之前再把dev合并到master上
开发人员应该在dev分支的基础上再分支成个人分支,自己的分支(在自己的pc上)里面开发代码,然后合并到dev上
场景:
当你正在进行项目中某一部分的工作,里面的东西处于一个比较杂乱的状态,而你想转到其他分支上进行一些工作中。问题是你不想提交进行了一半的工作,否则以后你无法回到这个工作点。解决问题的办法就是:git stash(存储)命令。比如,我们在jack分支,编辑一个新的文件3.txt,此时我们要去其他分支。
[root@centos7-2 jack-git]# vim 3.txt[root@centos7-2 jack-git]# git add 3.txt#此时我不想提交,想切换到其他分支去看[root@centos7-2 jack-git]# git status
#无文件要提交,干净的工作区但是你发现3.txt不见了?[root@centos7-2 jack-git]# lsREADME.md test1.txt test2 test3 testjack1.txt找出来:
[root@centos7-2 jack-git]# git stash liststash@{0}: WIP on master: 1f6aec8 ch te[root@centos7-2 jack-git]# git stash apply stash@{0}#
#
#
[root@centos7-2 jack-git]# ls3.txt README.md test1.txt test2 test3 testjack1.txt删除保存的内容:git stash drop stash@{0}
远程上创建分支
客户端上更新
[root@centos7-2 jack-git]# git pull origin test查看远程仓库信息
[root@centos7-2 jack-git]# git remote -vorigin git@github.com:sundysj/jack-git.git (fetch)origin git@github.com:sundysj/jack-git.git (push)查看远程分支信息:
[root@centos7-2 jack-git]# git ls-remote origin2742321ca81a99c464d107ebbb405f76264d462e HEAD1f6aec8e78685142779c8862940a2e55fb516eca refs/heads/jack2742321ca81a99c464d107ebbb405f76264d462e refs/heads/masterbe35aae7e71a8149dcace5f6a151ccd1f1249565 refs/heads/test推送本地分支到远程:
[root@centos7-2 jack-git]# git add test1[root@centos7-2 jack-git]# git commit -m 'ch test1'[test d3e19a6] ch test11 file changed, 5 insertions(+)[root@centos7-2 jack-git]# git push origin test查看别名配置文件信息
[root@centos7-2 jack-git]# git config --list |grep alias转载于:https://blog.51cto.com/jacksoner/2049875