利用Git命令进行版本控制之常见命令汇总

学习目标:

        利用Git命令实现简单的版本控制,设好存档点,保护好自己写的代码不至于丢失,避免重复写代码的尴尬,并跟踪自己的所有修改变化。 


主干内容:

1、设置git config  –global 参数

        有了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,一个电脑只需配置一次,当然你也可以对某个仓库指定的不同的用户名和邮箱,需要另外配置,就不多说了。

git config  –-global user.name "用户名"
git config  –-global user.email "用户邮箱"

2、初始化一个仓库 git init

         什么是仓库?仓库又名版本库,英文名repository,你可以简单的理解成一个文件夹,这个文件夹里面的所有文件都可以被Git管理起来,每个文件的修改,删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻还可以将文件”还原”。

选中你的工程文件夹 --> 右键 --> Git Bash Here ,进入Git 命令窗口,输入:

git init 
// pwd   //使用该命令可以查看初始化后的仓库绝对路径

这样,就创建好了仓库,并默认在(master)主分支上。

3、把文件添加到仓库中 git add 

git add test1.txt   // 可以单个添加
git add .   // 也可以一次添加所有文件

4、提交到本地仓库 git commit 

git commit -m "提交信息说明"

5、新建分支并切换到新建分支 git checkout -b 

可以将主分支保护起来,所有修改都在分支上进行,当调试ok后,再合并分支。

git checkout -b 新的分支名  // 新建分支并切换到新建分支

git checkout 分支名  // 切换到指定分支
git branch    // 查看分支 * 后面的分支是当前分支

6、合并分支 git merge branch 

git merge branch 主分支名 分支命  // 合并两个任意分支

7、删除本地分支 git branch -d

合并分支后并不会删除原分支,需要手动删除。

git branch -d 分支   // 删除本地分支

8、连接远程仓库 git remote add origin

远程仓库的作用在于共享代码,协同编程,如果只是版本控制也可不用上传。(主要是本人学艺不精,远程推送第一次还正常,第二次推送总是出现各种冲突)

git remote add origin [email protected]:用户名/远程仓库名.git   // 使用HHS 加密传输

9、推送主分支 git push -u origin master

git push -u origin master   // 推送主分支 第一次需要加 -u 后面就不用了

10、推送任意分支 git push  origin

git push  origin 分支名  // 推送任意分支

11、删除任意远程分支 git push  origin :

git push  origin :分支名  // 删除任意远程分支 (本地分支还在) 注意区别是多了个冒号

12、拉取远程分支到本地创建的新分支 git checkout -b   origin/  

git checkout -b 本地新建分支名  origin/远程分支名   // 拉取远程分支到本地创建的新分支

13、查看本地与远程所有分支 git branch -a

git branch -a  // 查看本地与远程所有分支 

辅助内容:

1、获取常用git命令及说明  git 或 git help

$ git
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           [--super-prefix=<path>] [--config-env=<name>=<envvar>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone     Clone a repository into a new directory
   init      Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add       Add file contents to the index
   mv        Move or rename a file, a directory, or a symlink
   restore   Restore working tree files
   rm        Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect    Use binary search to find the commit that introduced a bug
   diff      Show changes between commits, commit and working tree, etc
   grep      Print lines matching a pattern
   log       Show commit logs
   show      Show various types of objects
   status    Show the working tree status

grow, mark and tweak your common history
   branch    List, create, or delete branches
   commit    Record changes to the repository
   merge     Join two or more development histories together
   rebase    Reapply commits on top of another base tip
   reset     Reset current HEAD to the specified state
   switch    Switch branches
   tag       Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch     Download objects and refs from another repository
   pull      Fetch from and integrate with another repository or a local branch
   push      Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.

2、显示所有git命令  git help -a

git help -a  // 由于内容太多就不展示了

3、查看单个命令的说明 git help ***

git help branch  // 查看branch 的手册信息

4、查看文件的区别 git diff

git diff  // 查看当前仓库所有文件与上一次存档的区别,只能区别文本信息

git diff 文件1 文件2  // 查看两个文件的差异,必须在当前目录下

5、查看日志(所有提交信息)

git log --oneline  // 显示简单的提交日志

后记

        非常感谢大家能看到这里,其实关于git命令的教程在网上已经有很多了,我这里只是挑了些自己觉得有用的稍作整理方便自己查看,后面还有一些关于冲突解决等的知识都没有提到,如果想更系统的学习我可以给大家推荐一些好的内容。

git菜鸟教程_国服第l帅的博客-CSDN博客_git菜鸟教程安装git使用Windows版的msysgit,官方下载地址:http://msysgit.github.io/详细安装过程:http://jingyan.baidu.com/article/a3a3f811d4cd308da2eb8ad1.html安装完成后先进行如下配置:$ git config --global user.name "Your Nahttps://blog.csdn.net/wu020708/article/details/52120530

Git基本命令行用法讲解_哔哩哔哩_bilibili宁皓网Git教学视频分享Git基本命令行用法讲解https://www.bilibili.com/video/BV1WW411Q7EW?Git全套教程丨2021最新IDEA版_哔哩哔哩_bilibili制作不易,大家记得点个关注,一键三连呀【点赞、投币、收藏】感谢支持~需资料文档及课件看up个人主页简介获取本套教程内容丰富、详实,囊括:Git安装过程、本地库基本操作、远程基本操作、基于分支的Gitflow工作流、跨团队协作的 Forking工作流、Eclipse中的Git版本控制以及Git对Eclipse特定文件忽略的配置方法。还通过展示Git内部版本管理机制,让你了解 到Git高效操作的https://www.bilibili.com/video/BV1XP4y147v1?

猜你喜欢

转载自blog.csdn.net/weixin_45694843/article/details/124766183