一些常用的Git命令

整理了非常由于的Git一些常用命令,Git是目前世界上最先进的分布式版本控制系统。由于它就没有中央服务器的,每个人的电脑就是一个完整的版本库,这样,工作的时候就不需要联网了,因为版本都是在自己的电脑上。既然每个人的电脑都有一个完整的版本库,那多个人如何协作呢?比如说自己在电脑上改了文件A,其他人也在电脑上改了文件A,这时,你们两之间只需把各自的修改推送给对方,就可以互相看到对方的修改了。

1、init,创建一个git仓库
[root@linuxidc git]# cd /usr/local/
[root@linuxidc local]# mkdir github
[root@linuxidc local]# cd github
[root@linuxidc git]# git init
Initialized empty Git repository in /usr/local/github/.git/
[root@linuxidc git]# ls -a
.  ..  .git

2、status,检查状态
[root@linuxidc git]# touch code1.py
[root@linuxidc git]# git status 
# On branch master
# Untracked files:
#  (use "git add <file>..." to include in what will be committed)
#
#  code1.py
nothing added to commit but untracked files present (use "git add" to track)

3、add,文件添加到索引,也就是接受版本控制

[root@linuxidc git]# git add code1.py 
[root@linuxidc git]# git status 
# On branch master
# Changes to be committed:
#  (use "git reset HEAD <file>..." to unstage)
#
#  new file:  code1.py
#

4、commit,提交
[root@linuxidc git]# git commit -m "New add code1.py" #第一次会提示我们输入信息,名字与邮件
[master 7894d32] New add code1.py
 Committer: root <root@linuxidc.(none)>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
 
    git config --global user.name "Your Name"
    git config --global user.email you@example.com
 
If the identity used for this commit is wrong, you can fix it with:
 
    git commit --amend --author='Your Name <you@example.com>'
 
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 code1.py
[root@linuxidc git]# git config --global user.name sunshine                #名字
[root@linuxidc git]# git config --global user.email sunshine@git.com      #邮件地址
[root@linuxidc git]# git config --global color.ui                          #auto显示颜色

5、branc 创建、查看、删除分支
[root@linuxidc git]# git branch
* master
[root@linuxidc git]# git branch dev
[root@linuxidc git]# git branch
  dev
* master
[root@linuxidc git]# git branch -d dev
Deleted branch dev (was 7894d32).
[root@linuxidc git]# git branch
* master
 
#删除分支会有另外一种诡异的现象发生
[root@linuxidc git]# git checkout -b dev1        #创建并切换dev1分支
Switched to a new branch 'dev1'
[root@linuxidc git]# cat code.py 
[root@linuxidc git]# echo "print 'Sunshine Good~'" > code.py  #输出内容至code.py
[root@linuxidc git]# cat code.py 
print 'Sunshine Good~'
[root@linuxidc git]# git add code.py 
[root@linuxidc git]# git commit -m "New add rom 'Sunshine Good~'"
[dev1 865c84e] New add rom 'Sunshine Good~'
 1 files changed, 1 insertions(+), 0 deletions(-)
[root@linuxidc git]# git checkout master                    #切换至master
Switched to branch 'master'
[root@linuxidc git]# cat code.py                            #查看mastercode.py,没有内容
[root@linuxidc git]# git branch -d dev1                    #删除分支dev1报错,俩中方法,第一dev1确实不是我需要的,那么小d换成大D即可,第二个就下面步骤
error: The branch 'dev1' is not fully merged.
If you are sure you want to delete it, run 'git branch -D dev1'.
[root@linuxidc git]# git merge dev1                        #合并分支dev1至master
Updating bf266f5..865c84e
Fast-forward
 code.py |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
[root@linuxidc git]# cat code.py 
print 'Sunshine Good~'
[root@linuxidc git]# git branch -d dev1                    #使用小d删除分支dev1,删除成功
Deleted branch dev1 (was 865c84e).
[root@linuxidc git]#

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/13654.html