preface

This article is the study notes of Git, please follow liao Xuefeng Git tutorial.

Important concepts

1. Version repository & staging area

Git is the version library of Git. Git’s repository contains a number of things, the most important of which is a staging area called stage (or index), the first branch that Git automatically creates for us, master, and a pointer to master called HEAD.

Git add adds a file to the staging area.

Git commit Commits the changes, essentially committing the entire contents of the staging area to the current branch.

2. Commit ID (version)

A bunch of things like 1094adb… Git has a commit id (version number). Unlike SVN, Git has a commit id not 1,2,3… An increasing number, but a very large number calculated by SHA1, expressed in hexadecimal.

Why is the COMMIT ID represented by such a large string of numbers?

Because Git is a distributed version control system, many people are working in the same repository, and if everyone uses 1,2,3… As a version number, that’s definitely a conflict.

3. The HEAD pointer

The version that HEAD points to is the current version, and the reason why the version can be rolled back so quickly is that it actually only changes the HEAD pointer.

4. Create and merge branches

HEAD doesn’t technically point to the commit, it points to the master, and the master points to the commit, so HEAD points to the current branch.

Git uses the master branch to point to the latest commit and the HEAD branch to point to the master to determine the current branch and its commit point. Every time you commit, the master branch moves a step forward, so as you commit, the master branch gets longer and longer.

When we create a new branch such as dev, Git creates a new pointer called dev to point to the same commit as master, and heads to dev to indicate that the current branch is on dev.

Create and switch branches: git checkout -b dev

Switch back to the master branch: Git Checkout Master

Git merge dev to master

Git branch -d dev

5. Branch strategy

In actual development, we should follow a few basic principles for branch management:

First of all, the Master branch should be very stable, that is, it should only be used to release new versions, and not work on it.

So where do you work? All the work is on the dev branch, that is, the dev branch is not stable, and at some point, such as a 1.0 release, the dev branch is merged with the master branch, and the 1.0 release is released on the master branch.

You and your friends each work on the Dev branch, and each has his or her own branch, so you can merge into the Dev branch from time to time.

So, the teamwork branch would look something like this:

Common commands

Configuration commands

Git config --global user.name"[name]"
git config --global user.email "[email address]"
Copy the code

New code base

Create a new Git code library in your current directory, Git initCopy the code

Add file

Git add [file1] [file2]... Add all files in the current directory to the staging area git add.Copy the code

Submit code

Git commit -m [message]Copy the code

branch

Git branch -r Create a new branch. Git branch [branch-name] Create a new branch. Git checkout [branch-name] git checkout [branch-name] Git branch --set-upstream [branch] [remote-branch] Git merge [branch] Delete a branch-dGit push origin --delete [branch-name]Copy the code

Check the information

Git status Displays the version history of the current branch gitlogGit reflog displays differences between staging and workspace git diffCopy the code

Remote synchronization

Git remote -v; Git checkout -b branch-name origin/branch-name establish the connection between the local branch and remote branch. Git branch --set-upstream Git pull [remote] [branch] Upload the local branch to the remote repository git push [remote] [branch]Copy the code

undo

Git checkout [file] Restore all files in the staging area to the workspace git checkout. Git reset --hard commit_id When you change the contents of a file in your workspace and want to discard the workspace changes directly, use the git checkout [file] command. Git reset HEAD [file] git checkout [file] git checkout [file]Copy the code

Delete the file

Git mv [file-original] [file-renamed]...Copy the code

Save the scene

Save the on-site Git Stash return to the on-site Git Stash popCopy the code