Git命令使用

目录

  • [查看git版本git --version](#查看git版本git --version)
  • [查看命令说明git help -a](#查看命令说明git help -a)
  • [克隆仓库git clone https://xxxx/xx/](#克隆仓库git clone https://xxxx/xx/)
  • [拉取更新git pull](#拉取更新git pull)
  • [推送更新git push](#推送更新git push)
  • [查看本地分支与远程分支关联git branch -vv](#查看本地分支与远程分支关联git branch -vv)
  • [设置推送关联分支git push --set-upstream origin master](#设置推送关联分支git push --set-upstream origin master)
  • [设置拉取关联分支git pull --set-upstream origin dev](#设置拉取关联分支git pull --set-upstream origin dev)
  • [查看当前分支状态git status](#查看当前分支状态git status)
  • [切换分支git checkout 分支名(提交点等)](#切换分支git checkout 分支名(提交点等))
  • [添加改动的文件到暂存区git add 文件名](#添加改动的文件到暂存区git add 文件名)
  • [提交代码git commit -m "注释"](#提交代码git commit -m "注释")
  • [取消暂存文件git restore --staged 文件名](#取消暂存文件git restore --staged 文件名)
  • [撤销未暂存文件的修改git restore 文件名](#撤销未暂存文件的修改git restore 文件名)
  • [移除未跟踪的新文件git clean -f 文件名](#移除未跟踪的新文件git clean -f 文件名)
  • [查看本地仓库关联的远程仓库地址git remote -v](#查看本地仓库关联的远程仓库地址git remote -v)
  • [删除本地仓库关联的远程仓库地址git remote rm origin](#删除本地仓库关联的远程仓库地址git remote rm origin)
  • [重新关联本地仓库到远程仓库地址git remote add origin 仓库地址](#重新关联本地仓库到远程仓库地址git remote add origin 仓库地址)
  • [存放当前工作区的改动切换到另外的分支或提交点git stash](#存放当前工作区的改动切换到另外的分支或提交点git stash)

查看git版本git --version

查看git版本

bash 复制代码
PS D:\SourceTree\repository\practiceServer> git --version
git version 2.35.1.windows.2

查看命令说明git help -a

查看命令说明,回车展示更多命令,q退出

使用 git help <command>,查看指定命令的使用,会打开本地html页面

bash 复制代码
PS D:\SourceTree\repository\practiceServer> git help -a
See 'git help <command>' to read about a specific subcommand

Main Porcelain Commands
   add                  Add file contents to the index
   am                   Apply a series of patches from a mailbox
   archive              Create an archive of files from a named tree
   bisect               Use binary search to find the commit that introduced a bug
   branch               List, create, or delete branches
   ...
   
git help checkout

克隆仓库git clone https://xxxx/xx/

从远端仓库克隆分支到本地

bash 复制代码
git clone https://xxxx/xx/

拉取更新git pull

拉取远程仓库的更新到本地,默认拉取的分支是绑定的远端分支(使用命令git branch -vv 查看关联情况)

可以使用 git pull origin master 指定远程仓库的分支

bash 复制代码
PS D:\SourceTree\repository\practiceServer> git pull
Already up to date.

PS D:\SourceTree\repository\practiceServer> git pull origin master

推送更新git push

推送本地的更新到远程仓库,默认拉取的分支是绑定的远端分支(使用命令git branch -vv 查看关联情况)

可以使用 git push origin master 指定远程仓库的分支

bash 复制代码
PS D:\SourceTree\repository\practiceServer> git push
Everything up-to-date
PS D:\SourceTree\repository\practiceServer> git push origin master
Everything up-to-date

查看本地分支与远程分支关联git branch -vv

查看本地分支与远程分支关联情况。

bash 复制代码
PS D:\SourceTree\repository\practiceServer> git branch -vv
* master 80126bc [origin/master]

设置推送关联分支git push --set-upstream origin master

将本地分支推送命令关联到远程分支

bash 复制代码
PS D:\SourceTree\repository\practiceServer> git push --set-upstream origin master
Everything up-to-date
branch 'master' set up to track 'origin/master'.

设置拉取关联分支git pull --set-upstream origin dev

将本地分支拉取命令关联到远程分支

bash 复制代码
PS D:\SourceTree\repository\practiceServer> git pull --set-upstream origin master
 * branch            master     -> FETCH_HEAD
Already up to date.

查看当前分支状态git status

查看当前分支状态

bash 复制代码
PS D:\SourceTree\repository\practiceServer> git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
...

切换分支git checkout 分支名(提交点等)

切换分支,提交点等

bash 复制代码
 git checkout master
  git checkout 提交点

添加改动的文件到暂存区git add 文件名

git add .

添加所有修改的文件;

git add 文件名

添加单个文件;

git add file1 file2 file3

添加多个文件。

bash 复制代码
PS D:\SourceTree\repository\practiceServer> git add .
PS D:\SourceTree\repository\practiceServer> git add filename                                                                
fatal: pathspec 'filename' did not match any files
(这是找不到文件,乱写的文件名)

提交代码git commit -m "注释"

git commit提交代码,git commit -m "注释",加注释提交代码

说明:提交代码只是提交到本地仓库,要更新到远程仓库需要用git push。

bash 复制代码
PS D:\SourceTree\repository\practiceServer> git commit -m "修改"
On branch master
nothing to commit, working tree clean

取消暂存文件git restore --staged 文件名

暂存区内的文件,想要保留修改仅取消暂存():

可以使用git restore --staged 文件名;

多个文件 git restore --staged 文件名1 文件名2

所有文件git restore --staged .

说明:该命令是新的文件也适用;没有--staged就是另外一个命令。

bash 复制代码
PS D:\SourceTree\repository\practiceServer> git restore --staged filename

撤销未暂存文件的修改git restore 文件名

对未进入暂存区的文件撤销修改,不影响已经进去暂存区的修改

bash 复制代码
// 撤销所有未进入暂存区的修改
PS D:\SourceTree\repository\practiceServer> git restore .
// 撤销指定文件未进入暂存区的修改
PS D:\SourceTree\repository\practiceServer> git restore filename
error: pathspec 'filename' did not match any file(s) known to git // filename就是指定的文件名,报错是因为没有文件

移除未跟踪的新文件git clean -f 文件名

未跟踪的新文件:Untracked files

git clean -f . 是移除全部

git clean -f 文件名是移除单个文件

参数:

-i 显示要做什么,并以交互方式清理文件

可以使用git help clean查看命令的使用说明

bash 复制代码
PS D:\SourceTree\repository\practiceServer> git clean -f .                                                                          
Removing xxx/test/Test.java
PS D:\SourceTree\repository\practiceServer> git clean -f xxx/test/Test.java        
Removing xxx/test/Test.java

查看本地仓库关联的远程仓库地址git remote -v

查看git远程仓库地址

bash 复制代码
PS D:\SourceTree\repository\practiceServer> git remote -v
origin  https://xxx/java/practiceServer.git (fetch)
origin  https://xxx/java/practiceServer.git (push)

删除本地仓库关联的远程仓库地址git remote rm origin

rm就是remove移除的缩写,remote是远程的,origin是源端

bash 复制代码
PS D:\SourceTree\repository\practiceServer> git remote rm origin

重新关联本地仓库到远程仓库地址git remote add origin 仓库地址

由于ip改变什么的需要重新关联本地仓库到远程仓库地址,使用git remote add origin

bash 复制代码
PS D:\SourceTree\repository\practiceServer> git remote add origin https://xxx/java/practiceServer.git

存放当前工作区的改动切换到另外的分支或提交点git stash

当我们想要切换到别的分支或者历史提交点,但又不想破坏当前工作区的改动,可以使用git stash命令将当前的改动存放起来,stash是隐藏、存放。类似于存放在栈中。

git stash list查看存放的工作区;git stash pop弹出存放的工作区。

原文解释:

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

bash 复制代码
PS D:\SourceTree\repository\practiceServer> git stash
Saved working directory and index state WIP on master: 871e604 修改
相关推荐
贩卖纯净水.7 小时前
白月光git
git·github
爱吃瓜的猹z11 小时前
git reset 几点疑问
git·源代码管理
悟空201618 小时前
001、Git开发流程规范
git
Li小李同学Li18 小时前
git学习【持续更新中。。。】
git·学习·elasticsearch
AI逍遥子18 小时前
如何从github上clone项目
github
iBaoxing18 小时前
如何在 Fork 的 GitHub 项目中保留自己的修改并同步上游更新?github_fork_update
github
晨春计19 小时前
【git】
android·linux·git
念幽20 小时前
Git常用命令
git
神技圈子20 小时前
【git系列】git中的那些迷惑的术语以及概念详解
git
benben04421 小时前
Photoshop使用方法大全
git