git使用入门

本文是学习笔记

地址(廖雪峰的git教程):https://www.liaoxuefeng.com/wiki/896043488029600

  • 将一个目录作为git的仓库,进入到此目录执行git init命令
$ cd git_demo/

$ git init
Initialized empty Git repository in /Users/supery/git_demo/.git/
  • 将该目录下的文件添加到git仓库,该目录下新建了一个readme.txt文件,下面的三个命令 git status、git add readme.txt、git commit -m “message”。并且下面在执行git add前后比较了git status的状态。
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.DS_Store
	readme.txt

nothing added to commit but untracked files present (use "git add" to track)

$ git add readme.txt

$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   readme.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.DS_Store

$ git commit -m "wrote a readme file"
[master (root-commit) 38e53ff] wrote a readme file
 Committer: ** <suery@suerydeMacBook-Pro.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 2 insertions(+)
 create mode 100644 readme.txt
  • 下面的命令是查看文件的修改内容
$ git diff readme.txt
diff --git a/readme.txt b/readme.txt
index 013b5bc..cf7033e 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,3 @@
 Git is a distributed version control system.
-Git is free software.
\ No newline at end of file
+Git is free software.
+modified by suery
\ No newline at end of file
  • 查看git的提交日志,加上参数 --pretty=oneline 可以省略显示
$ git log
commit 5d2589a400e89add3213c7a00d5082fff23fe815 (HEAD -> master)
Author: ** <suery@suerydeMacBook-Pro.local>
Date:   Fri May 10 16:43:52 2019 +0800

    修改2

commit 7b116f1fbc6b8b386f28db435cbd123aae6d9944
Author: ** <suery@suerydeMacBook-Pro.local>
Date:   Fri May 10 16:35:08 2019 +0800

    修改

commit 38e53ff421937a957e9f99f177fe07da7f31bbcd
Author: ** <suery@suerydeMacBook-Pro.local>
Date:   Fri May 10 15:56:56 2019 +0800

    wrote a readme file

$ git log --pretty=oneline
5d2589a400e89add3213c7a00d5082fff23fe815 (HEAD -> master) 修改2
7b116f1fbc6b8b386f28db435cbd123aae6d9944 修改
38e53ff421937a957e9f99f177fe07da7f31bbcd wrote a readme file
  • git中的HEAD表示当前版本,上面HEAD->master表示当前版本在 “修改2” 这个版本。reset --hard HEAD^ 命令可以回到上一个版本。也可以使用reset --hard <commitId >回到指定的版本中。
$ git reset --hard HEAD^
HEAD is now at 7b116f1 修改

$ git log --pretty=oneline
7b116f1fbc6b8b386f28db435cbd123aae6d9944 (HEAD -> master) 修改
38e53ff421937a957e9f99f177fe07da7f31bbcd wrote a readme file

$ git reset --hard 5d2589a400e89a
HEAD is now at 5d2589a 修改2

$ git log --pretty=oneline
5d2589a400e89add3213c7a00d5082fff23fe815 (HEAD -> master) 修改2
7b116f1fbc6b8b386f28db435cbd123aae6d9944 修改
38e53ff421937a957e9f99f177fe07da7f31bbcd wrote a readme file
  • reflog 命令记录了每一次命令是的当前commit id
$ git reflog
5d2589a (HEAD -> master) HEAD@{0}: reset: moving to 5d2589a400e89a
7b116f1 HEAD@{1}: reset: moving to HEAD^
5d2589a (HEAD -> master) HEAD@{2}: commit: 修改2
7b116f1 HEAD@{3}: commit: 修改
38e53ff HEAD@{4}: commit (initial): wrote a readme file
  • 在git_demo目录下修改readme.txt和新增license文件,执行git status
$ git status
On branch master
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:   readme.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.DS_Store
	license.txt

no changes added to commit (use "git add" and/or "git commit -a")
  • 然后再执行下面的命令
$ git add readme.txt license.txt
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	new file:   license.txt
	modified:   readme.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.DS_Store
$ git commit -m "create license and modify readme"
[master f1f6fb8] create license and modify readme
 Committer: ** <suery@suerydeMacBook-Pro.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 2 files changed, 3 insertions(+), 2 deletions(-)
 create mode 100644 license.txt
 
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.DS_Store

nothing added to commit but untracked files present (use "git add" to track)
  • 下面讲解git reset HEAD 和git checkout – file(特别要注意: --,是两个"-")的功能
$ git status
On branch master
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:   readme.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.DS_Store

no changes added to commit (use "git add" and/or "git commit -a")

$ git checkout -- readme.txt 

$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.DS_Store

nothing added to commit but untracked files present (use "git add" to track)

$ git status
On branch master
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:   readme.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.DS_Store

no changes added to commit (use "git add" and/or "git commit -a")

$ git add readme.txt

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   readme.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.DS_Store
	
$ git reset HEAD readme.txt 
Unstaged changes after reset:
M	readme.txt

$ git status
On branch master
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:   readme.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.DS_Store

no changes added to commit (use "git add" and/or "git commit -a")

上面的执行结果说明,git reset HEAD 是在git add之后回退unstage,git checkout – file是在git add想要将修改的文件回退到修改之前的状态(即抹去修改过的内容)。

  • git 删除,执行下面的命令之后执行git commit -m "remove file"命令
$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	deleted:    license.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.DS_Store

no changes added to commit (use "git add" and/or "git commit -a")

$ git rm license.txt

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	deleted:    license.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.DS_Store
  • 接下来我们要学习git远程仓库的相关内容了,这我们直接用GitHub,注册一个账号即可使用。

首先我们看自己的用户主目录下是否有.ssh这个目录,若有再看这个目录下是否有id_rsa和id_rsa.pub这两个文件,若是没有则需要执行下面命令

$ ssh-keygen -t rsa -C "youremail@example.com"

执行了以上命令就会在用户主目录上看到前面说的.ssh目录和id_rsa和id_rsa.pub文件了。其中前面的私钥后面的是公钥。然后将公钥设置到GitHub的SSH key中。

  • 之后再本地仓库目录下执行下面的命令,将本地仓库和远程仓库关联起来。下面的地址是我的GitHub上仓库的地址。
$ git remote add origin https://github.com/superY-25/git-demo.git
$ git remote
origin
  • 现在就可以用来提交(push)文件了。
$ git push origin master
Username for 'https://github.com': username
Password for 'https://username@github.com': ******
Counting objects: 16, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (16/16), 1.48 KiB | 757.00 KiB/s, done.
Total 16 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To https://github.com/superY-25/git-demo.git
 * [new branch]      master -> master
  • 从远程仓库clone项目
$ git clone git@github.com:superY-25/git-demo.git
Cloning into 'git-demo'...
Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 17 (delta 3), reused 16 (delta 2), pack-reused 0
Receiving objects: 100% (17/17), done.
Resolving deltas: 100% (3/3), done.
  • 创建并切换到新分支dev上
$ git checkout -b dev
Switched to a new branch 'dev'

上面的命令相当于下面的两个命令

$ git branch dev
$ git checkout dev
Switched to branch 'dev'
  • 查看本地仓库分支
$ git branch
* dev
  master
  • 查看远程分支
$ git branch -r
  origin/master
  • 我们在当前的dev分支上进行修改readme.txt文件,然后commit。这时我们切换到master分支上是readme.txt文件还是在未修改之前。因此我们需要在master分支上执行下面的命令进行合并。
$ git merge dev
Updating 250ff58..01983e5
Fast-forward
 readme.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

注意要将dev分支合并到master上,必须切换到master分支执行merge命令。

  • 删除分支
$ git branch -d dev
Deleted branch dev (was 01983e5).
  • 当我们创建了一个新的分支,并且在两个同时更改了内容commit提交了,这时当我们合并两个分支时会出现冲突
$ git merge feature1
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.

$ git status
On branch master
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:   readme.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.DS_Store

no changes added to commit (use "git add" and/or "git commit -a")
  • 接下来我们需要手动解决这个冲突,然后再提交。我们需要手动修改冲突的文件然后执行add 和commit命令,执行之后冲突就解决了(在master分支merge其它分支上的代码产生冲突,需要手动更改冲突文件然后重新add和commit,其它未冲突的文件已经merger到master分支。所以最终只需要修改提交master分支就好了。这个冲突的解决确实是麻烦)
$ git add readme.txt 
$ git commit -m "conflict fixed"
[master ffa826c] conflict fixed
 Committer: ** <supery@superydeMacBook-Pro.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author
  • 合并分支时可以使用 --no-ff参数保留合并历史信息。
$ git merge --no-ff -m "merge with no-ff" dev
Merge made by the 'recursive' strategy.
 readme.txt | 1 +
 1 file changed, 1 insertion(+)

上面命令中的-m参数是merge的描述,和commit的提交描述一样。

  • stash命令的使用场景:当正在一个分支进行开发时,突然临时要改一个bug,但是当前的工作又不能提交,这时我们直接切换到master分支时不安全的,会将当前分支更改的代码移到master分支
$ git checkout master
M	readme.txt
Switched to branch 'master'

因此需要先执行stash命令,将当前分支的更改“隐藏”起来。然后切换到master分支,再新增一个临时修改bug的分支进行修改bug并提交,最后切换到master分支进行merge。

$ git stash
Saved working directory and index state WIP on dev: a1b60e3 go back

$ git checkout master
Switched to branch 'master'

$ git checkout -b bug
Switched to a new branch 'bug'

$ git add license.txt 

$ git commit -m "fix bug"
[bug 03e3a5c] fix bug
 Committer: ** <supery@superydeMacBook-Pro.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 2 insertions(+), 1 deletion(-)
 
$ git checkout master
Switched to branch 'master'

$ git merge bug
Updating a1b60e3..03e3a5c
Fast-forward
 license.txt | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

修改完bug我们在切换回之前开发的分支dev,此时我们看status是干净的,并没有之前的开发记录,因为之前在切换到master分支之前执行了git stash命令,现在执行一下git stash list命令看一下stash列表。现在我们需要将其恢复。有两个办法:一是用git stash apply恢复,但是恢复后,stash内容并不删除,你需要用git stash drop来删除;另一种方式是用git stash pop,恢复的同时把stash内容也删了。

$ git checkout dev

$ git status
On branch dev
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.DS_Store

nothing added to commit but untracked files present (use "git add" to track)

$ git stash list
stash@{0}: WIP on dev: a1b60e3 go back

$ git stash pop
On branch dev
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:   readme.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	.DS_Store

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (801ed092605c2d30fe18b8f5b33cd88b83362bfa)

如果有多个stash,可以使用命令git stash apply stash@{0}指定恢复哪个stash,最终我们提交当前分支并merge到master分支。

$ git merge --no-ff -m "merge test stash" dev
Merge made by the 'recursive' strategy.
 readme.txt | 1 +
 1 file changed, 1 insertion(+)

  • 远程仓库,git remote查看远程仓库,remote -v查看远程仓库的详细信息,git push origin master本地提交推送到远程仓库
$ git remote
origin

$ git remote -v
origin	https://github.com/supery/git-demo.git (fetch)
origin	https://github.com/supery/git-demo.git (push)

$ git push origin master
Counting objects: 12, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (12/12), 1.10 KiB | 1.10 MiB/s, done.
Total 12 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 1 local object.
To https://github.com/supery/git-demo.git
   987ad6e..3e8c33f  master -> master
  • 远程分支,在远程仓库上创建一个新的dev分支
$ git branch -r
  origin/dev
  origin/master
  
$ git push origin dev
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 348 bytes | 348.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/superY-25/git-demo.git
   3e8c33f..cfe5d72  dev -> dev
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值