preface

In my work, I basically use Git for program version management and multi-person cooperative development. However, we often don’t know why we use Git, and we don’t understand the various Git commands, and we often forget about them after a period of time. Even if we remember them, we are still empty, and we worry about the failure of the code base. The root cause of all this is that we don’t have a clear understanding of Git. Although we can use git version management tools, it is much easier to use them under the premise of mastering command line. Compared with the previous centralized storage SVN, Git adopts the distributed storage, which brings the benefit that all versions of the program are stored on each distributed machine, and the program can continue to be written even when the network is disconnected.

Git timelines work with multiple people

Git strings each commit into a timeline, which is a branch. Git, as a source code management system, inevitably involves the collaboration of many people. “Collaboration must have a standardized workflow that allows people to work together effectively to keep the project in order.” Workflow workflow workflow workflow workflow workflow workflow workflow workflow workflow workflow workflow workflow

Understanding and initializing Git project libraries

Git project repository These are folders managed by Git. First of all, Git is a program version management tool. The specific instance of a program is a folder, so Git can manage the contents of the folder. Git initializes a project library. This is called Git init.

  • git init

When you create a new Git project locally, or if you want to git a folder, you first need to initialize the project with the command git init. After initializing a Git project, there will be a hidden file inside the folder. This is the repository of the project, where information such as each project commit is recorded.

What makes up a Git project library

The Git project repository consists of two parts:The workspaceandrepository.As the figure above shows, git project libraries are divided intoThe workspaceandrepositoryTwo parts,The workspaceThat’s our original folder, andrepositoryRefers to thegit initAdded after the folder is initialized.gitFile. Pay attention to,.gitThe file is hidden from us. Git repositories contain many things, but one of the most important is calledstage(orindex), andThe local branchGit automatically creates the first branch for usmaster) and a pointer to the current local branch calledHEAD. Git maintains the same branch of code in the local project library as in the remote library.git commitThe command actually commits the code to the locally maintained branch of the code and then synchronizes the local branch to the remote library branch.

repository.gitFolder Content snapshot

Workspace and staging area

Git add

is used to add changes to the stage/index, and then commit all changes to the local branch using git commit -m

. Finally, git push origin-u < branch name of remote library > pushes the local branch to the remote library. From the three steps above, you can see that some new concepts have emerged: stage/index, workspace, and local branch. Let’s take a look at these three concepts.

  • Working Directory

Is a directory you can see on your computer and where you can program directly from your IDE. For example, my test folder is a workspace:Notice in the figure above that there is a hidden folder.gitThis is not a workspace, but a Git repository.

  • Repository (Repository)

As mentioned above, hidden folders in the workspace.gitNot a workspace, but a repository for Git. Git repositories contain many things, but one of the most important is calledstage(orindex), andThe local branchGit automatically creates the first branch for usmaster) and a pointer to the current local branch calledHEAD.

Summary of git common commands

Adding a remote library

Once you’ve created a local project, you want to connect it to a remote library so that it can be submitted to the code hosting site every time it’s written, safe and structurally clear. Where origin refers to the name of the remote library, generally default to Origin, can also be changed. This is not recommended, because writing code is a collaborative work with people, don’t make it difficult for others to read and understand for the sake of individuality.

Submit code
  • Commit to the staging areagit add .orGit add < name >
  • Commit to local branchGit commit -m '< commit note >'
  • Push to the remote libraryGit push -u origin

That’s all it takes to commit code, but before we go into specifics, first we need to create a visual model of Git.As the figure above shows, git project libraries are divided intoThe workspaceandrepositoryTwo parts,The workspaceThat’s our original folder, andrepositoryRefers to thegit initAdded after the folder is initialized.gitFile. Pay attention to,.gitThe file is hidden from us. inrepository, also mainly divided into two parts:The staging area (stage)andThe local branch. Git maintains the same branch of code in the local project library as in the remote library.git commitThe command actually commits the code to the locally maintained branch of the code and then synchronizes the local branch to the remote library branch.

The concept of a staging area, I understand, acts as a buffer, creating an opportunity for regret and second thoughts. Liao Xuefeng’s Git tutorial is recommended, which is simple and clear.

View the current project status

Git status tells you if your temporary store is clean and if your local code needs to be pushed to a remote repository.

Clone remote warehouse project to local

When we want to download the project code in the remote library, there is no one-click download button, but it is easy to get the address of the project remote library. Git clone: git clone: git clone: git clone: git clone The first thing to note for beginners is that before cloning a project, use the command line to enter the location of the project you want to download. When using Git Clone, you will not know where the cloned project is, adding frustration. Git clone -b [branch name] [remote branch address] Git clone -b [branch name] [remote branch address] In order to switch the code. But people are lazy, is there a command to specify the branch of the cloned code? The answer is yes. Git clone -b = git clone -b

Pull remote repository update

Git pull When someone commits code to the main branch and your code is still old, the remote repository’s main branch is one version ahead of your local branch. Programming is a collaborative effort, and you need to write your own modules on top of new project code. This is where you need to git pull the latest code from the remote library. Assuming you don’t, when you git commit your code, you’ll get an error saying your code is in conflict with the remote library. Therefore, it is a good practice to git pull your code before committing it.

See the branch

Having said that we have all branches of code locally, sometimes we need to know where we are so we don’t have to write the wrong branch.

  • Viewing a Local Branchgit branch
  • Viewing remote branchesgit branch -r
  • View all branchesgit branch -a

Create a new branch and switch

Git checkout -b < branch name > < branch name >

Switch branch

Git checkout < branch name >

Merging branches

Git checkout < merge branch >First switch to the main branch where you want to merge the other branchesGit merge --no-ff < branch name >Create a new node based on the current main branch and merge the other branches. When we have finished developing our own development branch, administrators need to merge the developer’s code into the main branch. This process is called merging branches, and the main process is divided into two steps. The first step is to switch to your main branch (e.gmaster) and thengit mergeThe branches you want to merge will do. But don’t relax, there are frequent errors, merge failures, code conflicts. This means that two branches make different changes to the code in the same place. Presumably, at some point, your different choices created two very different parallel universes. At this time, there is only one solution, find the conflict place, manually modify.

Code the rollback

This is another important skill in case you make a major error with the version, or can’t find the source of the error. You need to time travel and reset the code to the previous version. Before we do that, we need to make it clear that every commit, Git strings them together into a timeline, which is a branch. As of now, there is only one timeline, and in Git this branch is called the main branch, i.emasterBranch. In the repository, there is aHEADA pointer,HEADNot technically to commit, but to pointmaster.masterIt points to submission, so,HEADIt points to the current branch. In the beginning,masterA branch is a line that Git usesmasterPoint to the latest submission, then useHEADPoint to themaster, you can determine the current branch and the commit point for the current branch.When we create a new branch, for exampledevGit creates a pointer calleddevTo point tomasterSame submission, againHEADPoint to thedev, which means the current branch isdevOn:With that out of the way, the next step is code rollback.Git log -[number]Each commit has an automatically generated COMMIT identifier, which is needed to roll back to the specific version. The numbers in the above command represent the most recent commit logs.Git reset --hard [commit sequence]This command can be used to roll back to the specified version.git reset --hard HEAD^This command rolls back to the previous version.

Submitted to offset

In my recent work, I encountered a situation where, after committing a code, I found that there was something wrong with the committed code and had to undo the commit. If you manually change back, first of all a little stupid, and then there will inevitably be missing places. You can use Git Revert to undo an operation. All commit and history before and after the operation are retained, and the undo is treated as the latest commit.

Git revert <commit> git revert <commit> Fa042ce57ebbe5bb9c8db709f719cec2c58ee7ff) revoked the specified version, cancellation will be submitted as a to save. Git push origin < branch name >Copy the code

Git Revert vs. Git Reset

  1. Git Revert rolls back the previous commit with a new commit, and Git Reset deletes the specified commit directly.
  2. In the case of the rollback, the effect is similar. However, there is a difference when you continue to merge older versions of the previous version in the future. Git Revert “neutralizes” the previous commit with a reverse commit, so the change does not occur again if the old branch is merged later, but git Reset removes a commit from that branch. Therefore, when merging with the old branch again, the rolled back COMMIT should be introduced.
  3. Git Reset moves the HEAD back a bit, while Git Revert moves the HEAD forward, but the new commit is the reverse of what is being reverted.

Refer to the article

  1. Git tutorial – Liao Xuefeng
  2. List of common Git commands
  3. Use the git revert