开源中文网

您的位置: 首页 > Linux应用 > Git > 正文

linux中git命令基础篇

来源: 网络整理  作者: 佚名

git命令基础可以帮助到各位基本撑握到git的一个使用了,下面整理的关于git命令的用法比较实用了各位朋友可以和小编一起来看看哦。

之前一直在玩耍svn,git最近几年已经灰常的流行了,稍微尝试玩耍下:
[root@git ~]# git config --global user.name "Byrd"    #全局设置用户
[root@git ~]# git config --global user.email "root#t4x.org"    #全局设置用户邮箱
[root@git ~]# git config --global color.ui true    #设置颜色
[root@git ~]# git config --list    #列出已设置项
user.name=Byrd
user.email=root@t4x.org
color.ui=true
[root@git ~]# mkdir /git    #建立一个空目录
[root@git ~]# cd /git/
[root@git git]# git init    #创建一个空仓库
Initialized empty Git repository in /git/.git/

[root@git git]# cat readme.txt
1 : hello,world
[root@git git]# git status    #查看当前master分支情况,因为新建立了文件夹,所有会提示
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       readme.txt
nothing added to commit but untracked files present (use "git add" to track)
[root@git git]# git add readme.txt    #将文件放入暂存区
[root@git git]# git commit -m "hello,world"    #将文件提交
[master (root-commit) 41c7830] hello,world
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 readme.txt
[root@git git]# git status
# On branch master
nothing to commit (working directory clean)
[root@git git]# git diff readme.txt    #比较当前修改和仓库文件对比
diff --git a/readme.txt b/readme.txt
index bcfe5e3..d3fded2 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,2 @@
1 : hello,world
+2 : 2th
[root@git git]# git add readme.txt    #可以使用git add .表示当前目录所有文件
[root@git git]# git commit readme.txt -m "2th"    #可以改成git commit -m "2th"
[master 2b91b7b] 2th
1 files changed, 1 insertions(+), 0 deletions(-)
[root@git git]# git log
commit 2b91b7bf0acaef5603dc4b2e49371c3fedf93d0e
Author: Byrd <root@t4x.org>
Date:   Tue Aug 30 06:14:06 2016 +0800

    2th

commit 41c78306340288daee42ea98727efac8941b624f
Author: Byrd <root@t4x.org>
Date:   Tue Aug 30 06:07:19 2016 +0800

    hello,world

[root@git git]# cat readme.txt
1 : hello,world
2 : 2th
[root@git git]# git reset --hard HEAD^    #回退一个版本git reset --hard HEAD^^ 几个^表示几个版本
HEAD is now at 41c7830 hello,world
[root@git git]# cat readme.txt       
1 : hello,world
[root@git git]# git reflog     #可以查看所有分支的所有操作记录
41c7830 HEAD@{0}: HEAD^: updating HEAD
6471806 HEAD@{1}: commit: 2th
41c7830 HEAD@{2}: HEAD^: updating HEAD
2b91b7b HEAD@{3}: commit: 2th
[root@git git]# git reset --hard 2b91b7b    #回退到某个固定修改
HEAD is now at 2b91b7b 2th
[root@git git]# cat readme.txt
1 : hello,world
2 : 2th
[root@git git]# git commit -m "3th" readme.txt
[master afe6480] 3th
1 files changed, 1 insertions(+), 0 deletions(-)
[root@git git]# cat readme.txt
1 : hello,world
2 : 2th
3 : 3th
4 : 4th
[root@git git]# git checkout readme.txt    #git checkout -- readme.txt    未提交的可以checkout
[root@git git]# cat readme.txt 
1 : hello,world
2 : 2th
3 : 3th
进阶篇:
[root@git git]# ssh-keygen -t rsa -C "pub#t4x.org"    #设置key,可以到github上面提交key后,把本地的提交到github
[root@git git]# cat /root/.ssh/id_rsa.pub
[root@git git]# tail -1 /etc/hosts
1.1.1.103   gitlab.ssh.t4x.org
[root@git git]# git remote add origin git@gitlab.ssh.t4x.org:byrd/t4x.git    #我本地搭建了gitlab,本地测试
[root@git git]# cat .git/config | grep t4x.org
        url = git@gitlab.ssh.t4x.org:byrd/t4x.git




[pub@git project]$ git push -u origin master    #本地数据提交到仓库
The authenticity of host 'gitlab.ssh.t4x.org (1.1.1.103)' can't be established.
RSA key fingerprint is 26:e5:f9:83:91:b7:96:ed:48:01:b8:3a:00:f0:16:71.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitlab.ssh.t4x.org,1.1.1.103' (RSA) to the list of known hosts.
Counting objects: 3702, done.
Compressing objects: 100% (3556/3556), done.
Writing objects: 100% (3702/3702), 26.49 MiB | 7.15 MiB/s, done.
Total 3702 (delta 453), reused 0 (delta 0)
To git@gitlab.ssh.t4x.org:publicgroup/project-pub.git
* [new branch]      master -> master
Branch master set up to track remote branch master from origin.
[pub@git project]$ git push -u origin master
To git@gitlab.ssh.t4x.org:publicgroup/project-pub.git
! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@gitlab.ssh.t4x.org:publicgroup/project-pub.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'Note about
fast-forwards' section of 'git push --help' for details.

[pub@git project]$ git pull origin master    #将数据拉回本地
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From gitlab.ssh.t4x.org:publicgroup/project-pub
* branch            master     -> FETCH_HEAD
Updating 7803093..7d907d3
Fast-forward
go.php |    1 +
1 files changed, 1 insertions(+), 0 deletions(-)
[pub@git project]$ git add go.php
[pub@git project]$ git commit go.php -m "delete 10 lines"
[master 51f4544] delete 10 lines
1 files changed, 0 insertions(+), 1 deletions(-)
[pub@git project]$ git push -u origin master  
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 292 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@gitlab.ssh.t4x.org:publicgroup/project-pub.git
   7d907d3..51f4544  master -> master
Branch master set up to track remote branch master from origin.
常规操作方式:
一、Git global setup 全局配置

git config --global user.name "int"
git config --global user.email "int@t4x.org"
二、Create a new repository 建立一个新仓库

git clone git@gitlab.ssh.t4x.org:int/1111.git
cd 1111
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
三、Existing folder or Git repository 已经有仓库

cd existing_folder
git init
git remote add origin git@gitlab.ssh.t4x.org:int/1111.git
git add .
git commit
git push -u origin master

[int@git project-pub]$ git push -u origin master
To git@gitlab.ssh.t4x.org:publicgroup/project-pub.git
! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@gitlab.ssh.t4x.org:publicgroup/project-pub.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'Note about
fast-forwards' section of 'git push --help' for details.
[int@git project-pub]$ git pull origin master
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From gitlab.ssh.t4x.org:publicgroup/project-pub
* branch            master     -> FETCH_HEAD
Updating d27bb42..f2a4d14
Fast-forward
go.php |    1 -
1 files changed, 0 insertions(+), 1 deletions(-)

Tags:命令 基础
关于开源中文网 - 联系我们 - 广告服务 - 网站地图 - 版权声明