directory

Git’s core concept: reference everything

  • 1. Basic concepts of Git
  • 2. Actual operation of Git
  • Git in depth 2: Interactive Rebase, Interactive Add and workflow 3, other common directives and Git flows

Git’s deep core concept: Reference everything

1. Basic concepts of Git

One of the most important differences between a Git distributed version control system and a central repository is that it will have a local repository. Compared with traditional SVN, it has the following advantages: 1, there is no need to worry about submitting code without network, and local office can be done; 2, SVN can also work without network, but there is a COMMIT record for submitting code, which cannot be generated without network on SVN; 3, collaborative development of the team. Improved efficiency, especially local branch building, function development, and branch maintenance. (Git flow,rebase and other common functions are mentioned in the article.) 4. The temporary storage area and workspace function provided in the local repository can solve the difficulty of collaborative development between branches. For example, I have two branches, A and B. I have modified some files in branch A, but I need to switch to branch B for development at this time. Then I can put these files into the temporary storage area without submitting them

2. Actual operation of Git

Let’s get familiar with how Git does some common things. First use Git Clone to clone a remote repository

$ git clone https://github.com/micaixiaoduanku/hello-world.git
Cloning into 'hello-world'. remote: Enumerating objects: 41, done. remote: Total 41 (delta 0), reused 0 (delta 0), pack-reused 41 Unpacking objects: 100% (41/41), done.Copy the code

Then modify the file and place it in the staging area

vim README.md 
$ git add README.md 
$ 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)

    modified:   README.md
Copy the code

Then commit to the local repository

$ git commit -m "First submission"[master 62Fb97b] 1 File changed, 1 insertion(+), 3 deletions(-)Copy the code

Git log look at the commit log

git logCommit 62 fb97b3d93d8bf347049921dfb7f7eddc58ff16 (HEAD - > master) submitted for the first timeCopy the code

Each commit generates a unique ID. Finally push to the remote end:

$ git push origin master Counting objects: 100% (5/5), done. Delta compression using up to 8 threads Compressing objects: 100% (1/1), done. Writing objects: 272.00 100% (3/3), 272 bytes | KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/micaixiaoduanku/hello-world.git de97033.. 62fb97b master -> masterCopy the code

Git branch creation and switching

git logcommit 62fb97b3d93d8bf347049921dfb7f7eddc58ff16 (HEAD -> master, origin/master, origin/HEAD) Author: XXXXXX <[email protected]> Date: Sun Jan 6 21:46:07 2019 +0800Copy the code

So now HEAD is the master that points to, so this is the master branch, so let’s create a branch and switch to it

$ git branch feature1
$ git checkout feature1
Switched to branch 'feature1'
huanglideMacBook-Pro:hello-world huangli$ git log
commit 62fb97b3d93d8bf347049921dfb7f7eddc58ff16 (HEAD -> feature1, origin/master, origin/HEAD, master)
Copy the code

For now, notice that HEAD points to Feature1

3. References in Git

The current code is always HEAD. As a new commit passes

HEAD always means to the current position.

Origin /master and origin/HEAD

Origin/Master and Origin /HEAD are mirror images of remote repositories.

What does Git pull do?

Git merge (fast forward)

Git dig two: Interactive Rebase, interactive Add, and workflow

Fast Forward in merge

In this case, Master merge Feature1 is fast forward and does not generate new commits. However, if you want to do this, you can do it without fastForward

git merge --no-ff feature1
Copy the code

Master Merge Feature2 is a normal merge that generates new commits

feature branching

During development, we encountered a situation where the remote and local branches were out of sync, as follows

In this case, the local master cannot push to the remote master, so it must merge first

If you are unlucky, the remote branch and the local branch are out of sync. At this point, you need to merge again

Git rebase operation

The actual operation of Rebase

This is what happens when feature1 rebase master

Interactive Rebase mainly provides some optional functions in the Rebase process, such as merging commit, modifying some commit information, and so on.

git rebase -i HEAD~2
Copy the code
Pick 62fB97b first submits pick 4F94057 second submits pick 0AD478e TTT# Rebase de97033.. 0ad478e onto de97033 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c 
      
        to reword the commit message.
      
Copy the code