GIT 基础
仓库常用命令
初始化一个仓库
$ git init
Initialized empty Git repository in E:/git/test/.git/
查看仓库的状态
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
新建一个文件,再次查看状态:
Untracked files:未被跟踪的文件列表,也就是Git在上一次快照(提交中没有发现的文件)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README
nothing added to commit but untracked files present (use "git add" to track)
跟踪新文件并再次查看状态
Changes to be committed(等待提交的更改):标题下列出的就是已暂存的文件
$ git add README
warning: LF will be replaced by CRLF in README.
The file will have its original line endings in your working directory
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README
修改已经暂存后的a.txt文件,然后查看status
Changes not staged for commit(已更改但未添加到暂存区):表示处于跟踪状状态下的文件已经被修改,
但是没有被添加到暂存区需要执行git add 命令,因此git add 即可以用来跟踪新文件也可以用来暂存文件
把git add 理解为"添加内容到下一次提交中"
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README
new file: a.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: a.txt
跳过暂存区提交,作用:git自动把已跟踪的文件跳过暂存区提交
// -a:忽略暂存区
// -m: 添加备注信息
git commit -am 'some word'
git push
# 全部格式
git push <远程仓库名> <本地分支名>:<远程分支名>
# 将本地develop分支推送到远程 develop 分支
$ git push 20221109test develop
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 16 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 551 bytes | 551.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/chaung_sun/20221109test.git
9f67aa7..850be7b develop -> develop
# 省略远程分支格式(使用前提是本地分支和远程分支名称一致)
git push <远程仓库名> <本地分支名>
将本地 a 分支 推送到远程 develop 分支
$ git push 20221109test a:develop
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 249 bytes | 249.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/chaung_sun/20221109test.git
850be7b..90b878e a -> develop
# 省略远程分支格式,并且远程仓库不存在<本地分支名>,git会帮忙创建同名的远程仓库
git push <远程仓库名> <本地分支名>
$ git push 20221109test a
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'a' on Gitee by visiting:
remote: https://gitee.com/chaung_sun/20221109test/pull/new/chaung_sun:a...chaung_sun:master
To https://gitee.com/chaung_sun/20221109test.git
* [new branch] a -> a
git remote add
git remote add <shortname> <url>:将远程仓库唯一的URL<url> 映射成为 在本地仓库中对远程仓库起的别名<shortname>
#参数<shortname>:在本地仓库中对远程仓库起的别名。而我们按照Git官方教程,一般会把参数<shortname>设置为origin。
#参数<url>: 远程仓库在互联网上唯一的URL
git remote rm
: 删除关联的远程仓库
# 删除
$ git remote rm search_test
win@DESKTOP-MFK5B06 MINGW64 /d/jlc_auto_test/fa_search_test (master)
# 改名为 origin
$ git remote add origin http://192.168.150.250/test/fa_mall_auto_test.git
分支管理
查看分支:
# 查看远程分支
git branch -r
# 查看所有分支
git branch -a
蓝色:当前分支
白色:本地分支
红色:远程分支
删除本地分支
git branch -d <本地分支名称>
删除远程分支
git push origin --delete <远程分支名称>
# 删除远程分支后,可以使用如下命令 更新本地的远程跟踪分支列表,以确保已删除的远程分支不再显示:
git fetch -p
切换分支:
git checkout 分支名称
查看当前分支
git checkout
将分支a的内容添加到分支b
git checkout b
git merge a
删除分支参考:https://blog.csdn.net/u010665216/article/details/129736023
文件状态:
- 已跟踪
- 未修改
- 已修改
- 已暂存
- 未跟踪
遇到问题:
手动在远程仓库删除分支serverfix后,本地使用 git branch -a 还是一致显示该分支
解决:
$ git branch -a
* master
remotes/origin/error335
remotes/origin/master
remotes/origin/serverfix
86158@DESKTOP-9PANV23 MINGW64 /e/PyProject/douban (master)
$ git fetch -p
From https://gitee.com/chaung_sun/douban
- [deleted] (none) -> origin/serverfix
86158@DESKTOP-9PANV23 MINGW64 /e/PyProject/douban (master)
$ git branch -a
* master
remotes/origin/error335
remotes/origin/master
GIT 工作流:
- 修改工作目录中的文件
- 暂存文件, 将这些文件的快照加入暂存区
- 提交暂存区中的文件,将快照永久地保存在Git目录中
从远程仓库拉去代码并合并到本地分支
# git fetch 不会跟新当前工作目录下的文件内容,需要进行merge操作
$ git fetch origin
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 1.04 KiB | 42.00 KiB/s, done.
From http://192.168.150.250/test/fa_mall_auto_test
e58e22c..3e1792b develop -> origin/develop
$ git merge origin/develop
Updating e58e22c..3e1792b
Fast-forward
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
# 以上操作等价于git pull
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/15916734.html