Git命令详解-本地项目与远程服务器项目间的交互

该文详细介绍了如何使用Git进行代码管理,包括从头开始创建新仓库,添加和提交文件,推送代码到远程服务器。还讨论了如何在有新版代码时替换本地代码,以及Git的常用命令如`gitlog`,`gitreset`用于版本回退。此外,文章还涵盖了Git的分支操作,如创建、切换、合并及删除分支。
摘要由CSDN通过智能技术生成

1.如果没有最新代码,希望从头开始做

[root@localhost opt]# git clone git@192.168.137.40:/git-root/shell.git
[root@localhost opt]# cd shell/
[root@localhost shell]# echo '222' > 2.sh
[root@localhost shell]# git add 2.sh 
[root@localhost shell]# git commit -m 'note:add a shell'
[master 5adbf9a] note:add a shell
 1 file changed, 1 insertion(+)
 create mode 100644 2.sh
[root@localhost shell]# git push -u origin master
Authorized users only. All activities may be monitored and reported.
git@192.168.137.40's password: 
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 265 bytes | 265.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To 192.168.137.40:/git-root/shell.git
   002af51..5adbf9a  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

2.如果有一个新版代码,希望直接把本地代码替换到远程服务器

[root@localhost ~]# cd /opt/shell/
[root@localhost shell]# echo 333 > 3.sh
[root@localhost shell]# git init 
Reinitialized existing Git repository in /opt/shell/.git/
[root@localhost shell]# git add .
[root@localhost shell]# git commit -m 'note'
[master a5a158b] note
 1 file changed, 1 insertion(+)
 create mode 100644 3.sh
[root@localhost shell]# git push -u origin master

Authorized users only. All activities may be monitored and reported.
git@192.168.137.40's password: 
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 282 bytes | 282.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To 192.168.137.40:/git-root/shell.git
   5adbf9a..a5a158b  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

git clone过来的时候,git不会对比本地和服务器的文件,也就不会有冲突,建议确定完全覆盖本地的时候用clone,不确认会不会冲突的时候用git pull,将远程服务器的代码download下来
 

3.git常用命令

1.git init                   #初始化
2.git add main.sh            #将某文件添加到暂存区
3.git add.                   #将文件夹下所有的文件添加到暂存区
4.git commit -m 'note'       #将暂存区中的文件保存成某个版本
5.git log                    #查看所有的版本日志
6.git status                 #查看现在暂存区的状态
7.git diff                   #查看现在文件与上一个提交的commit版本的区别
8.git reset --hard HEAD^     #回到上一个版本
9.git reset --haed xxxx      #回到xxxx版本
10.git pull origin master    #从主分支pull到本地
11.git push -u origin master #从本地push到主分支
12.git pull/push             #执行默认是主分支

4.版本穿梭、回退

#用git log查看

#每一个提交的版本都唯一对应一个commit版本号

#使用git reset命令退到上一个版本

[root@localhost shell]# git reflog(查看命令历史,以便确认要回到哪个版本,注意commit版本号可以不用全部填写,输入前7位即可)
5adbf9a (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
a5a158b (origin/master, origin/HEAD) HEAD@{1}: clone: from git@192.168.137.40:/git-root/shell.git

[root@localhost shell]# git reset --hard 5adbf9a
HEAD is now at 5adbf9a note:add a shell

5.分支管理

1.创建分支
[root@localhost shell]# git checkout -b dev #创建dev分支并切换到dev
Switched to a new branch 'dev'
####git checkout命令加上-b参数表示创建并切换,相当于git branch dev + git checkout dev
[root@localhost shell]# git branch          #命令会列出所有分支,当前分支前面会有*号
* dev
  master

2.分支切换
[root@localhost shell]# git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
[root@localhost shell]# git branch
  dev
* master

3.合并分支
首先切换到dev分支,并随意创建一个文件
[root@localhost shell]# git checkout dev
Switched to branch 'dev'
[root@localhost shell]# ll
total 8.0K
-rw-r--r-- 1 root root 33 Apr 20 18:16 1.sh
-rw-r--r-- 1 root root  4 Apr 20 18:16 2.sh
[root@localhost shell]# echo '444' >4.sh
[root@localhost shell]# ll
total 12K
-rw-r--r-- 1 root root 33 Apr 20 18:16 1.sh
-rw-r--r-- 1 root root  4 Apr 20 18:16 2.sh
-rw-r--r-- 1 root root  4 Apr 20 22:29 4.sh
合并
[root@localhost shell]# git merge dev  #把dev分支的工作成果合并到master分支上
Already up to date.
#注意:git merge如果不指定分支,则默认将当前分支合并到master
切换回master,如下可看到dev分支提交的4.sh已合并到了master上
[root@localhost shell]# git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
[root@localhost shell]# ll
total 16K
-rw-r--r-- 1 root root 33 Apr 20 18:16 1.sh
-rw-r--r-- 1 root root  4 Apr 20 18:16 2.sh
-rw-r--r-- 1 root root  4 Apr 20 22:31 3.sh
-rw-r--r-- 1 root root  4 Apr 20 22:29 4.sh

4.删除分支
[root@localhost shell]# git branch -d dev
Deleted branch dev (was 5adbf9a).
[root@localhost shell]# git branch
* master

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值