In the last article we introduced the basic concepts of Git. In this article we will talk about branching

branch

A branch in Git is really just a pointer to a commit object, rather than a copy of the current version as in a traditional version control system. Git /refs/heads/git/refs/heads/git/refs/heads/git/refs/heads The master branch contains the last COMMIT ID on the master branch, and the branch represents a chain of commit objects.

HEAD is also a pointer to the current branch, not the commit object. Git has a HEAD file in the.git directory that records the branch to which the HEAD points.

Branch merge

Git branch merge is an example of git branch merge. Let’s assume we are on the master branch

git checkout -b dev

git merge dev

--no-ff
git merge --no-ff dev

Reset

Git reset actually has 3 modes, mixed, soft and hard. If you do not write git reset by default, mixed mode will be called. So what is the difference between the three modes? Reset reset reset reset reset reset reset reset reset reset reset

Stash

So what happens with this git stash? Let’s say when we’re working on Feature1 and we need to work on Feature2 urgently, we need to temporarily save the stuff from Feature1 with this Git stash and work on Feature2. Then go back to Feature1 and pull out the changes you saved using a Git Stash pop. It should be noted that git Stash remembers the current commit ID, so if the stash changes the same file and the same line twice under the same commit, there will be a conflict after the first recovery and the second recovery.

Tag

A tag is a tag object that we mentioned earlier, and it also points to a commit object. The difference between a tag and a branch is that when a commit occurs, the branch moves forward, but once the tag is set, it doesn’t move any more, so it’s usually used when our project reaches a milestone, Used when a new version needs to be released. Git tags are classified into two types: Lightweight and annotated. The difference between lightweight and annotated tags is that one has description information and the other doesn’t.

Diff

Git diff uses the Linux diff module to compare files between files

instruction

  1. View branch list

    git branch

  2. Create a branch

    Git branch Specifies the branch name

  3. Switch branch

    Git Checkout branch name

  4. Switch to the branch where you were last time

    git checkout -

  5. Delete branch ()

    Git branch -d Specifies the branch name

    You cannot delete a branch that is currently in use unless you use git branch -d to merge a branch

  6. Create a new branch and switch to the new branch

    Git checkout -b

  7. Displays the most recent commit message for the current branch

    git branch -v

  8. Merges branches into the current branch

    Git merge branch name

  9. If fast-forward is disabled, a commit ID is added

    Git merge --no-ff branch name

  10. Fallback to last Commit (based on current COMMIT)

    git reset --hard HEAD^

  11. Fallback to last commit (both based on current COMMIT)

    git reset --hard HEAD^^

  12. The last n commits that fall back to the current branch (n before the current COMMIT)

    git reset --hard HEAD~n

  13. Fall back to specify commit

    Git reset --hard commit

  14. Changing the branch name

    Git branch -m Old branch name New branch name

  15. Save the workspace changes

    git stash

  16. List all saves

    git stash list

  17. Manually set stash description

    The default git stash return is

    This is the message that contains the latest commit, so you can modify the description with the following commandgit stash save 'hello basic'

  18. Restores the last save and deletes it from the list

    git stash pop

  19. Restores the most recent save, but will not be deleted from the list

    git stash apply

  20. Apply specifies a version and saves it in the list for deletion

    git stash apply stash@{0}

  21. Manually deletes a specified saved version

    git stash drop stash@{0}

  22. Create lightweight labels

    Git tag v1.0.1

  23. Create an annotation label

    Git tag-a v1.0.2 -m 'release 1.0.2'

  24. Check the label

    Git tag allows you to view all tags, as well as git tag show

  25. Find the label

    Git tag -l ‘v1.0’ git tag -l ‘v1.0’ git tag -l ‘v1.0

  26. Remove the label

    Git tag -d Specifies the tag name

  27. List each row that was modified by who, at what time and by which commit

    Git Blame

  28. Compares the difference between the current file and the workspace file that has been modified into the staging area

    git diff

  29. Compare the current latest COMMIT with the workspace

    git diff HEAD

  30. Compare a COMMIT with a workspace

    git diff commit_id

  31. Compares the difference between the latest commit and the staging area

    git diff --cached

  32. Compare a COMMIT with a staging area

    git diff --cached commit_id

  33. Push local tags that have not been submitted once

    git push origin --tags

  34. Deleting a Remote Label

    You can push an empty tag by calling Git push Origin –delete tag name, or by using Git push Origin :refs/tags/v6.0

  35. Clipping the current remote branch

    Sometimes, user A may execute the command to delete the remote A1 branch. Then, user B will find that the remote A1 branch is stale when he executes git remote show Origin, that is, the remote branch has disappeared, but the local branch still exists. Git remote Prune Origin will delete the stale remote branch

scenario

  1. When we usegit reset --hardExecute when the instruction falls back to the previous COMMITgit logIf I want to go back to a later commit, how do I get my COMMIT ID?

    Git reflog: git reflog: git reflog: git reflog: git reflog: git reflog: git reflog Do not manually modify files in the. Git directory, because manual modification will not be recorded in reflog

  2. I accidentally used a branchGit branch -d Specifies the branch nameForcibly deleted, but also saved back?

    Git reflog (); git reflog (); git reflog (); git reflog (); git reflog (); Then execute git branch with the branch name commit-id to recreate our branch. Git merge commit-id specifies the name of the commit chain to be merged. Git merge commit-id specifies the name of the commit chain to be merged.

Q&A

  1. git checkout commit-idgit reset --hard commit-idWhat’s the difference?

    Both can return to the corresponding commit point. However, unlike reset, checkout will be in a dissociated state. If we do not commit any changes, we will be warned not to jump to another commit. Git branch (); git branch (); git branch ();