Git命令使用

查看git版本git --version

查看git版本

PS D:\SourceTree\repository\practiceServer> git --version
git version 2.35.1.windows.2

查看命令说明git help -a

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

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

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/

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

git clone https://xxxx/xx/

拉取更新git pull

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

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 指定远程仓库的分支

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

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

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

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

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

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

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

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

查看当前分支状态git status

查看当前分支状态

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 分支名(提交点等)

切换分支,提交点等

 git checkout master
  git checkout 提交点

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

git add .
添加所有修改的文件;

git add 文件名
添加单个文件;

git add file1 file2 file3
添加多个文件。

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。

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就是另外一个命令。

PS D:\SourceTree\repository\practiceServer> git restore --staged filename

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

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

// 撤销所有未进入暂存区的修改
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查看命令的使用说明

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远程仓库地址

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是源端

PS D:\SourceTree\repository\practiceServer> git remote rm origin

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

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

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.

PS D:\SourceTree\repository\practiceServer> git stash
Saved working directory and index state WIP on master: 871e604 修改

相关推荐

  1. git 命令使用

    2024-03-31 05:12:05       50 阅读
  2. git命令-项目使用

    2024-03-31 05:12:05       43 阅读
  3. Git命令使用

    2024-03-31 05:12:05       38 阅读
  4. git命令使用

    2024-03-31 05:12:05       34 阅读
  5. git cherry-pick命令使用

    2024-03-31 05:12:05       33 阅读
  6. git子模块使用关键命令

    2024-03-31 05:12:05       52 阅读
  7. 03 详细的Git命令使用大全

    2024-03-31 05:12:05       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-31 05:12:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-31 05:12:05       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-31 05:12:05       82 阅读
  4. Python语言-面向对象

    2024-03-31 05:12:05       91 阅读

热门阅读

  1. 服务器不支持PUT和DELETE请求处理

    2024-03-31 05:12:05       39 阅读
  2. 知识碎片-docker初始化db,自动导入SQL

    2024-03-31 05:12:05       41 阅读
  3. Python之旅:你能学到什么?

    2024-03-31 05:12:05       32 阅读
  4. RPM与YUM

    RPM与YUM

    2024-03-31 05:12:05      39 阅读
  5. go通道使用案例

    2024-03-31 05:12:05       39 阅读