当前位置:   article > 正文

git 使用

git 使用

1. 下载git

yum -y install git

2. 配置用户,邮箱

  1. git config --global user.name 'tom'
  2. git config --global user.email '328294199@qq.com'
  3. git config --global color.ui true
  4. git config --global --list

3. 初始化代码目录

  1. mkdir -p /app/src/tom-app
  2. cd /app/src/tom-app
  3. git init
  4. ll .git/

4. git工作空间

  1. 工作区:工作目录就是我们执行命令git init时所在的地方
  2. 暂存区:暂存区和本地仓库都是在.git目录,因为它们只是用来存数据的
  3. 本地仓库:暂存区和本地仓库都是在.git目录,因为它们只是用来存数据的
  4. 远程仓库:远程仓库在中心服务器,也就是我们做好工作之后推送到远程仓库

5. git常用命令

命令

含义

git init

初始化本地仓库目录

git config --global

邮箱,用户名,颜色

git add

提交数据到缓冲区(暂存区) git add . (所有文件) 或 git add 文件

git commit

把暂存区的数据提交到本地仓库 git commit -m "标记/说明"

git status

显示工作空间的状态

git reset

回滚

git reset --soft cid(版本号)

把指定的版本数据内容下载到暂存区

git reset HEAD

暂存区 -->工作空间(被修改的状态)

git checkout

文件下载到工作空间并可以使用 git checkout . 或 git checkout 文件

git reset --mix 版本号

git reset --hard 版本号

把本地仓库指定版本信息数据下载到工作目录中

git branch

查看分支

git branch name

创建分支

git branch -d name

删除分支

git checkout 分支名字

切换分支

git merge 分支名字

合并(吸收)分支(把指定的分支合并到当前分支中)

git checkout -b name

创建分支并切换到这个分支

git pull origin 分支名字

用于从远程仓库获取最新的提交,并将其合并到当前分支中,

它相当于执行了 git fetch 后紧接着执行了 git merge

git fetch origin 分支名字

用于从远程仓库获取最新的提交,但不会自动合并或更新本地分支

git push -u origin 分支名字

将本地仓库代码推送至远程分支

git clone [https://|git]

用于初始化本地仓库,只需要执行一次。它会在本地创建一个新的目录,并将远程仓库的整个代码库复制到该目录中

git tag -a v1.0 -m "new v1.0"

给当前版本打个标签,用于发版本

git push -u origin --tags

将标签推送至远程仓库

  1. #书写代码
  2. ]# echo 进度1% > index.html
  3. #查看状态
  4. ]# git status
  5. # On branch master
  6. # Initial commit
  7. # Untracked files:
  8. # (use "git add <file>..." to include in what will be committed)
  9. # index.html
  10. nothing added to commit but untracked files present (use "git add" to track)
  11. #把工作区中未被跟踪的文件加到暂存区进行跟踪
  12. git add .
  13. #再次查看状态,文件已加入到暂存区
  14. [root@gitlab tom-app]# git status
  15. # On branch master
  16. # Initial commit
  17. # Changes to be committed:
  18. # (use "git rm --cached <file>..." to unstage)
  19. # new file: index.html
  20. #把暂存区的文件提交到本地仓库
  21. [root@gitlab tom-app]# git commit -m "项目v1"
  22. [master (root-commit) 92bbd3f] 项目v1
  23. 1 file changed, 1 insertion(+)
  24. create mode 100644 index.html
  25. #再次查看状态,工作区和暂存区都没有文件要提交了
  26. [root@gitlab tom-app]# git status
  27. # On branch master
  28. nothing to commit, working directory clean
  29. #不小心把代码删除了,可以通过git checkout .把最新代码从本地仓库拉取下来
  30. [root@gitlab tom-app]# rm -rf index.html
  31. [root@gitlab tom-app]# ll
  32. total 0
  33. [root@gitlab tom-app]# git status
  34. # On branch master
  35. # Changes not staged for commit:
  36. # (use "git add/rm <file>..." to update what will be committed)
  37. # (use "git checkout -- <file>..." to discard changes in working directory)
  38. #
  39. # deleted: index.html
  40. #
  41. no changes added to commit (use "git add" and/or "git commit -a")
  42. [root@gitlab tom-app]# git checkout .
  43. [root@gitlab tom-app]# ll
  44. total 4
  45. -rw-r--r-- 1 root root 9 Jul 10 11:38 index.html
  46. [root@gitlab tom-app]# git status
  47. # On branch master
  48. nothing to commit, working directory clean

6. 分支

  1. #当前共一个分支,目前位于master分支
  2. [root@gitlab tom-app]# git branch
  3. * master
  4. #从当前分支位置,创建要给新分支shopping
  5. [root@gitlab tom-app]# git branch shopping
  6. #当前仓库共2个分支,代码目前位于master分支
  7. [root@gitlab tom-app]# git branch
  8. * master
  9. shopping
  10. #切换代码到shopping分支
  11. [root@gitlab tom-app]# git checkout shopping
  12. Switched to branch 'shopping'
  13. #当前仓库共2个分支,代码目前位于shopping分支
  14. [root@gitlab tom-app]# git branch
  15. master
  16. * shopping
  17. #shopping分支新增代码并提交---------------------------------------------------
  18. [root@gitlab tom-app]# vim shopping.html
  19. [root@gitlab tom-app]# git status
  20. # On branch shopping
  21. # Untracked files:
  22. # (use "git add <file>..." to include in what will be committed)
  23. #
  24. # shopping.html
  25. nothing added to commit but untracked files present (use "git add" to track)
  26. [root@gitlab tom-app]# git add .
  27. [root@gitlab tom-app]# git commit -m "shopping v1"
  28. [shopping 94cead0] shopping v1
  29. 1 file changed, 1 insertion(+)
  30. create mode 100644 shopping.html
  31. #----------------------------------------------------------------------
  32. #把shoping分支的代码合并到master分支--------------------------------------
  33. #先确保当前分支在master分支
  34. [root@gitlab tom-app]# git checkout master
  35. Switched to branch 'master'
  36. [root@gitlab tom-app]# ll
  37. -rw-r--r-- 1 root root 9 Jul 10 12:08 index.html
  38. [root@gitlab tom-app]# git merge shopping
  39. Updating 65d8fab..94cead0
  40. Fast-forward
  41. shopping.html | 1 +
  42. 1 file changed, 1 insertion(+)
  43. create mode 100644 shopping.html
  44. [root@gitlab tom-app]# ll
  45. -rw-r--r-- 1 root root 9 Jul 10 12:08 index.html
  46. -rw-r--r-- 1 root root 9 Jul 10 12:26 shopping.html
  47. #----------------------------------------------------------------------

7. 远程仓库(gitee)

7.1. 方式一:https方式(密码认证方式)

  1. #仓库为https(密码认证方式),每次push的时候,都要输入用户名密码
  2. ]
  3. ]# git push -u origin "master"
  4. Username for 'https://gitee.com': 13202283227
  5. Password for 'https://13202283227@gitee.com':
  6. Counting objects: 12, done.
  7. Compressing objects: 100% (7/7), done.
  8. Writing objects: 100% (12/12), 1.04 KiB | 0 bytes/s, done.
  9. Total 12 (delta 1), reused 0 (delta 0)
  10. remote: Powered by GITEE.COM [GNK-6.4]
  11. To https://gitee.com/a-little-phoenix/tomapp.git
  12. * [new branch] master -> master
  13. Branch master set up to track remote branch master from origin.

7.2. 方式二:为了提交代码方便,重新设置远程仓库地址为ssh的方式(密钥认证方式)

  1. git remote set-url origin git@gitee.com:a-little-phoenix/tomapp.git
  2. #本地生成密钥对
  3. [root@gitlab tom-app]# ssh-keygen
  4. Generating public/private rsa key pair.
  5. Enter file in which to save the key (/root/.ssh/id_rsa):
  6. Enter passphrase (empty for no passphrase):
  7. Enter same passphrase again:
  8. Your identification has been saved in /root/.ssh/id_rsa.
  9. Your public key has been saved in /root/.ssh/id_rsa.pub.
  10. The key fingerprint is:
  11. SHA256:9iVmjImoq30tv/FAixhelh9mBp/zAVWM8KvW4UF8Cy0 root@gitlab
  12. The key's randomart image is:
  13. +---[RSA 2048]----+
  14. | ...+. |
  15. | +... |
  16. | . . E o |
  17. | = = O . |
  18. | . = @ S * . |
  19. | . * B X B o |
  20. | + ..B + . |
  21. | . .o..+ |
  22. |..o. oo.. |
  23. +----[SHA256]-----+
  24. [root@gitlab tom-app]# ll /root/.ssh/
  25. total 12
  26. -rw------- 1 root root 1679 Jul 10 13:45 id_rsa
  27. -rw-r--r-- 1 root root 393 Jul 10 13:45 id_rsa.pub

gitee上面添加刚刚生成的公钥

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/822119
推荐阅读
相关标签
  

闽ICP备14008679号