常用的Git操作都在这里了!

git是程序员刚需工具;今天就来总结下git常用操作;

    一克隆项目

这里我在GitHub上创建了一个示例库

根据如下步骤我们就可以看到本地已经克隆一份出来了,进入目录查看就看到了

README.md文件了

momodeMBP:~ momo$ git clone [email protected]:zhuifengxia/examplegit.git
Cloning into 'examplegit'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
momodeMBP:~ momo$ cd examplegit
momodeMBP:examplegit momo$ ls
README.md
momodeMBP:examplegit momo$

注:大家使用的时候,git库地址换成自己的就可以了

    二管理修改

我们需要对项目添加文件或者编辑文件;之后如何提交到远程库呢?

现在我们来添加一个文件gitexample.txt;查看状态;

momodeMBP:examplegit momo$ 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)

    gitexample.txt

nothing added to commit but untracked files present (use "git add" to track)
momodeMBP:examplegit momo$

上面的状态告诉我们gitexample.txt文件是新增的;

然后添加(到暂存区);

momodeMBP:examplegit momo$ git add gitexample.txt 
momodeMBP:examplegit momo$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   gitexample.txt

momodeMBP:examplegit momo$

然后提交:git commit -m 'new file gitexample' (提交并写提交日志)

momodeMBP:examplegit momo$ git commit -m 'new file gitexample'
[master 06951cb] new file gitexample
 1 file changed, 1 insertion(+)
 create mode 100644 gitexample.txt
momodeMBP:examplegit momo$ 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
momodeMBP:examplegit momo$

然后提交到远程库;git push

momodeMBP:examplegit momo$ git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 290 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:zhuifengxia/examplegit.git
   2306378..06951cb  master -> master
momodeMBP:examplegit momo$

以上操作就是完成了新建一个文件并提交到远程库,这样其他人克隆项目也能看到这个文件

如果是修改文件,我们对某个文件进行编辑;status 就是modified

momodeMBP:examplegit momo$ 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 checkout -- <file>..." to discard changes in working directory)

    modified:   gitexample.txt

no changes added to commit (use "git add" and/or "git commit -a")
momodeMBP:examplegit momo$

然后进行 add、commit;

因为是修改文件,为了防止有其他人也更新了同一个文件,我们先更新 git pull

更新完成之后再git push

当然如果你不执行更新操作,如果真的出现其他人更新同一个文件,git也会提示你先git pull

撤销修改:

1、命令git checkout -- gitexample.txt意思就是,把gitexample.txt文件在工作区的修改全部撤销,这里有两种情况:

一种是gitexample.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;

一种是gitexample.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。

总之,就是让这个文件回到最近一次git commitgit add时的状态。

momodeMBP:examplegit momo$ cat gitexample.txt 
error--test changes

momodeMBP:examplegit momo$ git checkout -- gitexample.txt 
momodeMBP:examplegit momo$ cat gitexample.txt 
test changes

momodeMBP:examplegit momo$

用命令git reset HEAD gitexample.txt可以把暂存区的修改撤销掉(unstage),重新放回工作区:

momodeMBP:examplegit momo$ git reset gitexample.txt 
Unstaged changes after reset:
M   gitexample.txt
momodeMBP:examplegit momo$

也可以把暂存区的修改回退到工作区。当我们用HEAD时,表示最新的版本。

这时候暂存区就干净了;

    三版本回退

上面我们将了如何撤销修改,现在讲下如何进行版本回退

如果我们已经提交了commit;返回提交前的内容如下命令

momodeMBP:examplegit momo$ cat gitexample.txt 
error--test changes
momodeMBP:examplegit momo$ git reset --hard HEAD^
momodeMBP:examplegit momo$ cat gitexample.txt 
test changes
momodeMBP:examplegit momo$

注:首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交3628164...882e1e0(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100

查看提交历史

momodeMBP:examplegit momo$ git log
commit 474cf1bd7c57b5b7a8eedf67d354f802edd17acd
Author: momo <[email protected]>
Date:   Wed Jul 19 09:55:27 2017 +0800
    edit file
commit 498234bde2c05ea24b5ead48ad2f3e8e88210b7b
Author: momo <[email protected]>
Date:   Wed Jul 19 09:54:30 2017 +0800
    new file

我们可以回退指定版本,找到指定版本的commit id 我们执行如下的时候不需要commit id录入完,一部分就可以;

momodeMBP:examplegit momo$ git reset --hard 474cf1bd7c57b5b7a8eed

HEAD指向的版本就是当前版本,因此,Git允许我们在版本的历史之间穿梭,使用命令git reset --hard commit_id
穿梭前,用git log可以查看提交历史,以便确定要回退到哪个版本。
要重返未来,用git reflog查看命令历史,以便确定要回到未来的哪个版本。

    四删除文件

有时候需要把没用的文件删掉;

momodeMBP:examplegit momo$ git rm gitexample.txt 
rm 'gitexample.txt'
momodeMBP:examplegit momo$ git commit -m "remove gitexample.txt"
[master e22ddc4] remove gitexample.txt
 1 file changed, 2 deletions(-)
 delete mode 100644 gitexample.txt
momodeMBP:examplegit momo$

现在,文件就从版本库中被删除了。

也有可能出现误删的情况:还原

momodeMBP:examplegit momo$ git checkout -- gitexample.txt

git checkout其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。

    五 创建合并分支

创建momo分支,然后切换到momo分支;

momodeMBP:examplegit momo$ git checkout -b momo
Switched to a new branch 'momo'
momodeMBP:examplegit momo$

git checkout命令加上-b参数表示创建并切换,相当于以下两条命令:

momodeMBP:examplegit momo$ git branch momo
momodeMBP:examplegit momo$ git checkout momo
Switched to branch 'momo'

使用git branch 查看当前分支:

momodeMBP:examplegit momo$ git branch
  master
* momo
momodeMBP:examplegit momo$

git branch命令会列出所有分支,当前分支前面会标一个*号。
我们在这个分支上进行操作添加文件,并提交

momodeMBP:examplegit momo$ git commit -m 'branch'
[momo 89bf816] branch
 1 file changed, 1 insertion(+)
 create mode 100644 branch.txt
momodeMBP:examplegit momo$ ls
README.md   branch.txt
momodeMBP:examplegit momo$

现在切换到master分支上去;

momodeMBP:examplegit momo$ git checkout master
Switched to branch 'master'
momodeMBP:examplegit momo$ ls
README.md
momodeMBP:examplegit momo$

你会发现我们在momo分支上建的branch.txt文件不见了!
现在我们把momo分支上内容合并到master分支上

momodeMBP:examplegit momo$ git merge momo
Updating e3c1f9c..89bf816
Fast-forward
 branch.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 branch.txt
momodeMBP:examplegit momo$ ls
README.md   branch.txt
momodeMBP:examplegit momo$

现在发现master分支上也有branch.txt文件了;说明合并已经完成了;
我们可以把创建的分支删除了;

momodeMBP:examplegit momo$ git branch -d momo
Deleted branch momo (was 89bf816).
momodeMBP:examplegit momo$ git branch
* master
momodeMBP:examplegit momo$

现在看到分支只剩下master了;

    六 查看修改

查看修改内容使用git diff ;

momodeMBP:examplegit momo$ git diff diff.txt 
diff --git a/diff.txt b/diff.txt
index e779424..3aba9ce 100644
--- a/diff.txt
+++ b/diff.txt
@@ -1 +1 @@
-diff
+changes-diff
momodeMBP:examplegit momo$

关于git的使用操作暂时写到这里了

猜你喜欢

转载自blog.csdn.net/momo_mutou/article/details/81835158