#配置用户信息 git config
git config --global user.name "jacobtsang"
git config -- global user.email "xxxxxxx@qq.com"
cat ~/.gitconfig
#初始化一个本地仓库,同时Git帮助我们初始化了一个master的分支名。 git init
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial
$ git init
Initialized empty Git repository in C:/Users/MY-PC/Desktop/git-tutorial/.git/
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
#显示当前目录下文件,初始创建时为空 ll
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ ll
total 0
#创建一个文本 touch
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ touch test.txt
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ ll
total 0
-rw-r--r-- 1 MY-PC 197609 0 6月 7 09:38 test.txt
#查看当前仓库状态 git status
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ git status
On branch master #当前在master分支
No commits yet #没有提交
Untracked files: #未跟踪文件列表
(use "git add <file>..." to include in what will be committed) #提示我通过此命令添加跟踪
test.txt
nothing added to commit but untracked files present (use "git add" to track)
#暂存test.txt ,git add -A
命令将当前仓库目录下所有文件加入版本控制 / git add
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ git add test.txt
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage) #通过此命令可恢复上一个状态
new file: test.txt
#恢复上一个状态 git rm --cached
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ git rm --cached test.txt
rm 'test.txt'
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
nothing added to commit but untracked files present (use "git add" to track)
#查看git的暂存区
#git add只上传在暂存区进行版本控制
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ git add -A test.txt
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ ll -la
total 52
drwxr-xr-x 1 MY-PC 197609 0 6月 7 09:38 ./
drwxr-xr-x 1 MY-PC 197609 0 6月 7 09:33 ../
drwxr-xr-x 1 MY-PC 197609 0 6月 7 09:45 .git/
-rw-r--r-- 1 MY-PC 197609 0 6月 7 09:38 test.txt
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ cd .git/
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial/.git (GIT_DIR!)
$ ll
total 8
-rw-r--r-- 1 MY-PC 197609 130 6月 7 09:34 config
-rw-r--r-- 1 MY-PC 197609 73 6月 7 09:34 description
-rw-r--r-- 1 MY-PC 197609 23 6月 7 09:34 HEAD
drwxr-xr-x 1 MY-PC 197609 0 6月 7 09:34 hooks/
-rw-r--r-- 1 MY-PC 197609 104 6月 7 09:45 index
drwxr-xr-x 1 MY-PC 197609 0 6月 7 09:34 info/
drwxr-xr-x 1 MY-PC 197609 0 6月 7 09:42 objects/
drwxr-xr-x 1 MY-PC 197609 0 6月 7 09:34 refs/
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial/.git (GIT_DIR!)
$ cat index
DIRC\▒▒(#\▒▒(1▒D▒▒▒⛲▒▒CK▒)▒wZ▒▒▒Stest.txtw▒Ƭ▒▒▒ ▒gG8▒▒6h▒
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial/.git (GIT_DIR!)
#最终确认需要git commit,一般我们通过-m参数(message)写备注 git commit
#此时test.txt提交在本地仓库中,执行了一次交付状态。相当于拍了一次快照。
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial/.git (GIT_DIR!)
$ cd ..
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ git commit -m "add test.txt"
[master (root-commit) bf031db] add test.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
#将代码推到远程仓库中
#首先在github上创建本地仓库
#如果是一个新的仓库,执行以下代码
echo "# git-tutorial" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:Jacobtsang/git-tutorial.git
git push -u origin master
#如果仓库中已经有内容,执行以下代码
git remote add origin git@github.com:Jacobtsang/git-tutorial.git
git push -u origin master
因为我们仓库中已经有了test.txt,因此我们选择后者。
origin是远端约定俗成的名字。不建议修改。
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ git remote add origin git@github.com:Jacobtsang/git-tutorial.git
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ git remote -v
origin git@github.com:Jacobtsang/git-tutorial.git (fetch)
origin git@github.com:Jacobtsang/git-tutorial.git (push)
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ git push origin master -u
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 207 bytes | 51.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:Jacobtsang/git-tutorial.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
#将本地的文件推送上远端仓库 -u参数表示以后直接git push即可。
#执行命令后刷新页面,上传成功。
#远程文件拉取到本地 git clone
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ cd ..
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop
$ git clone git@github.com:Jacobtsang/git-tutorial.git
fatal: destination path 'git-tutorial' already exists and is not an empty directory.
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop
$ git clone git@github.com:Jacobtsang/git-tutorial.git git-demo
Cloning into 'git-demo'...
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
模拟合作开发,在demo仓里修改test.txt后git push
到远端仓库
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop
$ cd git-demo/
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-demo (master)
$ ll
total 0
-rw-r--r-- 1 MY-PC 197609 0 6月 7 10:04 test.txt
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-demo (master)
$ vim test.txt
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-demo (master)
$ cat test.txt
hello world
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-demo (master)
$ git add test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-demo (master)
$ git commit -m "modified test.txt"
[master c0b5233] modified test.txt
1 file changed, 1 insertion(+)
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-demo (master)
$ git push
Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 252 bytes | 126.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:Jacobtsang/git-tutorial.git
bf031db..c0b5233 master -> master
#在git-demo仓库里面修改test.txt,并不会影响git-tutorial仓库中的test.txt
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-demo (master)
$ cd ..
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop
$ cd git-tutorial
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ cat test.txt
#git-tutorial中是旧的代码,而远端仓库中已经被git-demo更新为新的代码
#通过git pull将远端新的代码拉取到本地
#可以添加链接和分支参数 Example:git pull origin master
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:Jacobtsang/git-tutorial
bf031db..c0b5233 master -> origin/master
Updating bf031db..c0b5233
Fast-forward
test.txt | 1 +
1 file changed, 1 insertion(+)
MY-PC@DESKTOP-S026G4J MINGW64 /c/Users/MY-PC/Desktop/git-tutorial (master)
$ cat test.txt
hello world