git操作

简介:

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

安装:
bash-completion:tab键补全命令
[root@centos7-2 ~]# sudo yum install git bash-completion

1、配置个人信息

[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 = jacker
email = jack@163.com

3、创建仓库推送文件
[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.txt
1 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.txt

3、[root@centos7-2 gitroot]# git status
#On branch master
nothing to commit, working directory clean

4、只更改不提交
[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.test1
2.test2
3.test3
4.test4

[root@centos7-2 gitroot]# git log
git操作

简写:
[root@centos7-2 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@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.test1
2.test2
3.test3
4.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的密码

git操作

git操作

新增资源
git操作

git操作

查看
git操作

克隆远程仓库

[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 test1
1 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 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 git@github.com:sundysj/jack-git.git
430a41c..7c80b77 master -> master

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

在客户端上拉去最新的文件
[root@centos7-2 jack-git]# git pull origin master

分支管理

*:表示当前的所在分支
查看分支
[root@centos7-2 jack-git]# git branch

  • master

创建分支
[root@centos7-2 jack-git]# git branch 
jack

  • master

切换分支
[root@centos7-2 jack-git]# git checkout jack
Switched to branch 'jack'
[root@centos7-2 jack-git]# git branch

  • jack
    master

jack分支上创建文件
[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上的分支
git操作

分支的合并和删除

1、先切换到master上
[root@centos7-2 jack-git]# git checkout master

2、合并
[root@centos7-2 jack-git]# git merge jack

3、合并原则
主分支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 branch

  • master

分支使用原则

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

创建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]# ls
README.md test1.txt test2 test3 testjack1.txt

找出来:
[root@centos7-2 jack-git]# git stash list
stash@{0}: WIP on master: 1f6aec8 ch te
[root@centos7-2 jack-git]# git stash apply stash@{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

#
[root@centos7-2 jack-git]# ls
3.txt README.md test1.txt test2 test3 testjack1.txt

删除保存的内容:git stash drop stash@{0}

远程上创建分支
git操作

客户端上更新
[root@centos7-2 jack-git]# git pull origin test

查看远程仓库信息
[root@centos7-2 jack-git]# git remote -v
origin 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 origin
2742321ca81a99c464d107ebbb405f76264d462e HEAD
1f6aec8e78685142779c8862940a2e55fb516eca refs/heads/jack
2742321ca81a99c464d107ebbb405f76264d462e refs/heads/master
be35aae7e71a8149dcace5f6a151ccd1f1249565 refs/heads/test

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

查看别名配置文件信息
[root@centos7-2 jack-git]# git config --list |grep alias











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



相关文章
|
27天前
|
开发工具 git
记IDEA Git版本回退并push到远程操作
记IDEA Git版本回退并push到远程操作
29 1
记IDEA Git版本回退并push到远程操作
|
1月前
|
开发工具 git 开发者
|
1月前
|
开发工具 git
web后端-IDEA的Git操作
web后端-IDEA的Git操作
|
2月前
|
Linux 网络安全 开发工具
Git拉取代码的完整示例操作
Git拉取代码的完整示例操作
65 0
|
3月前
|
Shell 开发工具 git
git相关操作
git相关操作
|
4月前
|
开发工具 git
Git操作远程仓库及解决合并冲突
Git操作远程仓库及解决合并冲突
72 0
|
9天前
|
Linux 开发工具 git
还不会 Git 子模块操作?一文教你学会 git submodule 的增、删、改、查!
还不会 Git 子模块操作?一文教你学会 git submodule 的增、删、改、查!
|
1月前
|
开发工具 git
|
1月前
|
存储 持续交付 开发工具
Git操作入门
Git是一个的开源分布式版本控制系统,它已经被广泛应用于软件开发、文档管理、代码托管等领域,成为当今最流行的版本控制系统之一。Git通过高效地管理文件的变化,使得团队协作更加高效,错误率更低。本文将介绍Git的工作原理、基本命令和常见用法等内容。
21 0
Git操作入门

相关实验场景

更多