前面有讲到过,GIT是分布式的,GIT不需要依赖服务端,在自己的机器上就可以工作。
# yum install -y git
# mkdir /data/gitroot && cd /data/gitroot/
# git init Initialized empty Git repository in /data/gitroot/.git/# ls -latotal 0
drwxr-xr-x 3 root root 18 Aug 31 22:06 .drwxr-xr-x 5 root root 51 Aug 31 22:05 ..drwxr-xr-x 7 root root 119 Aug 31 22:06 .git #和svn有点类似# ls .git/branches config description HEAD hooks info objects refs
# vim 1.txt #随便写点东西rggjohktrlhm
dkmklwdo3ofker
gjrkghrjlhkgrlh# vim 1.txt# vim .git/config #添加下面内容,否则下面提交到仓库会报错[user]
name = lzx
email = lzx@lzx.com# git add 1.txt #添加到仓库# git commit -m "add 1.txt" #正式提交到仓库[master (root-commit) dd8aa2f] add 1.txt
1 file changed, 3 insertions(+)
create mode 100644 1.txt
# vim 1.txt #更改1.txtrggjohktrlhm
dkmklwdo3ofker
gjrkghrjlhkgrlh
deihfefufr
absqwjbejwfn# git add 1.txt # git commit -m "add 1.txt agin" #再次提交[master 70335c6] add 1.txt agin
1 file changed, 2 insertions(+)# git status #查看仓库状态# On branch masternothing to commit, working directory clean #干净的工作区,没有文件要提交# vim 1.txt #更改1.txtrggjohktrlhm
dkmklwdo3ofker
gjrkghrjlhkgrlh# git status #没有执行add和commit,所以提示我们执行这两步# On branch master# Changes not staged for commit:# (use "git add..." to update what will be committed)# (use "git checkout --..." to discard changes in working directory)## modified: 1.txt#no changes added to commit (use "git add" and/or "git commit -a")
# git diff 1.txt #查看变更,将更改后的内容和之前内容作对比diff --git a/1.txt b/1.txt
index be325ea..a42241c 100644
--- a/1.txt
+++ b/1.txt
@@ -1,5 +1,4 @@
rggjohktrlhm
dkmklwdo3ofker
gjrkghrjlhkgrlh
-deihfefufr
-absqwjbejwfn #显示我删掉了最后面两行+
# git add 1.txt # git commit -m "add 1.txt agin"[master 129107f] add 1.txt agin 上面的每一行都可以看做是一个版本# git reset --hard 70335c #回退版本,最后面的字符串可以简写HEAD is now at 70335c6 add 1.txt agin# git log --pretty=oneline70335c6f9906c1e171fbad0a5146fc6084779ee7 add 1.txt agin 假如回退了版本之后又后悔了怎么办,如果记得之前最新版本的字符串还可以更改,那如果不记得了怎么办? # git reflog #查看所有历史版本,可以得到之前代表最新版本的字符串70335c6 HEAD@{0}: reset: moving to 70335c 如果不小心把文件删掉了怎么办? # ls1.txt# rm -rf 1.txt # ls# git checkout -- 1.txt #恢复文件# ls1.txt 因为文件还在git版本库里面,所以才可以恢复。 如果文件作了修改,而且add了,但没有commit,如何撤销add,回到提交add前的状态呢? # vim 1.txt #更改1.txtrggjohktrlhm 从上面可以看出来,每次修改完之后都必须要add和commit,否则版本库里不会更新。 # git rm 1.txt #在本地删除1.txtrm '1.txt'# ls# git reset HEAD 1.txt #撤销del操作,回到删除前的状态Unstaged changes after reset: 浏览器打开 仓库名字自定义,选择public(private收费),点击创建仓库; 添加key,右上角点击个人头像,选择 在linux机器上创建SSH公钥: # ssh-keygen Generating public/private rsa key pair. 回到浏览器界面,Title自定义,将linux上公钥复制在Key中,然后 在linux客户端创建仓库,随意写点东西推送到远程服务端上: # cd /tmp/# mkdir mylinux && cd mylinux/# echo "# mylinux" >> README.md# git initInitialized empty Git repository in /tmp/mylinux/.git/# ls -latotal 8 然后在服务端刷新一下,就可以看到我们推送的内容了。 # vim .git/config #url默认为https地址,修改为SSH地址url = git@github.com:Tobewont/mylinux.git# echo "lzxlzx" > 2.txt# git add 2.txt # git commit -m "add 2.txt"[master 50590ed] add 2.txt 先在服务器端仓库找到 # cd /home/# lsadmin myproject# git clone git@github.com:Tobewont/mylinux.git #git clone进行仓库克隆Cloning into 'mylinux'... # vim mylinux/README.mdaaaaaa 到服务端仓库查看,刷新一下就可以看到刚刚的更改。 先在服务端仓库编辑 # git pull #从服务端拉取内容Enter passphrase for key '/root/.ssh/id_rsa':remote: Counting objects: 3, done. # cd /data/gitroot/# ls1.txt# git branch #查看分支* master #当前处于master分支 # git branch lzx #创建分支# git branch # git checkout lzx #切换分支Switched to branch 'lzx'# git branch* lzx # echo "adfwrfrg" > 2.txt# git add 2.txt# git commit -m "add 2.txt"[lzx 9a601f6] add 2.txt 如果想把lzx分支合并到master分支,就需要先切换到master分支# git branch 分支合并很容易产生冲突,如果master分支和lzx分支都对2.txt进行了编辑, 产生冲突的原因就是两边提交的内容不一致,那解决冲突就是要让两边提交的内容一致# vim 2.txt #修改为lzx分支中2.txt的内容adfwrfrg 而如果lzx分支修改的内容不是我们想要的怎么办?可以在lzx分支下修改为master分支中的内容,提交,然后合并master分支到lzx分支即可(倒着合并)。 分支合并的原则:要把最新的分支合并到旧的分支,即merge后面跟着的分支一定是最新的分支。 # git checkout masterSwitched to branch 'master'# git branch -d lzx #删除分支Deleted branch lzx (was 5fd7ae9). 1. master分支十分重要,线上发布代码使用这个分支,开发代码不要使用这个分支; 本地新建的分支如果不推送到远程,对其他人就是不可见的。 对勾在哪里就在哪一个分支下。 # git clone git@github.com:Tobewont/mylinux.gitCloning into 'mylinux'... 即克隆远程仓库默认只会克隆master分支,即使远程仓库有多个分支。 # git ls-remote originEnter passphrase for key '/root/.ssh/id_rsa': #输入之前配置生成SSH时输入的密码c24d9bbd51579bd6bb0b68484935732f61f7783c HEAD # git checkout -b dev origin/dev #克隆远程分支并切换到该分支Branch dev set up to track remote branch dev from origin. # vim 2.txt #添加下面内容fremkg 对于git push有两种情况: 1. 当本地分支和远程分支一致时: 示例: # git branch dev2# git branch * dev 刷新一下,可以看到服务端已经有dev2分支了。 并且dev2分支下有3.txt了。 标签类似于快照功能,可以给版本库打一个标签,记录某个时刻版本库的状态,也可以随时恢复到该状态。相比于分支来说,分支更加实用。 标签主要是针对master分支进行操作。 # git checkout master #切换到master分支Switched to branch 'master'# git tag v1.0 #打上标签v1.0# git tag #查看当前分支所有标签v1.0# git show v1.0 #查看标签v1.0内容commit c24d9bbd51579bd6bb0b68484935732f61f7783c #commit信息字符串Author: Tobewont <42890839+Tobewont@users.noreply.github.com>Date: Thu Sep 6 14:50:01 2018 +0800 # git log --pretty=oneline #查看历史commitc24d9bbd51579bd6bb0b68484935732f61f7783c Update README.md # git tag -a v0.1 -m "lzx tag" ca836f8# git tagv0.1 # git tag -d v0.1 #删除标签v0.1Deleted tag 'v0.1' (was cdb35ab)# git tagv0.8 以上操作都是在客户端本地进行的,在远程服务端还没有任何标签。 # git push origin v1.0 #推送标签v1.0Enter passphrase for key '/root/.ssh/id_rsa':Total 0 (delta 0), reused 0 (delta 0)To git@github.com:Tobewont/mylinux.git 服务端刷新一下,可以看到刚刚推送的标签v1.0。 # git push --tag origin #推送所有标签Enter passphrase for key '/root/.ssh/id_rsa':Total 0 (delta 0), reused 0 (delta 0)To git@github.com:Tobewont/mylinux.git 服务端刷新一下,可以看到刚刚推送的标签v0.8。 # git tag -d v0.8 #本地删除v0.8Deleted tag 'v0.8' (was e38ef1e)# git tag v1.0# git push origin :refs/tags/v0.8 #远程删除v0.8Enter passphrase for key '/root/.ssh/id_rsa':To git@github.com:Tobewont/mylinux.git 服务端刷新一下,可以看到刚刚标签0.8已经被删除。 SVN可以使用别名,GIT也支持别名,命令支持Tab键补全。以下别名自定义。 # echo "lzxlzx" > 4.txt # git add 4.txt # git config --global alias.ci commit #给commit设置别名ci# git ci -m "add 4.txt"[master 200f1f5] add 4.txt # git config --global alias.br branch #给branch设置别名br# git br # git config --global alias.ch checkout #给checkout设置别名ch# git br # git config --list |grep alias #查看设置的别名alias.ci=commit # git config --list #查看所有的配置push.default=simple # git config --global --unset alias.br #取消别名br# git brgit: 'br' is not a git command. See 'git --help'. #提示br不是一个命令Did you mean one of these? Github毕竟是公开的,而私有仓库又得花钱买。所以我们可以想办法搭建一个私有的仓库,只有自己公司使用的。Gitlab是一个不错的选择,在介绍它之前,先讲一下命令行的git服务器。 # yum install -y git # useradd -s /usr/bin/git-shell git 设置shell为 # cd /home/git/# mkdir .ssh# touch .ssh/authorized_keys #用来存放客户端机器的公钥# chmod 600 !$chmod 600 .ssh/authorized_keys# chown -R git:git .ssh # cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDqWlFPl3JwzR3AiJgolBthMJradp2r1UekJZnnU5hVjDb+pZ72YQUfNdatuUMr96avQYsF+V61sOc/cxa3YPn35n36TW8P+u7FMxZf31eqMatcHG/AWvjW0UsDw+zQrBr5414mj+AIYQgj0GtDIQJbfifGizK7i9UPLy7oW3Ss7+G2+fqhJ2hIo6qTSBHwSdN3rn9ypL0dPIEqJyaaBUpg5a5JKv3KHO5EyJt6Z787SPf3snKddQNpLkgoQ8yPcbZQ3BE5gt6DapMMpLEUUR2adIfe0rWqcDr4Gp9QTW0u+/LgFI6I1UKdTVYvU2UkpUf4WEp+6Q8AROasXxljrNC1 root@lzx 粘贴到服务端刚刚新建的authorized_keys文件中 # vim .ssh/authorized_keysssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDqWlFPl3JwzR3AiJgolBthMJradp2r1UekJZnnU5hVjDb+pZ72YQUfNdatuUMr96avQYsF+V61sOc/cxa3YPn35n36TW8P+u7FMxZf31eqMatcHG/AWvjW0UsDw+zQrBr5414mj+AIYQgj0GtDIQJbfifGizK7i9UPLy7oW3Ss7+G2+fqhJ2hIo6qTSBHwSdN3rn9ypL0dPIEqJyaaBUpg5a5JKv3KHO5EyJt6Z787SPf3snKddQNpLkgoQ8yPcbZQ3BE5gt6DapMMpLEUUR2adIfe0rWqcDr4Gp9QTW0u+/LgFI6I1UKdTVYvU2UkpUf4WEp+6Q8AROasXxljrNC1 root@lzx # ssh git@192.168.100.160The authenticity of host '192.168.100.160 (192.168.100.160)' can't be established. # cd /data/# mkdir /data/gitroot/# cd !$cd /data/gitroot/# git init --bare sample.git #创建sample.git裸仓库Initialized empty Git repository in /data/gitroot/sample.git/# lssample.git# chown -R git:git sample.git 裸仓库没有工作区,因为服务器上的git仓库纯粹是为了共享,所以不让用户直接登录到服务器上更改工作区,并且服务器上的git仓库通常都是以.git结尾。 对于客户端来说,git服务器不需要开发人员直接登录,它仅仅是充当着服务器的角色,就像github一样,平时的操作都是在自己的PC上进行操作的。 客户端怎么使用git服务器呢? 1. 首先要把客户端上的公钥放到git服务器的/home/git/.ssh/authorized_keys文件里 示例: # git clone git@192.168.100.160:/data/gitroot/sample.gitCloning into 'sample'... # cp /etc/init.d/README .# git add .# git commit -m "add new file"[master (root-commit) 7ee55ab] add new file # cd /tmp/# git clone git@192.168.100.160:/data/gitroot/sample.git #换个目录克隆仓库Cloning into 'sample'... # echo "ufhueirhfi" > fff.txt # git add fff.txt # git commit -m "add fff.txt"[master bb1f8d4] add fff.txt 这样就实现了公司内部人员的协同操作,这是一种简单的git服务器的搭建方法,适用于开发人员较少的公司。 更多资料参考: Git使用详细教程
1 file changed, 1 insertion(+), 2 deletions(-)# git log #查看变更记录commit 129107fc060b196b813d5070b0c80de1f7b450c1
Author: lzx
add 1.txt agin
commit 70335c6f9906c1e171fbad0a5146fc6084779ee7
Author: lzx
add 1.txt agin
commit dd8aa2f8aed7d30d7163b1e0c49d3f2001e9ea81
Author: lzx
add 1.txt# git log --pretty=oneline #以一行形式显示变更记录129107fc060b196b813d5070b0c80de1f7b450c1 add 1.txt agin
70335c6f9906c1e171fbad0a5146fc6084779ee7 add 1.txt agin
dd8aa2f8aed7d30d7163b1e0c49d3f2001e9ea81 add 1.txt #-m 指定解释说明字段,可以在这里显示出来,比较重要
dd8aa2f8aed7d30d7163b1e0c49d3f2001e9ea81 add 1.txt #变成两行了,回退之后最新的版本消失
129107f HEAD@{1}: commit: add 1.txt agin
70335c6 HEAD@{2}: commit: add 1.txt agin
dd8aa2f HEAD@{3}: commit (initial): add 1.txt# git reset --hard 129107f #再回退到最新版本HEAD is now at 129107f add 1.txt agin# git log --pretty=oneline129107fc060b196b813d5070b0c80de1f7b450c1 add 1.txt agin
70335c6f9906c1e171fbad0a5146fc6084779ee7 add 1.txt agin
dd8aa2f8aed7d30d7163b1e0c49d3f2001e9ea81 add 1.txt #可以看到已经回到了最新版本
dkmklwdo3ofker
gjrkghrjlhkgrlh
nfbgfrjegbjerk
fejkfnergner
frjgkoerhgi# git add 1.txt # git reset HEAD 1.txt #撤销add操作,回到提交前的状态Unstaged changes after reset:
M 1.txt# git checkout -- 1.txt #恢复版本库里的最新版本,撤销了修改# cat 1.txt rggjohktrlhm
dkmklwdo3ofker
gjrkghrjlhkgrlh
D 1.txt# git checkout -- 1.txt #恢复1.txt# ls1.txt
真正删除一个文件# git rm 1.txt #在本地删除1.txtrm '1.txt'# ls# git commit -m "delete 1.txt" #从版本库里删除1.txt[master b7f069a] delete 1.txt
1 file changed, 4 deletions(-)
delete mode 100644 1.txt# git reset HEAD 1.txt # git checkout -- 1.txt #这样再想恢复就无法恢复了error: pathspec '1.txt' did not match any file(s) known to git.
确实想再恢复它# git log --pretty=onelineb7f069a9146fea7e0682266a857708f42ccb460d delete 1.txt
129107fc060b196b813d5070b0c80de1f7b450c1 add 1.txt agin
70335c6f9906c1e171fbad0a5146fc6084779ee7 add 1.txt agin
dd8aa2f8aed7d30d7163b1e0c49d3f2001e9ea81 add 1.txt git reset --hard 129107fc #通过版本字符串,reset恢复HEAD is now at 129107f add 1.txt agin# ls1.txt# cat 1.txtrggjohktrlhm
dkmklwdo3ofker
gjrkghrjlhkgrlh建立远程仓库
www.github.com
,注册账号,创建新的仓库;setting
,左侧选择SSH and GRP keys
,然后右侧选择New SSH key
;
Enter file in which to save the key (/root/.ssh/id_rsa): #直接按Enter键 Enter passphrase (empty for no passphrase): #输入密码,建议留空Enter same passphrase again: #确认密码Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:/jPZgy/prZShxkNduQ1xlOST/pfzYCf2el0JLeQsWCY root@lzx
The key's randomart image is:
+---[RSA 2048]----+| .o+. || E o *.. || = B = || o o X o || S o o = .|| + . o oo|| * o= =o*|| . +B.oo =*|| .+*o..oo|+----[SHA256]-----+# cat .ssh/id_rsa.pub #查看公钥ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDqWlFPl3JwzR3AiJgolBthMJradp2r1UekJZnnU5hVjDb+pZ72YQUfNdatuUMr96avQYsF+V61sOc/cxa3YPn35n36TW8P+u7FMxZf31eqMatcHG/AWvjW0UsDw+zQrBr5414mj+AIYQgj0GtDIQJbfifGizK7i9UPLy7oW3Ss7+G2+fqhJ2hIo6qTSBHwSdN3rn9ypL0dPIEqJyaaBUpg5a5JKv3KHO5EyJt6Z787SPf3snKddQNpLkgoQ8yPcbZQ3BE5gt6DapMMpLEUUR2adIfe0rWqcDr4Gp9QTW0u+/LgFI6I1UKdTVYvU2UkpUf4WEp+6Q8AROasXxljrNC1 root@lzxAdd SSH key
,这样就生成了key,可以让服务端识别linux客户端;
drwxr-xr-x 3 root root 35 Sep 1 08:26 .drwxrwxrwt. 12 root root 4096 Sep 1 08:25 ..drwxr-xr-x 7 root root 119 Sep 1 08:26 .git
-rw-r--r-- 1 root root 10 Sep 1 08:25 README.md# cd .git/# ls .git/branches config description HEAD hooks index info objects refs# vim .git/config #添加下面内容,身份识别,否则commit报错[user]
name = lzx
email = lzx@lzx.com #这里应该填你注册git时使用的用户名和邮箱
# git add README.md# git commit -m "first commit"[master (root-commit) ca836f8] first commit
1 file changed, 1 insertion(+)
create mode 100644 README.md
# git remote add origin https://github.com/Tobewont/mylinux.git# 将当前仓库中的文件推送到远程服务端上# git push -u origin master #将更改推送到远程服务端上Username for 'https://github.com': Tobewont #输入之前注册git的用户名Password for 'https://Tobewont@github.com': #输入之前注册git的密码Counting objects: 3, done.
Writing objects: 100% (3/3), 211 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)To https://github.com/Tobewont/mylinux.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
1 file changed, 1 insertion(+)
create mode 100644 2.txt
# git push -u origin masterEnter passphrase for key '/root/.ssh/id_rsa': #输入之前生成SSH时输入的密码即可,不用再输入用户名和密码Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 266 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)To git@github.com:Tobewont/mylinux.git
ca836f8..50590ed master -> master
Branch master set up to track remote branch master from origin.克隆远程仓库
Clone or download
按钮,复制链接:git@github.com:Tobewont/mylinux.git
。
Enter passphrase for key '/root/.ssh/id_rsa': #输入之前配置生成SSH时输入的密码,下面类似remote: Counting objects: 6, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Receiving objects: 100% (6/6), done.# lsadmin mylinux myproject# ls mylinux/2.txt README.md
bbbbbb
cccccc# cd mylinux/# git add README.md # git commit -m "change readme.md"[master e38ef1e] change readme.md
1 file changed, 3 insertions(+)# git push #推送到服务端Enter passphrase for key '/root/.ssh/id_rsa':Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 283 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)To git@github.com:Tobewont/mylinux.git
50590ed..e38ef1e master -> masterREADME.md
,然后点击Commit changes
按钮提交。
remote: Compressing objects: 100% (2/2), done.
Unpacking objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
From github.com:Tobewont/mylinux
e38ef1e..c24d9bb master -> origin/master
Updating e38ef1e..c24d9bb
Fast-forward
README.md | 4 ++++
1 file changed, 4 insertions(+)# cat README.md # mylinuxaaaaaa
sn
ek
rk
gt #可以看到刚刚的更改bbbbbb
cccccc本地分支管理
lzx
* master #* 号表示当前处于哪一分支
master# ls1.txt
1 file changed, 1 insertion(+)
create mode 100644 2.txt# ls1.txt 2.txt #分支lzx增加了2.txt# git checkout masterSwitched to branch 'master'# ls1.txt #master分支并没有2.txt
lzx
* master# git merge lzx #合并分支Updating 129107f..9a601f6
Fast-forward
2.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 2.txt# ls 1.txt 2.txt #此时就可以查看到lzx分支的内容
当合并时会提示冲突,需要先解决冲突才能继续合并# vim 2.txt #添加下面内容fjfier
frjgire
grjnkjrntg# git add 2.txt# git commit -m "add 2.txt"[master 2852bdc] add 2.txt
1 file changed, 3 insertions(+)# git checkout lzx #切换到lzx分支Switched to branch 'lzx'# vim 2.txt #添加下面内容,和上面不一致即可fefre
gktmgt
gmlhmyj# git add 2.txt# git commit -m "add 2.txt"[lzx 5fd7ae9] add 2.txt
1 file changed, 3 insertions(+)# git checkout master #切换到master分支Switched to branch 'master'# git merge lzx #合并分支Auto-merging 2.txt
CONFLICT (content): Merge conflict in 2.txt
Automatic merge failed; fix conflicts and then commit the result. #提示冲突
fefre
gktmgt
gmlhmyj# git add 2.txt# git commit -m "add 2.txt"[master b23fe49] add 2.txt
1 file changed, 3 insertions(+), 3 deletions(-)# git merge lzxAlready up-to-date. #合并成功# git checkout lzxSwitched to branch 'lzx'# cat 2.txt adfwrfrg
fefre
gktmgt
gmlhmyj
如果分支没有合并,删除之前会提示,那就不合并,强制删除:git branch -D lzx远程分支管理
2. 创建一个dev分支,专门用来开发,只有当需要发布到线上时,才会把dev分支合并到master分支;
3. 开发人员应该在dev的基础上再分支成个人分支,个人分支(在个人PC上)里面开发代码,再合并到dev分支。
Enter passphrase for key '/root/.ssh/id_rsa': #输入之前配置生成SSH时输入的密码remote: Counting objects: 12, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 12 (delta 0), reused 9 (delta 0), pack-reused 0
Receiving objects: 100% (12/12), done.# cd mylinux/# ls2.txt README.md# git branch* master #可以看到只有一个master
c24d9bbd51579bd6bb0b68484935732f61f7783c refs/heads/dev
c24d9bbd51579bd6bb0b68484935732f61f7783c refs/heads/master
Switched to a new branch 'dev'# git branch* dev
master# ls2.txt README.md
gtgtr;gmtkrm# git add 2.txt # git commit -m "add 2.txt"[dev 2bcdc0f] add 2.txt
1 file changed, 3 insertions(+)# git pushEnter passphrase for key '/root/.ssh/id_rsa': #输入之前配置生成SSH时输入的密码Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 287 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)To git@github.com:Tobewont/mylinux.git
c24d9bb..2bcdc0f dev -> dev# 当前在dev分支下,这里可以看出是本地dev分支推送到远程dev分支的,因为master分支没有做任何修改
git push 会把所有分支的变更一同推送到远程,如果只想推送一个分支,使用git push origin branch-name
2. 当本地分支比远程分支多时:
git push 默认只推送本地与远程一致的分支,想要把多出来的分支推送到远程时,使用git push origin branch-name,
如果推送失败,先用git pull拉取远程内容重新提交
dev2
master# git checkout dev2Switched to branch 'dev2'# ls2.txt README.md# echo "lzxlzx" > 3.txt# git add 3.txt # git commit -m "add 3.txt"[dev2 c8dd849] add 3.txt
1 file changed, 1 insertion(+)
create mode 100644 3.txt# git pushfatal: The current branch dev2 has no upstream branch.
To push the current branch and set the remote as upstream, use git push --set-upstream origin dev2 #提示你应该这样git push# git push origin dev2Enter passphrase for key '/root/.ssh/id_rsa':Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 289 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 1 (delta 0)To git@github.com:Tobewont/mylinux.git
* [new branch] dev2 -> dev2 #推送成功标签管理
Update README.mddiff --git a/README.md b/README.md
index 6ca591e..5fc27ec 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,8 @@ # mylinux
aaaaaa
+sn
+ek
+rk
+gt
e38ef1e23a4dbf850dbfbfbe0e3253ccc343c740 change readme.md
50590ed936bf9585318a70156a01846fe78f7e74 add 2.txt
ca836f820e54cde73cfba7f768cb36cbce5c0975 first commit# git log --pretty=oneline --abbrev-commit #查看历史commit,信息字符串简写c24d9bb Update README.md
e38ef1e change readme.md
50590ed add 2.txt
ca836f8 first commit# git tag v0.8 e38ef1e23a #针对commit打标签,commit信息字符串可以简写# git tagv0.8
v1.0# git show v0.8commit e38ef1e23a4dbf850dbfbfbe0e3253ccc343c740 #可以看到,commit信息是我们指定的Author: lzx
change readme.mddiff --git a/README.md b/README.md
index cc9aa45..6ca591e 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,4 @@ # mylinux+aaaaaa
+bbbbbb
+cccccc
v0.8
v1.0# git show v0.1tag v0.1
Tagger: lzx
lzx tag #描述信息commit ca836f820e54cde73cfba7f768cb36cbce5c0975 #指定的commitAuthor: lzx
first commitdiff --git a/README.md b/README.md
new file mode 100644
index 0000000..cc9aa45
--- /dev/null
+++ b/README.md
v1.0
* [new tag] v1.0 -> v1.0
* [new tag] v0.8 -> v0.8 #v1.0已经推送,所以只显示v0.8为[new tag]
- [deleted] v0.8 #可以看到v0.8已经删除git别名
1 file changed, 1 insertion(+)
create mode 100644 4.txt
dev
dev2
* master
dev
dev2
* master# git ch devSwitched to branch 'dev'# git br* dev
dev2
master
alias.br=branch
alias.ch=checkout
user.name=lzx
user.email=lzx@lzx.com
alias.ci=commit
alias.br=branch
alias.ch=checkout
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@github.com:Tobewont/mylinux.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
branch.dev.remote=origin
branch.dev.merge=refs/heads/dev# cat ~/.gitconfig #别名也可以在配置文件中设置[push]
default = simple[user]
name = lzx
email = lzx@lzx.com[alias]
ci = commit
br = branch
ch = checkout
branch
var搭建git服务器
/usr/bin/git-shell
:/usr/bin/git-shell
,目的是为了不让git用户远程登录。
ECDSA key fingerprint is SHA256:teKu3atU+OByPeXXD2xXhyb30vg6nW8ETqqCr785Dbc.
ECDSA key fingerprint is MD5:13:a4:f1:c0:1f:62:65:d4:f4:4e:42:ab:40:f1:36:60.
Are you sure you want to continue connecting (yes/no)? yes //输入yes
Warning: Permanently added '192.168.100.160' (ECDSA) to the list of known hosts.
Enter passphrase for key '/root/.ssh/id_rsa':
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access. #提示说明验证成功Connection to 192.168.100.160 closed.
2. 然后克隆git服务器的sampl.git仓库
3. 进入仓库进行操作,然后push到服务端
Enter passphrase for key '/root/.ssh/id_rsa':warning: You appear to have cloned an empty repository.# lsanaconda-ks.cfg mylinux sample# cd sample/# ls -latotal 0
drwxr-xr-x 3 root root 18 Sep 6 06:06 .dr-xr-x---. 7 root root 276 Sep 6 06:06 ..drwxr-xr-x 7 root root 119 Sep 6 06:06 .git
1 file changed, 27 insertions(+)
create mode 100644 README# git push origin master #不指定分支git push会报错,因为服务端是空仓库Enter passphrase for key '/root/.ssh/id_rsa':Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 781 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)To git@192.168.100.160:/data/gitroot/sample.git
* [new branch] master -> master# git push #再次git push就没问题了Enter passphrase for key '/root/.ssh/id_rsa':Everything up-to-date# lsREADME
Enter passphrase for key '/root/.ssh/id_rsa':remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)Receiving objects: 100% (3/3), done.# cd sample/# lsREADME #发现有刚刚git push的文件
1 file changed, 1 insertion(+)
create mode 100644 fff.txt# git pushEnter passphrase for key '/root/.ssh/id_rsa':Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)To git@192.168.100.160:/data/gitroot/sample.git
7ee55ab..bb1f8d4 master -> master# cd /root/sample/# git pull #拉取仓库内容Enter passphrase for key '/root/.ssh/id_rsa':remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)Unpacking objects: 100% (3/3), done.
From 192.168.100.160:/data/gitroot/sample
7ee55ab..bb1f8d4 master -> origin/master
Updating 7ee55ab..bb1f8d4
Fast-forward
fff.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 fff.txt# lsfff.txt README# cat fff.txt ufhueirhfi