0


GIT分布式版本控制系统 | 命令讲解入门

Git概述

Git是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。 也是

Linus Torvalds

为了帮助管理Linux内核开发而开发的一个开放源码的版本控制软件;分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者通过克隆(git clone),在本地机器上拷贝一个完整的Git仓库。

Git常用命令

命令名称作用git config --global user.name 用户名设置用户签名git congfig --global user.email 邮箱设置用户邮箱git init初始化本地库git status查看本地库状态git add 文件名添加到缓存区git commit -m “日志信息” 文件名提交到本地库git reflog查看历史记录git reset --hard 版本号****版本穿梭

工作区(写代码) == git add ==》 暂存区(临时存储) == git commit == 》 本地库(历史版本)

设置用户签名/邮箱

ws199@DESKTOP-2N1I9JA MINGW64 ~/Desktop
$ git config --global user.name weishuo

ws199@DESKTOP-2N1I9JA MINGW64 ~/Desktop
$ git config --global user.email [email protected]

初始化本地库——git init

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo
$ git init
Initialized empty Git repository in D:/GIT/Git-Space/git.demo/.git/

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$

查看本地库状态——git status

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git status
On branch master    #当前本地库分支位置

No commits yet        #目前没有文件要提交

nothing to commit (create/copy files and use "git add" to track)#未提交,且无文件提交

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        hello.txt

nothing added to commit but untracked files present (use "git add" to track)

添加暂存区——git add

git add 文件名

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ gitadd hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   hello.txt

文件删除暂存区——rm --cached

rm --cached 文件名

#删除暂存区,仍然存在工作区
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ gitrm--cached hello.txt
rm'hello.txt'

提交本地库——git commit -m

将暂存区文件提交到本地库
git commit -m “日志信息” 文件名

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git commit -m"first commit" hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory
[master (root-commit) 26da280] first commit
 1file changed, 7 insertions(+)
 create mode 100644 hello.txt

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git status
On branch master
nothing to commit, working tree clean

查看版本信息——git reflog

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git reflog
26da280 (HEAD -> master) HEAD@{0}: commit (initial): first commit
#版本号                                                    版本

查看版本详细信息——git log

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git log
commit 26da28016adaee50fc76b4be71fbf2b4b3fda929 (HEAD -> master)
Author: weishuo <[email protected]>
Date:   Tue Apr 1221:53:00 2022 +0800

    first commit

版本穿梭——git reset --hard 版本号

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git reset --hard ffce8cd
HEAD is now at ffce8cd second commit

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git reflog
ffce8cd (HEAD -> master) HEAD@{0}: reset: moving to ffce8cd
534cbd3 HEAD@{1}: commit: third commit
ffce8cd (HEAD -> master) HEAD@{2}: commit: second commit
26da280 HEAD@{3}: commit (initial): first commit

分支的操作

命令名称作用git branch 分支名创建分支git branch -v查看当前分支git checkout 分支名切换分支git merge 分支名****把指定的分支合并到当前分支上

查看当前分支——git branch -v

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git branch -v
* master 534cbd3 third commit

创建分支——git branch 分支名

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git branch hot-fix

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git branch -v
  hot-fix 534cbd3 third commit
* master  534cbd3 third commit

切换分支——git checkout 分支名

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (hot-fix)
$

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (hot-fix)
$ git branch -v
* hot-fix 534cbd3 third commit
  master  534cbd3 third commit

合并分支——git merge 分支名

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git merge hot-fix
Updating 534cbd3..017517e
Fast-forward
 hello.txt |13 +++----------
 1file changed, 3 insertions(+), 10 deletions(-)

合并分支(冲突合并)

检测有文件有两处修改

显示冲突
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git merge hot-fix
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master|MERGING)
$

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master|MERGING)
$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")(use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")
冲突合并
# 1.查看冲突代码
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master|MERGING)
$ vim hello.txt
# <<<< HEAD  当前分支代码# <<<< HEAD 当前分支代码    =======# ======= 合并分支代码   >>>>>>> hot-fix#删除特殊符号 以及 重复代码#2.添加到暂存区
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master|MERGING)
$ gitadd hello.txt

#3.提交本地库(不能带文件名)
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master|MERGING)
$ git commit -m"marge test"
fatal: cannot do a partial commit during a merge.

#4.切换分支查看
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git checkout hot-fix
Switched to branch 'hot-fix'#冲突合并,合并当前分支,不合并被合并分支# head ---> master(分支内容)--->版本号# HEAD决定当前分支

Git团队协作

在这里插入图片描述

在这里插入图片描述
命令名称作用git remote -v查看当前所有进程地址别名git remote add 别名 远程地址起别名git push 别名 分支推送本地分支上的内容到远程仓库git push 远程地址 分支推送本地分支上的内容到远程仓库git clone 远程地址将远程仓库的内容克隆到本地git pull 远程库地址别名 远程分支名将远程仓库对于分支最新内容拉下来后与当前本地分支直接合并git push -f 远程库地址别名 远程分支名****强制推送

创建远程库别名

git remote -v #查看当前所有进程地址别名
git remote add 别名 远程地址 #起别名

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (hot-fix)
$ git remote -v

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (hot-fix)
$ git remote add git.demo https://github.com/weishuoHH/git-demo.git

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (hot-fix)
$ git remote -v
git.demo        https://github.com/weishuoHH/git-demo.git (fetch)
git.demo        https://github.com/weishuoHH/git-demo.git (push)

推送本地分支上的内容到远程仓库

git push 别名 分支 #推送本地分支上的内容到远程仓库

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git push git.demo master
Enumerating objects: 21, done.
Counting objects: 100% (21/21), done.
Delta compression using up to 16 threads
Compressing objects: 100% (14/14), done.
Writing objects: 100% (21/21), 1.55 KiB |792.00 KiB/s, done.
Total 21(delta 4), reused 0(delta 0), pack-reused 0
remote: Resolving deltas: 100% (4/4), done.
To https://github.com/weishuoHH/git-demo.git
 * [new branch]      master -> master

拉取远程库到本地库

git pull 远程库地址别名 远程分支名

$ git pull [email protected]:wei-shuo-red/travel.git master --allow-unrelated-histories
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git pull git.demo master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3(delta 1), reused 0(delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 649 bytes |64.00 KiB/s, done.
From https://github.com/weishuoHH/git-demo
 * branch            master     -> FETCH_HEAD
   b129c29..d40c0cd  master     -> git.demo/master
Updating b129c29..d40c0cd
Fast-forward
 hello.txt |1 +
 1file changed, 1 insertion(+)

克隆远程库到本地库

git clone 远程地址 将远程仓库的内容克隆到本地

clone操作:
1.拉取代码
2.初始化本地库
3.创建别名

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-lhc
$ git clone https://github.com/weishuoHH/git-demo.git
Cloning into 'git-demo'...
remote: Enumerating objects: 27, done.
remote: Counting objects: 100% (27/27), done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 27(delta 6), reused 20(delta 4), pack-reused 0
Receiving objects: 100% (27/27), done.
Resolving deltas: 100% (6/6), done.

ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-lhc
$ ll
total 0
drwxr-xr-x 1 ws199 1976090 Apr 1714:27 git-demo/

#创建别名
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-lhc/git-demo (master)
$ git remote  -v
origin  https://github.com/weishuoHH/git-demo.git (fetch)
origin  https://github.com/weishuoHH/git-demo.git (push)

GitHub免密登录

生成ssh免密登录命令 -t "以某种加密算法登录 -C "描述

ws199@DESKTOP-2N1I9JA MINGW64 ~
$ ssh-keygen.exe -t rsa -C [email protected]
Generating public/private rsa key pair.
Enter fileinwhich to save the key (/c/Users/ws199/.ssh/id_rsa):
Created directory '/c/Users/ws199/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/ws199/.ssh/id_rsa
Your public key has been saved in /c/Users/ws199/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:CVIbj1aIxRUz8t0m7WXpzBqwJ6RzNHze+B5dd5Jyiwk [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|     +=o*.       |
|    ..oO = o   . |
|    . = o O = +  |
|     o . = X O . |
|        S E B O +|
|         o + O ++|
|            + + .|
|             . . |
|              .  |
+----[SHA256]-----+

ws199@DESKTOP-2N1I9JA MINGW64 ~
$

#通过ssh拉去远程库代码
ws199@DESKTOP-2N1I9JA MINGW64 /d/GIT/Git-Space/git.demo (master)
$ git pull [email protected]:weishuoHH/git-demo.git master
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? y
Please type'yes', 'no' or the fingerprint: yes
Warning: Permanently added 'github.com'(ED25519) to the list of known hosts.
From github.com:weishuoHH/git-demo
 * branch            master     -> FETCH_HEAD
Already up to date.
标签: git 分布式 github

本文转载自: https://blog.csdn.net/weixin_62765017/article/details/128166435
版权归原作者 wei_shuo 所有, 如有侵权,请联系我们删除。

“GIT分布式版本控制系统 | 命令讲解入门”的评论:

还没有评论