码迷,mamicode.com
首页 > 其他好文 > 详细

Git 初步学习

时间:2018-07-15 23:18:22      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:origin   merge   ...   学习   修改文件   保存   testing   用户组   commit   

学习目标

在linux 上安装Git 服务器

在windows 上安装 Git 客户端

创建Git仓库,git用户

在windows 中获取项目,对项目进行增删改查,更新到服务器

创建两个分支,进行分支修改和代码合并

1. 在linux上安装git服务器

  使用指令:sudo apt-get install git

  安装完成效果:

stark@ubuntu:~$ git --version
git version 2.7.4 

2. 安装Git客户端,下载Git for Windows,安装完成运行Git Bash 输入指令

$ git --version
git version 2.15.0.windows.1

3. 服务器端创建Git 仓库

stark@ubuntu:~/data/git$ mkdir gittest.git
stark@ubuntu:~/data/git$ git init gittest.git
Initialized empty Git repository in /home/stark/data/git/gittest.git/.git/

  以为git 默认禁止push代码需要配置 .git/config 文件添加 (push失败后,网上查询)

[receive]
 denyCurrentBranch = ignore

  添加git用户和git用户组,并且修改权限

#创建用户组
sudo groupadd gituser
#创建用户
sudo useradd gituser -g gituser
sudo passwd gituser
sudo mkdir /home/gituser
sudo chown -R gituser /home/gituser
#修改git仓库权限
sudo chown -R gituser.gituser gittest.git

4. 客户端抓取项目,进行增删改查

# 下载项目
$ git clone gituser1@192.168.195.149:/home/stark/data/git/gittest.git
Cloning into ‘gittest‘...
gituser1@192.168.195.149‘s password:
warning: You appear to have cloned an empty repository.
$ git init gittest/
Reinitialized existing Git repository in D:/GitTest/gittest/.git/
#创建文件,上传
$ echo "hellohit">hello.txt
$ git add hello.txt
$ git commit -m ‘hello‘
$ git push origin master
#修改文件,上传
$ echo ‘hellogit‘ >hello.txt
$ git add hello.txt
$ git commit -m ‘modify‘
$ git push origin master
#在另一处确认修改
$ git pull
#删除文件
$ rm hello.txt
$ git rm hello.txt
$ git commit -m ‘remove‘
$ git push origin master
#查询日志 关键信息是commit id
$ git log
#回滚到某一版本(本地版本)
$ git reset –hard <commit id>

  在仓库下没有发现上床的文件,因为git仓库保存的是快照。在另外一处重新抓取项目,可以发现文件已经被上传了

5. 使用Git的分支功能

#添加一个空白文件
$ touch branch.txt
$ git add status
$ git commit -m ‘add file‘
$ git push origin master
#创建分支
$ git branch testing1
$ git branch testing2
#查看本地分支
$ git branch
#切换到分支进行文件修改,push到远程分支
$ git checkout testing1
$ echo ‘testing1‘> branch.txt
$ git add branch.txt
$ git commit -m ‘testing1‘
$ git push origin testing1

$ git checkout testing2
$ echo ‘testing2‘> branch.txt
$ git add branch.txt
$ git commit -m ‘testing2‘
$ git push origin testing2
#查看远程分支
$ git branch -r
#合并分支1,上传,删除分支
$ git checkout master
$ git merge testing1 # Fast-forward 表示没有冲突
$ git push origin master
$ git push origin --delete testing1
$ git branch -d testing1 
#合并分支2,解决冲突,上传删除分支
$ git merge testing2  #CONFLICT 表示合并出现冲突
#解决冲突后像修改文件一样上传就行

Git 初步学习

标签:origin   merge   ...   学习   修改文件   保存   testing   用户组   commit   

原文地址:https://www.cnblogs.com/starktan/p/9315200.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!