提交:add -> commit -> push
下载:pull || clone
1.进入Git 安装目录 运行 git-bash.exe
2.输入命令 初始化: git init demo(文件夹名字)
3.进入文件夹: cd demo
4演示 创建一个文件 : echo "Holle Git !" >> TestDemo.txt
5查看: ll
-rw-r--r-- 1 Administrator 197121 12 Nov 26 11:29 TestDemo.txt
6查看状态: git status
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
TestDemo.txt
nothing added to commit but untracked files present (use "git add" to track)
7.把文件添加到栈存区: git add (文件) git add TestDemo.txt
$ git add TestDemo.txt
warning: in the working copy of 'TestDemo.txt', LF will be replaced by CRLF the next time Git touches it
8.再看一次状态(等待提交): git status
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: TestDemo.txt
9.把文件从暂存区 删掉 又变成了待提交的了(命令): git rm --cached TestDemo.txt
$ git rm --cached TestDemo.txt
rm 'TestDemo.txt'
10.把所有的文件添加到暂存区: git add -A
$ git add -A
warning: in the working copy of 'TestDemo.txt', LF will be replaced by CRLF the next time Git touches it
11.提交文件到本地仓库:git commit TestDemo.txt -m "第一次提交"
提交多个文件 git commit -am " msg"
$ git commit TestDemo.txt -m "第一次提交"
warning: in the working copy of 'TestDemo.txt', LF will be replaced by CRLF the next time Git touches it
[master (root-commit) 4bccf8b] 第一次提交
1 file changed, 1 insertion(+)
create mode 100644 TestDemo.txt
12.查看状态:
$ git status
On branch master
nothing to commit, working tree clean
13提交到本地仓库,输入命令 把文件添加到网络分支上 找到你的远程仓库,添加远程仓库(origin: 是本地仓库名字 随便起):git remote add origin https://gitcode.com/javaghui/javaTest.git
https://gitcode.com/javaghui/android.git
$ git remote add origin https://gitcode.com/javaghui/javaTest.git
14.提交本地仓库: git push origin
在远程仓库中创建一个 master 分支,会提示你 输入这条命令:git push --set-upstream origin master
$ git push origin
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
15. 输入命令, 创建分支,会提示你 输入令牌 (克隆,设置,找到令牌):git push --set-upstream android master
$ git push --set-upstream android master
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (6/6), 508 bytes | 508.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Start Git Hooks Checking [PASSED]
To https://gitcode.com/javaghui/android.git
* [new branch] master -> master
branch 'master' set up to track 'android/master'.
上传成功了.
拉代代码: git clone https://gitcode.com/javaghui/javaTest.git
更新: git pull https://gitcode.com/javaghui/javaTest.git
$ git clone https://gitcode.com/javaghui/javaTest.git
Cloning into 'javaTest'...
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 9 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (9/9), 5.87 KiB | 376.00 KiB/s, done.
下载:
git clone https://gitee.com/javaghui/spring-cloud-config.git 本地新的文件夹名字
其他操作
git log 看日志
$ cat data.txt
2023-11-26 hello git
2023-11-26 hello git
2023-11-23 19:30 hello git
创建文件夹
$ mkdir -p src/main/java/com/
$ echo "package com " > src/main/java/com/Hello.java
$ git add -A;git commit -am "备注";git push
[master fff6461] java
1 file changed, 1 insertion(+)
create mode 100644 src/main/java/com/Hello.java
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (7/7), 474 bytes | 474.00 KiB/s, done.
Total 7 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Start Git Hooks Checking [PASSED]
remote:
remote: To create a merge request for master, visit:
remote: https://gitcode.com/javaghui/javaTest/merge_requests/new?merge_request%5Bsource_branch%5D=master
remote:
To https://gitcode.com/javaghui/javaTest.git
1e18edc..fff6461 master -> master
查看分支
$ git branch
* master
$ git branch -a
a
* master
remotes/origin/Android
remotes/origin/day2
remotes/origin/main
remotes/origin/master
$ git branch -av
a fff6461 java
* master fff6461 java
remotes/origin/Android 6e6128f 第二次提交
remotes/origin/day2 8828376 第一次提交
remotes/origin/main de1eed1 Merge remote-tracking branch 'javaGHui/main'
remotes/origin/master fff6461 java
$ git branch -avv
* master fff6461 [origin/master] java
remotes/origin/Android 6e6128f 第二次提交
remotes/origin/day2 8828376 第一次提交
remotes/origin/main de1eed1 Merge remote-tracking branch 'javaGHui/main'
remotes/origin/master fff6461 java
切换分支
$ git checkout origin/master
Note: switching to 'origin/master'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at fff6461 java
创建分支4种:
当前分支
$ git branch dev
$ git branch -av
a fff6461 java
dev fff6461 java
* master fff6461 java
remotes/origin/Android 6e6128f 第二次提交
remotes/origin/day2 8828376 第一次提交
remotes/origin/main de1eed1 Merge remote-tracking branch 'javaGHui/main'
remotes/origin/master fff6461 java
远程分支
$ git branch httpdev origin/master
branch 'httpdev' set up to track 'origin/master'.
$ git branch -av
a fff6461 java
dev fff6461 java
httpdev fff6461 java
* master fff6461 java
remotes/origin/Android 6e6128f 第二次提交
remotes/origin/day2 8828376 第一次提交
remotes/origin/main de1eed1 Merge remote-tracking branch 'javaGHui/main'
remotes/origin/master fff6461 java
提交创建分支
$ git log
commit fff6461c23c2a6362718271e8aac7eb6681590a7 (HEAD -> master, origin/master, httpdev, dev, a)
Author: LEP <*******@qq.com>
Date: Sun Nov 26 19:46:44 2023 +0800
java
$ git branch branchForCommit fff6461c23c2a6362718271e8aac7eb6681590a7
$ git branch -av
a fff6461 java
branchForCommit fff6461 java
dev fff6461 java
httpdev fff6461 java
* master fff6461 java
remotes/origin/Android 6e6128f 第二次提交
remotes/origin/day2 8828376 第一次提交
remotes/origin/main de1eed1 Merge remote-tracking branch 'javaGHui/main'
remotes/origin/master fff6461 java
Tag创建分支
删除分支
$ git branch -av
branchForCommit fff6461 java
dev fff6461 java
httpdev fff6461 java
* master fff6461 java
remotes/origin/Android 6e6128f 第二次提交
remotes/origin/day2 8828376 第一次提交
remotes/origin/main de1eed1 Merge remote-tracking branch 'javaGHui/main'
remotes/origin/master fff6461 java
$ git branch -d dev
Deleted branch dev (was fff6461).
$ git branch -av
branchForCommit fff6461 java
httpdev fff6461 java
* master fff6461 java
remotes/origin/Android 6e6128f 第二次提交
remotes/origin/day2 8828376 第一次提交
remotes/origin/main de1eed1 Merge remote-tracking branch 'javaGHui/main'
remotes/origin/master fff6461 java
解决冲突
$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 353 bytes | 58.00 KiB/s, done.
From https://gitcode.com/javaghui/javaTest
fff6461..7201443 master -> origin/master
Auto-merging data.txt
CONFLICT (content): Merge conflict in data.txt
Automatic merge failed; fix conflicts and then commit the result.
$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: data.txt
no changes added to commit (use "git add" and/or "git commit -a")
//要修改文件
vim data.txt
<<<<<<< HEAD
2023-11-26 20:00 hello git
=======
2023-11-26 hello git
2023-11-26 hello git
2023-11-23 19:30 hello git
2023-11-29 world
>>>>>>> 7201443b2cbdeaf062b5cb59ec53584a637f0db9
删除 或 都保留 :wq 保存退出
修改后:
$ cat data.txt
2023-11-26 hello git
2023-11-26 hello git
2023-11-23 19:30 hello git
2023-11-29 world
$ git add -A; git commit -am "解决冲突"; git push
[master c023bce] 解决冲突
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 2 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 673 bytes | 673.00 KiB/s, done.
Total 6 (delta 1), reused 0 (delta 0), pack-reused 0
//免输入令牌密码
git config --global credential.helper store
git pull /git push (第一次输入,后续就不用再次数据)