Recently, the company can only commit code in Linux environment, so I had to learn about git command and git again. Related content can be roughly divided into three parts: understanding Git — some concepts of Git; Git — some commands for git; Learn about Git — some of the basics of Git.

Know the git

What is Git?

Git is a distributed version control system. “Distributed” means that each developer stores a local version repository (.git directory) from which they can interact with remote server repositories. Distributed corresponds to a centralized version control system, such as the SVN. Centralized means that all version records are stored on a central server, and the client performs version control locally on the central server.



Git pros and cons:

The advantages of Git (distributed version control system) : everyone can version control independently, independent of the remote server; Even if the server goes down, everyone has all their history locally.

Git’s disadvantages:Almost noExcept that the cost of learning is higher.

Advantages of the SVN (centralized version control system) : Centralized management. You can view the operations of each member, and administrators can easily control the rights of each developer.

Disadvantages of SVN: the developer loses native version control when the server goes down, and loses all historical version records if the server is damaged.



What concepts must you know before learning git commands?Git has several regions, from right to left:

-Jenny: Well, I’m working in a workspace.

Index/Stage: temporary storage area

Repository: a Repository or local Repository

Remote: Remote repository



A project managed by Git has a.git folder, a workspace is all the directories of the project except the.git folder, a staging area and a repository are stored in the.git folder, and a remote repository is a remote server repository.

To fit the git

Because Git has a local repository for each repository, collaboration is the interaction between local and remote repositories, so git commands can be roughly divided into two chunks, local and remote

The local operating

Common command to submit code

git status; View workspace file status

As shown, the workspace file has three states:

1.Changes to be committed file added to staging not committed to repository

The file has not been added to staged storage

3. Untracked files are not tracked, which is not managed by Git. They are typically new files



git add; Add files from your workspace to staging (git add. Or git add file path)



Git commit -m; Add files from staging to the repository



Git commit -a -m; A combination of the first 2 commands (cannot be files that have not yet been traced)



Note: a unique commit hash is generated after each commit, corresponding to the version of the commit, as shown below:

In addition to these common commit commands, there are other operations that are sometimes required…



Branch of two.

Many times you need to use branches, which can be independent of each other.

Branches are the final locations where versions are stored.

A branch is a timeline. Each Git commit forms a version, which is stored in turn at each commit point of the branch.

The repository has one and only one master branch by default.

To create a new branch, first create a pointer that points to the same commit point as the current branch. The new branch contains the commit point from the first commit point to the commit point that the branch pointer points to.

There’s a special pointer in Git called HEAD that points to the local branch you’re working on. Think of HEAD as an alias for the current branch.



Create a new dev branch on the master branch as shown below:

git branch; View the branch of the current repository, where * is the current branch

Git branch Specifies the branch name; Create a new branch

Git Checkout branch name; Switch branches (potholes: Changes to this branch may be carried to the switched branch, resulting in branch contamination)

Git checkout -b; New and switch to this branch

Git branch -d Specifies the branch name; Delete the branch

Git branch -d Specifies the branch name; Force branch deletion (staging area not empty or merged)

Git merge branch name; Merge branches into the current branch



There are two cases of merging branches:

1, Fast merge fast-forward, no conflict

2. Three-party merger may cause conflicts

Resolve the conflict: Retain the corresponding content and delete <<<< ==== >>>>, and add it again

3. Store

As mentioned above, there are two ways to avoid this pitfall when switching branches: commit the current change, and stash: Store uncommitted changes on a stack and then reapply those changes at any time.

git stash; Save changes to a stack

git stash list; Check the storage

git stash apply; Apply the nearest storage

Git Stash drop Storage name; Remove the storage

git stash pop; The combination of the first two commands applies and removes the nearest storage



4. Cancel

Many times you need to undo the changes or roll back the version.

Git Checkout; Undo workspace changes

Git reset; Withdraw the file from the staging area

git commit –amend; Modify the last commit (recommit the file in the staging area and overwrite the last commit information)



Reset Trilogy

Suppose the current submission looks like this:

Git reset –soft HEAD~ number; Git commit rollback only back to the HEAD.

As shown in figure:

Git reset –mixed HEAD~ number; Back up the HEAD pointing and staging area

As shown in figure:

Git reset –hard HEAD~ number; Rollback HEAD, staging area, and working directory. (Use with caution, it overwrites the working directory)

As shown in figure:

Git branch Specifies the branch name. Git branch specifies the branch name. Git branch specifies the branch name. Create a new branch and point to the corresponding commit object. Git tag git tag git tag git tag git tag git tag git tag Git git git git git git git git git git git git git git Create a tag git show. Git tag -d specifies the tag name. Git checkout git checkout Detached HEAD check out the version of the file to which the tag points (this time in the detached HEAD pointer, you must create a new branch to modify the contents). Git config – — list; Git init; Git diff; Git diff file name; Git diff – cached; Git show commit hashes Git log; Git log –online; Git reflog; Git log –oneline –decorate –graph –all; Git config –global alias New command old command; Git config –global alias.lol “log –oneline –decorate –graph –all”

Remote operation

Collaborative development with multiple people often requires pushing or retrieving code between remote repositories

Git remote add origin Associated remote repository (Origin is the alias or host name of a remote repository. Git clone Git remote -v; Git push origin git push origin git push origin Git push -u origin Specifies the name of the local branch that you want to push to the remote branch. Git push git push git push git push git push git push Git fetch Origin; Clone or fetch does not generate a local branch with the same name as the remote branch. Instead, it generates a remote trace branch named (remote)/(branch) with references to the remote branch. The remote trace branch cannot be modified. Git checkout -b specifies the name of the remote trace branch. Git checkout -b specifies the name of the remote trace branch. Create a local branch and trace the remote trace branch (or merge the remote trace branch directly into the current branch). Git pull origin git pull origin git pull origin Git push origin –delete remote branch name; Git branch -u Git branch -a; git branch -a; Git branch -vv; View all trace branches set up

Understand the git

Unfinished… Refer to the link: www.bilibili.com/video/BV15J… www.bilibili.com/video/BV1Mf… www.ruanyifeng.com/blog/2014/0…