Git - git 入门

git 工作区

 

git add .  #将所有文件添加至暂存区

git commit -m "xxx" #将暂存区内容添加至本地库

git push #将本地库内容添加在远程库

git 执行过程

Red corlor means such file has not been put into work cache (暂存区).

git 命令

git 分支管理

git brahch

列出本地已存在的分支,在当前分支前加”*”标记。

git brahch -a

-a 查看本地分支和远程分支

git brahch -r

列出远程分支。

git branch -avv

check current branch: git branch -avv

C:\Projects\git>git branch -avv  
* master                28c9d49 [origin/master] 1 deliver  
  remotes/origin/master 28c9d49 1 deliver  

git branch <branch name>

Create new branch: git branch <branch name>

C:\Projects\git>git branch test  
  
C:\Projects\git>git branch -avv  
* master                28c9d49 [origin/master] 1 deliver  
  test                  28c9d49 1 deliver  
  remotes/origin/master 28c9d49 1 deliver  
  
C:\Projects\git>

git branch 说明

git branch branch1: 创建名为branch1的新分支,但不进行分支切换

git branch -d | -D branch1: 删除branch1分支

git push origin --delete [branch_name] 删除远程分支

git branch -d -r branch1: 删除远程branch1分支

git checkout <branch name>

Switch branch: git checkout <branch name>

C:\Projects\git>git checkout test  
Switched to branch 'test'  
  
C:\Projects\git>git branch -avv  
  master                28c9d49 [origin/master] 1 deliver  
* test                  28c9d49 1 deliver  
  remotes/origin/master 28c9d49 1 deliver 

git checkout 说明

git checkout -- * 撤销上次提交之后所做的修改

git checkout – filename 撤销上次提交之后单个文件的修改

git checkout -b XX切换至分支 XX

git checkout name 没有--,就是创建新分支,名称为name

git merge <merge target>

Git merge: git merge <merge target>

Failed to merge conflict, git status will stay in merging. Thus must need do merge manually.

git remote [-v]

1) Check remote repository: git remote [-v]

C:\Projects\git>git remote  
origin  
  
C:\Projects\git>git remote -v  
origin  https://gitee.com/yangsunior/TestGit.git (fetch)  
origin  https://gitee.com/yangsunior/TestGit.git (push) 

2) add remote repository: git remote add origin http:xxx.xx

3) delete remote repository: git remove remote origin

4) upload new branch to remote: git push –set-upstream origin master

git fetch

获取项目地址

git log

git log

view all submit history.

C:\Projects\git>git branch  
* master  
  test  
  
C:\Projects\git>git log  
commit 28c9d49aa418ff4902bf25e08ae410db45151577 (HEAD -> master, origin/master, test)  
Author: Bill ZHENG <bill.zheng@merckgroup.com>  
Date:   Thu Dec 10 21:55:23 2020 +0800  
  
    1 deliver  
  
C:\Projects\git>  
C:\Projects\git>git checkout test  
Switched to branch 'test'  
  
C:\Projects\git>git log  
commit 28c9d49aa418ff4902bf25e08ae410db45151577 (HEAD -> test, origin/master, master)  
Author: Bill ZHENG <bill.zheng@merckgroup.com>  
Date:   Thu Dec 10 21:55:23 2020 +0800  
  
    1 deliver  
  
C:\Projects\git>  

git log --oneline 

display log with 1 line

C:\Projects\git>git log --oneline  
28c9d49 (HEAD -> master, origin/master, test) 1 deliver  

git log -n 

check specific number of records

C:\Projects\git>git log -2  
commit b502c1581a22f8f4edf6d494fecc20ded430245a (HEAD -> master, origin/master)  
Merge: 6d1621b 80de3a4  
Author: Bill ZHENG <bill.zheng@merckgroup.com>  
Date:   Fri Dec 11 13:22:00 2020 +0800  
  
    resolve confilict  
  
commit 6d1621b47779e913324d4a13f2be2ecb712f3456  
	Author: Bill ZHENG <bill.zheng@merckgroup.com>  
	Date:   Fri Dec 11 13:13:02 2020 +0800  
	  
	    modify at 13:07 

git log 说明

查看所有commit记录,空格向下翻页,b向上翻页,q退出。

git log master..dev: 查看只在maser分支,而不在dev分支的提交。

git log dev..master:查看只在dev分支,而不在master分支的提交。

git config 

git config user.name    # Check name in current local repository
git config user.password    # Check password in current local repository
git config user.email    # Check email in current local repository
git --global config user.name    # Check name in current system
git --global config user.password    # Check password in current system
git --global config user.email    # Check email in current system

C:\Projects\git>git config user.name  
Bill ZHENG  
  
C:\Projects\git>git config --global user.name  
Bill ZHENG  

git diff

Current version, folder and status is as below:

There’s 1 file named git01.txt. and content is as below:

C:\Projects\git>type git01.txt  
"20201211 13:07"  
  
C:\Projects\git>git diff  
  
C:\Projects\git>  

Add new file named git02.txt.

C:\Projects\git>type git02.txt  
"20201211 git02 15:37"  
  
C:\Projects\git>git diff  
  
C:\Projects\git>git status  
On branch master  
Your branch is up to date with 'origin/master'.  
  
	Untracked files:  
	  (use "git add <file>..." to include in what will be committed)  
	        git02.txt  
	  
	nothing added to commit but untracked files present (use "git add" to track)  
	  
	C:\Projects\git>git diff head 

Before do git add, git diff can not track new file.

C:\Projects\git>git add git02.txt  
  
C:\Projects\git>git diff  
  
C:\Projects\git>echo "20201211 git02 15:43" > git02.txt  
  
C:\Projects\git>git diff  
diff --git a/git02.txt b/git02.txt  
index d5421e9..5d1b299 100644  
	--- a/git02.txt  
	+++ b/git02.txt  
	@@ -1 +1 @@  
	-"20201211 git02 15:37"  
	+"20201211 git02 15:43"  
	  
C:\Projects\git>  

After doing git add, git will track file status.

C:\Projects\git>git diff head  
diff --git a/git02.txt b/git02.txt  
new file mode 100644  
index 0000000..5d1b299  
--- /dev/null  
+++ b/git02.txt  
@@ -0,0 +1 @@  
+"20201211 git02 15:43"  

git diff 说明

git diff  比较工作区与暂存区

git diff –cache 比较暂存区与本地库

git diff HEAD 比较工作区与本地库

git withdraw code

git stash & git statsh drop

git show

git show 查看最新commit内容

git show commitID 查看指定commitID修改的内容

git proxy

查看http/https代理

git config --global --get http.proxy/https.proxy

设置http/https代理

git config --global http.proxy [http://135.245.48.34:8000](http://135.245.48.34:8000/)

git config --global https.proxy [https://135.245.48.34:8000](http://135.245.48.34:8000/)

取消http/https代理

git config --global --unset http.proxy

git config --global --unset https.proxy

git 提交代码流程说明

Step1: git clone <remote_url>

Step2: git init <directory>

Step3:

1) add local files or folder to “local cache”

git add <fileName>
git add <directory>

2) add all files and folder to “local cache”

git add -A
git add ./

Step4: remove files or folder from “local cache”

git remove –cached target -r

Step5: add configure file that neglect configuration. à .gitignore

Step6: submit changeset to local repository

git commit -m “commit”

Step7: push code to remote repository.

git push

示例1:本地提文件到远程仓库

Step1: create folder named git within C:\Projects

cd C:\Projects\git  

Step2: init a git content.

C:\Projects\git>git init  
Initialized empty Git repository in C:/Projects/git/.git/ 

Step3: a new git repository without branch master, therefore git commands will not take effect.

C:\Projects\git>git status  
On branch master  
  
No commits yet  
  
nothing to commit (create/copy files and use "git add" to track)

Step4: create file named git01.txt

C:\Projects\git>echo "20201210 001" > git01.txt  

C:\Projects\git>dir  
 Volume in drive C is OSDisk  
 Volume Serial Number is 96F1-F8A9  
  
 Directory of C:\Projects\git  
  
2020/12/10  21:54    <DIR>          .  
2020/12/10  21:54    <DIR>          ..  
2020/12/10  21:54                17 git01.txt  
	               1 File(s)             17 bytes  
	               2 Dir(s)  364,950,126,592 bytes free 

Step5: Do git add, without remote repository, update only orients to local.

C:\Projects\git>git status  
On branch master  
  
No commits yet  
  
Untracked files:  
  (use "git add <file>..." to include in what will be committed)  
        git01.txt  
  
nothing added to commit but untracked files present (use "git add" to track)  
  
C:\Projects\git>git add ./  
  
C:\Projects\git>git commit -m "1 deliver"  
[master (root-commit) 28c9d49] 1 deliver  
 1 file changed, 1 insertion(+)  
 create mode 100644 git01.txt  
  
C:\Projects\git>git diff  
  
C:\Projects\git>  
C:\Projects\git>git status  
On branch master  
nothing to commit, working tree clean  

Step6: execute git commands

C:\Projects\git>git diff  
  
C:\Projects\git>  
C:\Projects\git>git status  
On branch master  
nothing to commit, working tree clean  
  
C:\Projects\git>git push  
fatal: No configured push destination.  
Either specify the URL from the command-line or configure a remote repository using  
  
    git remote add <name> <url>  
  
and then push using the remote name  
  
    git push <name>  
  
C:\Projects\git>  
C:\Projects\git>git branch  
* master  
  
C:\Projects\git>git branch  -av  
* master 28c9d49 1 deliver 

These commands will not take effect since there is not ,remote repository. Create repository named TestGit in 码云.

Step7: set remote branch and push git01.txt.

C:\Projects\git>git remote add origin https://gitee.com/yangsunior/TestGit.git  
  
C:\Projects\git>git push  
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  
  
C:\Projects\git>git push -u origin master  
Username for 'https://gitee.com': 821345488@qq.com  
Password for 'https://821345488@qq.com@gitee.com':  
Enumerating objects: 3, done.  
Counting objects: 100% (3/3), done.  
Writing objects: 100% (3/3), 239 bytes | 239.00 KiB/s, done.  
Total 3 (delta 0), reused 0 (delta 0)  
remote: Powered by GITEE.COM [GNK-5.0]  
To https://gitee.com/yangsunior/TestGit.git  
 * [new branch]      master -> master  
Branch 'master' set up to track remote branch 'master' from 'origin'.  
  
C:\Projects\git>git diff  
  
C:\Projects\git>git branch -av  
* master                28c9d49 1 deliver  
  remotes/origin/master 28c9d49 1 deliver 

示例2:git 提交代码过程展示

C:\Projects\git>git branch -avv  
* master                28c9d49 [origin/master] 1 deliver  
  test                  28c9d49 1 deliver  
  remotes/origin/master 28c9d49 1 deliver  
  
C:\Projects\git>type git01.txt  
"20201210 001"  
  
C:\Projects\git>echo "20201211 11:02" > git01.txt  
	  
C:\Projects\git>type git01.txt  
"20201211 11:02"  
  
C:\Projects\git>git diff  
diff --git a/git01.txt b/git01.txt  
index 2458422..73a5b94 100644  
--- a/git01.txt  
+++ b/git01.txt  
@@ -1 +1 @@  
-"20201210 001"  
+"20201211 11:02"  
  
C:\Projects\git>git status  
On branch master  
Your branch is up to date with 'origin/master'.  
  
Changes not staged for commit:  
  (use "git add <file>..." to update what will be committed)  
  (use "git restore <file>..." to discard changes in working directory)  
        modified:   git01.txt  
  
no changes added to commit (use "git add" and/or "git commit -a")  
  
C:\Projects\git>git add ./  
  
C:\Projects\git>git diff  
  
C:\Projects\git>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)  
        modified:   git01.txt  
  
  
C:\Projects\git>git commit -m "update git01.txt at 11:03"  
[master 0ee1e5e] update git01.txt at 11:03  
 1 file changed, 1 insertion(+), 1 deletion(-)  
  
C:\Projects\git>git diff  
  
C:\Projects\git>git status  
On branch master  
Your branch is ahead of 'origin/master' by 1 commit.  
  (use "git push" to publish your local commits)  
  
nothing to commit, working tree clean  
  
C:\Projects\git>git branch -avv  
* master                0ee1e5e [origin/master: ahead 1] update git01.txt at 11:03  
  test                  28c9d49 1 deliver  
  remotes/origin/master 28c9d49 1 deliver  
  
C:\Projects\git>git push  
Username for 'https://gitee.com': 821345488@qq.com  
Password for 'https://821345488@qq.com@gitee.com':  
Enumerating objects: 5, done.  
Counting objects: 100% (5/5), done.  
Writing objects: 100% (3/3), 283 bytes | 283.00 KiB/s, done.  
Total 3 (delta 0), reused 0 (delta 0)  
remote: Powered by GITEE.COM [GNK-5.0]  
To https://gitee.com/yangsunior/TestGit.git  
   28c9d49..0ee1e5e  master -> master  
  
C:\Projects\git>  
C:\Projects\git>git branch -avv  
* master                0ee1e5e [origin/master] update git01.txt at 11:03  
  test                  28c9d49 1 deliver  
  remotes/origin/master 0ee1e5e update git01.txt at 11:03  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值