# git常用命令 **Repository Path**: jianghuisheng/git-common-commands ## Basic Information - **Project Name**: git常用命令 - **Description**: git常用命令用法介绍 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 2 - **Created**: 2021-06-28 - **Last Updated**: 2022-04-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 上传文件 1. Git init (在本地工程目录下),生成.git 文件夹 git init 2. 上传修改的文件 git add . 3. 添加上传文件的描述 git commit -m "xxx" 4. 创建分支 git branch test 5. 切换分支 git checkout test 6. 创建并切换到新建分支 git checkout -b 7. 与远程分支相关联 git remote add origin "https://xxx" 8. 将分支上传 git push origin test ## git 删除分支 1. 删除远程分支 git push origin --delete test 2. 删除本地分支 git branch -D test ## git status 查看文件状态 - Changes not staged for commit 表示得大概就是工作区有该内容,但是缓存区没有,需要我们 git add - Changes to be committed 一般而言,这个时候,文件放在缓存区了,我们需要 git commit - nothing to commit, working tree clean 这个时候,我们将本地的代码推送到远端即可 ## 当前分支与指定分支合并 git merge xxx ## 查看哪些分支已经合并到当前分支 git branch --merged ## 查看哪些分支没有合并到当前分支 git branch --no-merged ## 查看各个分支最后一个提交对象的信息 git branch -v ## 重命名分支 git branch -m oldbranch-name newbranch-name ## 拉取远程分支并创建本地分支 - git checkout -b 本地分支名 x origin/远程分支名 x - git fetch origin branch-name:local-branch-name (fetch 方式) ## fetch 指令 - fetch 推荐写法 git fetch origin branch-name:local-branch-name 1. 一般而言,这个 origin 是远程主机名,一般默认就是 origin。 2. branch-name 你要拉取的分支 3. local-branch-name 通常而言,就是你本地新建一个新分支,将 origin 下的某个分支代码下载到本地分支。 ``` git fetch origin lixiaoxuan:yangwenlong // 你的工作目录下,就会有yangwenlong // 一般情况下,我们需要做的就是在这个分支上开发新需求 // 完成代码后,我们需要做的就是上传我们的分支 ``` ### fetch 其他写法 - 将某个远程主机的更新,全部取回本地。 git fetch 远程分支 - 这样子的话,取回的是所有的分支更新,如果想取回特定分支,可以指定分支名
git fetch 远程分支 分支名 ## 暂存区文件撤销 (不覆盖工作区),版本回退 - git reset HEAD^ # 回退所有内容到上一个版本 - git reset HEAD^ hello.php # 回退 hello.php 文件的版本到上一个版本 - git reset 052e # 回退到指定版本 ## 状态查询 - 查看状态 - git status - 查看历史操作记录 - git reflog - 查看日志 - git log ## 文档查询 - git help ## 差异比较 - 比较工作区与缓存区 - git diff ## 忽略文件 .gitignore ``` # 此行为注释 会被Git忽略 # 忽略 node_modules/目录下的所有文件 node_modules # 忽略所有.vscode结尾的文件 .vscode # 忽略所有.md结尾的文件 *.md # 但README.md除外 !README.md # 会忽略 doc/something.txt 但不会忽略doc/images/arch.txt doc/*.txt # 忽略 doc/ 目录下所有扩展名为txt文件 doc/**/*.txt ```