git操作

git介绍
与svn不同的是,svn是集中式管理,当自己主机上修改了文件,必须提交到服务器,其他人才能提交,不然冲突
git是自己本机也可以作为仓库,也有当做服务器,分布式管理

安装:
bash-completion:tab键补全命令
[[email protected] ~]# sudo yum install git bash-completion

1、配置个人信息

[[email protected] ~]# git config --global user.name "jacker"
[[email protected] ~]# git config --global user.email "[email protected]"

2、查看用户名和email
[[email protected] ~]# cat /root/.gitconfig 
[user]
name = jacker
email = [email protected]

3、创建仓库推送文件
[[email protected] ~]# mkdir /home/gitroot
[[email protected] ~]# cd /home/gitroot/
初始化操作
[[email protected] gitroot]# git init 
Initialized empty Git repository in /home/gitroot/.git/
[[email protected] gitroot]# vim test.txt
[[email protected] gitroot]# git add test.txt 
[[email protected] gitroot]# git commit -m "add a test file test.txt"
[master (root-commit) ee26be4] add a test file test.txt
1 file changed, 1 insertion(+)
create mode 100644 test.txt
[[email protected] gitroot]# vim test.txt
当更新文件时,没有提交,会提示你add,commit

[[email protected] 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.txt

3、[[email protected] gitroot]# git status
#On branch master
nothing to commit, working directory clean

4、只更改不提交
[[email protected] gitroot]# vim test.txt 
[[email protected] gitroot]# git add test.txt
以下是将test.txt给提交了,更新到最新的版本
[[email protected] gitroot]# git reset HEAD test.txt

版本变更

[[email protected] gitroot]# cat test.txt 
1.test1
2.test2
3.test3
4.test4

[[email protected] gitroot]# git log
git操作

简写:
[[email protected] gitroot]# git log --pretty=oneline
2b01244ccecfc72be09c8314d4050a023d4f8a32 6.txt
6b93439a630a9992eda8355bd421a23a3d90a269 5.txt
94eac572484320bfe534d92013be17c147907ef5 add a test file test.txt
ee26be4b4f753d09741b3685224d00902234936e add a test file test.txt

恢复至哪个版本--git reset

[root[email protected] gitroot]# git reset --hard 94eac
HEAD is now at 94eac57 add a test file test.txt
[[email protected] gitroot]# cat test.txt 
1.test1
2.test2
3.test3
4.test4

恢复到哪个版本后,当前版本就是该版本

文件的删除操作
[[email protected] gitroot]# git rm test.txt
[[email protected] gitroot]# git commit -m 'rm test.txt'

创建远程仓库

GitHub官网:github.com
注册账号并**,然后开始创建主机的仓库!
创建完成后,添加key:
点击浏览器右上角头像——setting——SSH and GPG keys(选择SSH keys)——在服务器(虚拟机)执行ssh-******命令生成**对(/root/.ssh/id_rsa-私钥, /root/.ssh/id_rsa.pub-公钥)——将公钥复制到浏览器后点“添加”。

以下是输入github的密码

git操作

git操作

新增资源
git操作

git操作

查看
git操作

克隆远程仓库

[[email protected] home]# git clone [email protected]:sundysj/jack-git.git

编辑
[[email protected] jack-git]# vim test1.txt

[[email protected] jack-git]# git add test1.txt
[[email protected] jack-git]# git commit -m 'ad test1'
[master 7c80b77] ad test1
1 file changed, 4 insertions(+)
create mode 100644 test1.txt
#提交到远程git push
[[email protected] jack-git]# git push 
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and 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 [email protected]:sundysj/jack-git.git
430a41c..7c80b77 master -> master

现在在github远端上新建一个文件
git操作

在客户端上拉去最新的文件
[[email protected] jack-git]# git pull origin master

分支管理

*:表示当前的所在分支
查看分支
[[email protected] jack-git]# git branch

  • master

创建分支
[[email protected] jack-git]# git branch 
jack

  • master

切换分支
[[email protected] jack-git]# git checkout jack
Switched to branch 'jack'
[[email protected] jack-git]# git branch

  • jack
    master

jack分支上创建文件
[[email protected] jack-git]# vim testjack1.txt
[[email protected] jack-git]# git add testjack1.txt
[[email protected] jack-git]# git commit -m 'ad tet'

将这个分支推送到远程github上
[[email protected] jack-git]# git push --set-upstream origin jack

查看github上的分支
git操作

分支的合并和删除

1、先切换到master上
[[email protected] jack-git]# git checkout master

2、合并
[[email protected] jack-git]# git merge jack

3、合并原则
主分支master不变,开发人员就在dev分支上开发后合并到master分支上

删除分支

[[email protected] 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:强制删除

[[email protected] jack-git]# git branch -D jack
Deleted branch jack (was 06beb7b).
You have new mail in /var/spool/mail/root
[[email protected] jack-git]# git branch

  • master

分支使用原则

master分支是非常重要的,线上发布代码用这个分支,平时开发代码不要在该分支操作;

创建dev分支,专门用作开发,只有到发布到线上之前再把dev合并到master上

开发人员应该在dev分支的基础上再分支成个人分支,自己的分支(在自己的pc上)里面开发代码,然后合并到dev上

保留没有做完的工作

场景:
当你正在进行项目中某一部分的工作,里面的东西处于一个比较杂乱的状态,而你想转到其他分支上进行一些工作中。问题是你不想提交进行了一半的工作,否则以后你无法回到这个工作点。解决问题的办法就是:git stash(存储)命令。

比如,我们在jack分支,编辑一个新的文件3.txt,此时我们要去其他分支。
[[email protected] jack-git]# vim 3.txt
[[email protected] jack-git]# git add 3.txt
#此时我不想提交,想切换到其他分支去看

[[email protected] jack-git]# git status
#无文件要提交,干净的工作区
但是你发现3.txt不见了?
[[email protected] jack-git]# ls
README.md test1.txt test2 test3 testjack1.txt

找出来:
[[email protected] jack-git]# git stash list
[email protected]{0}: WIP on master: 1f6aec8 ch te
[[email protected] jack-git]# git stash apply [email protected]{0}

On branch master

Your branch is ahead of 'origin/master' by 5 commits.

(use "git push" to publish your local commits)

#

Changes to be committed:

(use "git reset HEAD <file>..." to unstage)

#

new file: 3.txt

#
[[email protected] jack-git]# ls
3.txt README.md test1.txt test2 test3 testjack1.txt

删除保存的内容:git stash drop [email protected]{0}

远程上创建分支
git操作

客户端上更新
[[email protected] jack-git]# git pull origin test

查看远程仓库信息
[[email protected] jack-git]# git remote -v
origin [email protected]:sundysj/jack-git.git (fetch)
origin [email protected]:sundysj/jack-git.git (push)

查看远程分支信息:
[[email protected] jack-git]# git ls-remote origin
2742321ca81a99c464d107ebbb405f76264d462e HEAD
1f6aec8e78685142779c8862940a2e55fb516eca refs/heads/jack
2742321ca81a99c464d107ebbb405f76264d462e refs/heads/master
be35aae7e71a8149dcace5f6a151ccd1f1249565 refs/heads/test

推送本地分支到远程:
[[email protected] jack-git]# git add test1
[[email protected] jack-git]# git commit -m 'ch test1'
[test d3e19a6] ch test1
1 file changed, 5 insertions(+)
[[email protected] jack-git]# git push origin test

查看别名配置文件信息
[[email protected] jack-git]# git config --list |grep alias











本文转自方向对了,就不怕路远了!51CTO博客,原文链接: http://blog.51cto.com/jacksoner/2049875,如需转载请自行联系原作者