J-DuYa / DY-Book

迁移知识点
2 stars 1 forks source link

学习GIT相关指令 #1

Open J-DuYa opened 3 years ago

J-DuYa commented 3 years ago

Git相关指令

最近感觉学习的git相关知识太过Low,整理一下相关的知识点(如果有错误,请在下方评论区指出)

git官网

image

下面会列出部分git知识点,带你走一遍git的基础操作

git-init - Create an empty Git repository or reinitialize an existing one

使用git init指令, 会在本地创建一个空的git代码仓库或者重新初始化当前目录下现有的git项目库

📧 Tip: 当当前目录已经被init的时候,执行git init指令会报错,可以先在bash控制台下执行rm -rf .git,删除已有的git项目地址

bash下执行 git init --help 指令,得到相关的git init下的指令概览

git init [-q | --quiet] [--bare] [--template=<template_directory>]
                 [--separate-git-dir <git dir>]
                 [--shared[=<permissions>]] [directory]
下面是git init的相关指令列表 指令 含义
git init 初始化git项目, 在当前目录下面创建一个git本地仓库

git-clone - Clone a repository into a new directory

git clone指令,clone一个远程仓库到本地新目录中

bash下执行 git clone --help 指令,得到相关的git clone下的指令概览

git clone [--template=<template_directory>]
                 [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
                 [-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
                 [--dissociate] [--separate-git-dir <git dir>]
                 [--depth <depth>] [--[no-]single-branch] [--no-tags]
                 [--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules]
                 [--jobs <n>] [--] <repository> [<directory>]
下面是git clone的相关指令列表 指令 含义
git clone [url] 将远程仓库的代码copy到本地<.git自动带入>
git clone [url] [别名 or copy到本地目录的名称] 将远程仓库的代码copy到本地,并支持自定义本地同步远程项目的目录名称<.git自动带入>

git-add - Add file contents to the index

bash下执行 git add --help 指令,得到相关的git add下的指令概览

git add [--verbose | -v] [--dry-run | -n] [--force | -f] [--interactive | -i] [--patch | -p]
                 [--edit | -e] [--[no-]all | --[no-]ignore-removal | [--update | -u]]
                 [--intent-to-add | -N] [--refresh] [--ignore-errors] [--ignore-missing] [--renormalize]
                 [--chmod=(+|-)x] [--] [<pathspec>...]
下面是git add的相关指令列表 指令 含义
git add . or git add -A 将本地代码添加到缓存中
git add [文件A] [文件B] ... 指定某个文件添加到缓存中
git add [文件夹名字] 指定某目录下的所有文件都添加到缓存中

git-status - Show the working tree status

执行git status 指令,回去检查本地的工作区间<非缓存区>的diff树

bash下执行 git status --help 指令,得到相关的git status下的指令概览

git status [<options>...] [--] [<pathspec>...]
下面是git status的相关指令列表 指令 含义
git status 查看在你上次提交之后是否有对文件进行再次修改
J-DuYa commented 3 years ago

git-commit - Record changes to the repository

git commit 指令会记录本地改动跟版本库的差异

在bash下执行 git commit --help 指令,得到相关的git commit下的指令概览

git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]
                  [--dry-run] [(-c | -C | --fixup | --squash) <commit>]
                  [-F <file> | -m <msg>] [--reset-author] [--allow-empty]
                  [--allow-empty-message] [--no-verify] [-e] [--author=<author>]
                  [--date=<date>] [--cleanup=<mode>] [--[no-]status]
                  [-i | -o] [-S[<keyid>]] [--] [<file>...]
下面是git commit的相关指令列表 指令 含义
git commit -m [这个版本的修改描述] 将本地的修改记录到缓存中,并给这段修改的相关的文件,打上标签,说明这个版本修改的内容
git commit --amend -m [描述] 将本地所有commit 或 commit 的修改内容,统一打上一个共同的标签
git commit -m 'xxx' --no-verify 强制性commit文件
J-DuYa commented 3 years ago

git-pull - Fetch from and integrate with another repository or a local branch

git pull指令,从其他存储库或本地分支获取并集成

在bash下执行 git pull --help 指令,得到相关的git pull下的指令概览

git pull [<options>] [<repository> [<refspec>...]]

下面是git pull的相关指令列表

指令 含义
git pull 同步远程分支的代码,默认是同分支的代码
git pull origin [远程仓库分支名] 同步远程某具体仓库的代码到本地
J-DuYa commented 3 years ago

git-push - Update remote refs along with associated objects

git push指令,会更新本地的代码到远程仓库中

在bash下执行 git push --help 指令,得到相关的git push下的指令概览

git push [--all | --mirror | --tags] [--follow-tags] [--atomic] [-n | --dry-run] [--receive-pack=<git-receive-pack>]
                  [--repo=<repository>] [-f | --force] [-d | --delete] [--prune] [-v | --verbose]
                  [-u | --set-upstream] [-o <string> | --push-option=<string>]
                  [--[no-]signed|--signed=(true|false|if-asked)]
                  [--force-with-lease[=<refname>[:<expect>]]]
                  [--no-verify] [<repository> [<refspec>...]]

下面是git push的相关指令列表

指令 含义
git push 将本地代码的修改提交推送到当前分支同步的远程分支上
git push origin [远程分支名] 将本地代码的修改提交推送到远程某分支
J-DuYa commented 3 years ago

git-remote - Manage set of tracked repositories

git remote指令 管理跟踪存储库集

在bash下执行 git remote --help 指令,得到相关的git remote下的指令概览

git remote [-v | --verbose]
       git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=<fetch|push>] <name> <url>
       git remote rename <old> <new>
       git remote remove <name>
       git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
       git remote set-branches [--add] <name> <branch>...
       git remote get-url [--push] [--all] <name>
       git remote set-url [--push] <name> <newurl> [<oldurl>]
       git remote set-url --add [--push] <name> <newurl>
       git remote set-url --delete [--push] <name> <url>
       git remote [-v | --verbose] show [-n] <name>...
       git remote prune [-n | --dry-run] <name>...
       git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]
下面是git remote的相关指令列表 指令 含义
git remote add origin master [远程仓库的ssh或者url] 将本地git仓库指向指定某远程仓库
J-DuYa commented 3 years ago

git-stash - Stash the changes in a dirty working directory away

git stash指令将本地的更改,隐藏到脏工作目录中

在bash下执行 git stash --help 指令,得到相关的git stash下的指令概览

下面是git stash的相关指令列表

      git stash list [<options>]
       git stash show [<stash>]
       git stash drop [-q|--quiet] [<stash>]
       git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
       git stash branch <branchname> [<stash>]
       git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
                    [-u|--include-untracked] [-a|--all] [-m|--message <message>]
                    [--] [<pathspec>...]]
       git stash clear
       git stash create [<message>]
       git stash store [-m|--message <message>] [-q|--quiet] <commit>
下面是git stash的相关指令列表 指令 含义
git stash 会把所有未提交的修改(包括暂存的和非暂存的)都保存起来,用于后续恢复当前工作目录
git stash save [暂存内容的标记,用于提取] 会把所有未提交的修改(包括暂存的和非暂存的)都保存起来,用于后续恢复当前工作目录,给每个stash加一个message标记,用于记录版本
git stash pop [message标记] 恢复上一个缓存的工作目录的内容
git stash apply [message标记] 将缓存堆栈中的stash多次应用到工作目录中,但并不删除stash拷贝
git stash list 查看现有的所有stash缓存list
git stash drop [message标记] 移除某stash缓存区
git stash show [message标记] 查看某stash与现在代码的diff
git stash clear 清除stash缓存中所有的记录
J-DuYa commented 3 years ago

git-reset - Reset current HEAD to the specified state

git-reset 重置当前head的state

在bash下执行 git reset --help 指令,得到相关的git reset下的指令概览

       git reset [-q] [<tree-ish>] [--] <paths>...
       git reset (--patch | -p) [<tree-ish>] [--] [<paths>...]
       git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]
下面是git reset的相关指令列表 指令 含义
git reset --hard 重置当前本地代码库到上一次提交的版本库 [当前库] ---> [上一个版本库]
git reset --hard [commit ID] 重置当前版本到指定的版本 commit ID 可以根据 指令git log or git reflog 查询
J-DuYa commented 3 years ago

git-log - Show commit logs

在bash下执行 git log --help 指令,得到相关的git log下的指令概览

git log [<options>] [<revision range>] [[--] <path>...]
下面是git log的相关指令列表 指令 含义
git log 查看远程版本库的所有提交记录
J-DuYa commented 3 years ago

在bash下执行 git reflog --help,得到相关的git reflog下的指令概览

 git reflog <subcommand> <options>

下面是git reflog的相关指令列表

指令 含义
git reflog 显示整个本地仓储的commit, 包括所有branch的commit, 甚至包括已经撤销的commit, 只要HEAD发生了变化, 就会在reflog里面看得到. git log只包括当前分支的commit
J-DuYa commented 3 years ago

git-revert - Revert some existing commits

在bash下执行git revert --help,得到相关的git revert下的指令概览

git-revert - Revert some existing commits

git revert [--[no-]edit] [-n] [-m parent-number] [-s] [-S[<keyid>]] <commit>...
git revert --continue
git revert --quit
git revert --abort
下面是git revert的相关指令列表 指令 含义
git revert -n [commit ID] 重做某个commit提交的内容
hsk863hh commented 3 years ago

主管正常合并分支

(master)git fetch
git branch
git checkout feature-login
(feature-login) git pull origin feature-login (拉取最新代码)
git checkout master
(master) git merge feature-login
git push origin master

主管合并分支有冲突

(master) git merge 'feature-register'
报错CONFLICT ...
冲突文件解决冲突代码
git add .
git commit -m 'feature-register'
git merge 'feature-register'(会提示是最新代码不需要合并了)
git push origin master

不小心在master分支上写了很多代码 (master) 此时不能直接切换分支

git stash(修改的记录会放到一边,只剩下新增的文件,可切换分支)
git checkout -b feature-logout
(feature-logout) git stash pop(将之前的修改在拿出来)
git add .
git commit -m '注销'
git push origin feature-logout
J-DuYa commented 3 years ago

git-branch - List, create, or delete branches

git branch 返回一个列表或创建或删除分支

在bash下执行git branch --help,得到相关的git branch下的指令概览

     git branch [--color[=<when>] | --no-color] [-r | -a]
               [--list] [-v [--abbrev=<length> | --no-abbrev]]
               [--column[=<options>] | --no-column] [--sort=<key>]
               [(--merged | --no-merged) [<commit>]]
               [--contains [<commit]] [--no-contains [<commit>]]
               [--points-at <object>] [--format=<format>] [<pattern>...]
       git branch [--track | --no-track] [-f] <branchname> [<start-point>]
       git branch (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
       git branch --unset-upstream [<branchname>]
       git branch (-m | -M) [<oldbranch>] <newbranch>
       git branch (-c | -C) [<oldbranch>] <newbranch>
       git branch (-d | -D) [-r] <branchname>...
       git branch --edit-description [<branchname>]

下面是git branch的相关指令列表

指令 含义
git branch -a 查看本地和远程所有的分支
git branch -D [branch 名称] 删除本地某个分支
J-DuYa commented 3 years ago

git-checkout - Switch branches or restore working tree files

在bash下执行git checkout --help,得到相关的git checkout下的指令概览

       git checkout [-q] [-f] [-m] [<branch>]
       git checkout [-q] [-f] [-m] --detach [<branch>]
       git checkout [-q] [-f] [-m] [--detach] <commit>
       git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>]
       git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>...
       git checkout [<tree-ish>] [--] <pathspec>...
       git checkout (-p|--patch) [<tree-ish>] [--] [<paths>...]
下面是git checkout的相关指令列表 指令 含义
git checkout [branch name] 切换到指定的分支
git checkout -b [local name] [remote name] 将远程分支的代码拉取到本地,在本地建一个本地分支