Recently, I transferred to a new department, and I am strict with the front-end Git operation. I have learned it by myself and understood the operation process of Git and some commands of Git.

1. What is Git?

Git is an open source distributed version control system designed to handle any project, small or large, with agility and efficiency. In general, the Git workflow is divided into the following steps:

  • Clone the Git resource as the working directory.
  • Adds or modifies files on cloned resources.
  • If someone else makes changes, you can update the resource.
  • Review the changes before committing.
  • Commit changes.
  • After the changes are made, if errors are found, the commit can be withdrawn and modified and committed again

Git has three states:

1. Committed: Data is safely saved in the local database.

2. Modifed: A file is modified but not saved to the database.

Staged: The current version of a modified document is marked so that it can be included in your next submitted snapshot.

Here’s a picture you’ve seen before:

Git also has some concepts for regions:

  • Workspace: Where you type code
  • Staging area: a temporary location that you want to save to the git repository and commit to the git repository with the entire staging area
  • Git repository directory

Git workflow:

Submission process:

1. Go to the directory and run git status to check the modified file.

2. Git add. You can then use git status to check the status of the commit, with blue indicating temporary.

There’s a file that comes with the MAC, we don’t need it, we need to ignore it.

Git reset HEAD —

Ok. This file has been ignored.

Then we start the commit. Git commit –m ‘new commit’

In the other case, too many code changes need to be made at one time and multiple commits need to be made. In this case, we need to add the original commit and cannot commit a new commit. As follows:

By commit — Amend can be added on the last commit. No-edit does not modify the last commit.

Applicable scenarios:

For example, when your code has been submitted to git library, the leader finds that there is something wrong with the JS file and asks you to modify it. There are usually two methods:

Method 1: the leader abandon all the code you commit, and then you go back to git reset… Go back to the version before you submitted your code, and then you modify the offending JS file, and git add xx.java xxx. Java -s -m “Porject: 1. Modify the bug…”

Finally, git push origin XXX

Method 2:

The leader does not abandon code, you go back, modify the problem JS file, after modification, git add the problem.js

Then git commit — amend — no-edit,

Git push origin HEAD:refs/for/ XXX

When we want to modify the last commit, we can use the git commit -amend command. Git commit – Amend can either modify the last commit or modify the commit instructions.

In the other case, when we commit –amend, if the last commit has been added to the code base, the commit has been closed, in the case of multiple development you may commit to someone else’s code. This is a troublesome problem. You need to undo the commit, then pull, and then recommit.

The basic operations are as follows:

Git reset –soft HEAD~1 // Rollback to the previous version

Git stash save ‘list

Git pull –rebase origin XXX //pull remote

Resolve the conflict

Git stash pop //

Git commit -m ‘new commit’ // recommit

The above is some processes, and other companies may not operate quite the same oh.