最近由svn切换到git,总结了一下svn和git之间的优缺点,以及在工作中比较常见的git命令,如果有不对的地方,欢迎批评指正。
git和svn的区别:
git是一个分布式版本控制系统,每一个终端都可以建立仓库,每个仓库都可以独立工作,当然为了方便交换大家的修改,git也可以有一个服务器,这个服务器仓库和终端仓库地位一样,大家可以提交自己的修改到这个服务器,其他伙伴可以从这个服务器更新修改到自己的仓库
svn是一个集中式版本控制系统,必须有一个单一的集中管理的中心服务器,而且这个服务器挂了,就无法进行访问,提交,更新等任何操作
在Linux下git安装
ubuntu系统:sudo apt-get install git
其他Linux版本:先从git官网下载压缩包,解压,然后进入解压目录依次进行配置./config,编译make,安装sudo make install
配置
ubuntu@VM-0-3-ubuntu:~$ git config --global user.name mcchen
ubuntu@VM-0-3-ubuntu:~$ git config --global user.email 1350106xxx@qq.com
常用命令
git clone
简单的说就是从远程仓库克隆一份到本地仓库,后续就可以对本地仓库版本进行修改提交等工作了
ubuntu@VM-0-3-ubuntu:~$ git clone https://github.com/github-mcchen/my_git.git
Cloning into 'my_git'...
remote: Enumerating objects: 20, done.
remote: Counting objects: 100% (20/20), done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 20 (delta 3), reused 16 (delta 1), pack-reused 0
Unpacking objects: 100% (20/20), done.
Checking connectivity... done
还有另外一种方法,就是想把自己的本地仓库放到远程仓库去,这时候就可以使用git init和git remote将本地仓库和远程仓库关联起来,这里需要主要的是,git remote add origin只是添加一个别名为origin的远程仓库
ubuntu@VM-0-3-ubuntu:~$ mkdir git_create
ubuntu@VM-0-3-ubuntu:~$ cd git_create/
ubuntu@VM-0-3-ubuntu:~$ git init
Initialized empty Git repository in /home/ubuntu/.git/
ubuntu@VM-0-3-ubuntu:~/git_create$ ls -a
. .. .git
ubuntu@VM-0-3-ubuntu:~/git_create$ git remote add origin https://github.com/github-mcchen/git_create.git
接下来对仓库分支管理进行说明,上面说了git init和git remote仓库,但是此时仓库里没有内容的,需要手动添加文件进去
ubuntu@VM-0-3-ubuntu:~/git_create$ echo "#create a git" > README.txt
ubuntu@VM-0-3-ubuntu:~/git_create$ git add README.txt
ubuntu@VM-0-3-ubuntu:~/git_create$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.txt
ubuntu@VM-0-3-ubuntu:~/git_create$ git commit README.txt -m "新增读文件"
[master (root-commit) c0693aa] 新增读文件
1 file changed, 1 insertion(+)
create mode 100644 README.txt
ubuntu@VM-0-3-ubuntu:~/git_create$ git branch -a
* master
ubuntu@VM-0-3-ubuntu:~/git_create$ git push origin master
ubuntu@VM-0-3-ubuntu:~/git_create$ git branch -a
* master
remotes/origin/master
几条常用的命令
git add user.rb-------------------添加文件到版本库
git rm user.rb--------------------从版本库删除文件
git status--------------------------当前分支状态
git commit -m “story”----------提交修改到版本库
git branch -a---------------------查看仓库所有分支,不加-a选项时查看当前工作分支
git branch new_branch ------从当前分支创建一个新的分支new_branch
git push origin master--------推送本地master到origin
git pull origin master----------更新远程分支master到本地
上面主要讲了如何创建仓库和推送分支到仓库,下面将示范如何基于当前分支检出其它分支
ubuntu@VM-0-3-ubuntu:~/git_create$ git checkout -b develop master
ubuntu@VM-0-3-ubuntu:~/git_create$ git branch -a
* develop
master
remotes/origin/master
ubuntu@VM-0-3-ubuntu:~/git_create$ vi README.txt
#create a git
#checkout a branch
ubuntu@VM-0-3-ubuntu:~/git_create$ git commit README.txt -m "checkout a branch"
[develop e2b25fe] checkout a branch
1 file changed, 1 insertion(+)
ubuntu@VM-0-3-ubuntu:~/git_create$ git log
commit e2b25fed1734863dc27b432d364a2d56523f257b
Author: mcchen <1350106562@qq.com>
Date: Fri Aug 16 21:04:05 2019 +0800
checkout a branch
commit c0693aa4bf260c843128a39e026c3a0934f41e28
Author: mcchen <1350106562@qq.com>
Date: Fri Aug 16 20:40:47 2019 +0800
新增读文件
几条常用命令如下
git checkout -b develop master-----基于master分支创建develop分支
git checkout master--------------------切换到master分支
git log--------------------------------------查看分支提交记录
一般工作中都会在develop上进行修改然后合并到master分支,下面演示如何合并并且回退版本
ubuntu@VM-0-3-ubuntu:~/git_create$
ubuntu@VM-0-3-ubuntu:~/git_create$ git branch -a
* develop
master
remotes/origin/master
ubuntu@VM-0-3-ubuntu:~/git_create$ git checkout master
Switched to branch 'master'
ubuntu@VM-0-3-ubuntu:~/git_create$ git merge develop
ubuntu@VM-0-3-ubuntu:~/git_create$ git log
commit e2b25fed1734863dc27b432d364a2d56523f257b
Author: mcchen <1350106562@qq.com>
Date: Fri Aug 16 21:04:05 2019 +0800
checkout a branch
commit c0693aa4bf260c843128a39e026c3a0934f41e28
Author: mcchen <1350106562@qq.com>
Date: Fri Aug 16 20:40:47 2019 +0800
新增读文件
ubuntu@VM-0-3-ubuntu:~/git_create$ git reset --hard c0693aa4bf260c843128a39e026c3a0934f41e28
HEAD is now at c0693aa 新增读文件
ubuntu@VM-0-3-ubuntu:~/git_create$ git log
commit c0693aa4bf260c843128a39e026c3a0934f41e28
Author: mcchen <1350106562@qq.com>
Date: Fri Aug 16 20:40:47 2019 +0800
新增读文件
几条常用命令
git merge branch ----------------合并branch分支
git reset --hard revision ------回退到revision版本
git rebase branch ----------------变基分支到branch
git tag revision xxxx-------------------给版本打上标签
git checkout user.rb -------------回滚到上一次提交
以上就是比较常用的几条命令,当然git还有很多扩展的命令,这里就不一一列举,谢谢大家观看。