There are many Git commands, but this article has documented only a few that I have used frequently in my actual development and their usage scenarios.

Suppose there is a new project. SSH address: git://github.com/username/project_example.git

Copy the project

1. cloneproject
$ git clone git://github.com/username/project_example.git
Copy the code

The project_Example folder appears in the local directory.

$ cd project_example
Copy the code

Run the CD command to go to this folder. You can install dependencies and run projects.

2. Create a local repository.git remote addThe command

$ git init project_example
Copy the code

Or you can run git init in a project_Example folder that already exists locally

Pay attention to: The local folder project_Example can have the same name as the repository, but it is best to keep it the same.

$ cd project_example
$ git init
Copy the code

There is a local repository, project_Example

Next, execute the git remote add command

$ git remote add origin git://github.com/username/project_example.git
Copy the code

This command associates the local repository with the remote repository.

However, the directory is still empty. You need to perform the following operations

$ git fetch --all && git reset --hard origin/masterCopy the code

These two commands are executed in sequence, forcing the local repository to be overwritten.

Remote code forces overwriting local code

Git fetch –all and Git reset –hard Origin /master

The git fetch –all command is used to fetch the latest version from remote to local.

Git reset –hard origin/master to rollback the local code to the remote version, you will see a prompt like HEAD is now at

Indicates that the HEAD has reached the latest commit of the remote.

Git fetch –all and Git reset –hard Origin /master are often used together. In addition to cloning a remote project to a local location, git fetch –all and Git Reset –hard Origin /master are often used together. Use these two commands to fully copy the remote code to resolve local conflicts.

Three, branch

Three branches are usually required in real development

  • Development environment:dev
  • Test environment:test
  • Production environment: defaultmasterAs a production environment
1. Create a branch

The master branch is the default branch provided by the Git version management tool. To create a local branch, use the Git branch

command

$ git branch dev
Copy the code

The dev branch is successfully created. Git branch –all

  dev
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
Copy the code

You can see that the dev branch has already appeared locally, with the asterisk * pointing to the current master branch.

2. Switch branches

Git checkout

command to switch branches

$ git checkout dev
Copy the code

The dev branch has been created.

Git branch –all command to check

* dev
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
Copy the code

Currently in dev branch.

You can also create and switch to a new branch using the git checkout -b

command.

$ git checkout -b dev
Copy the code
3. Submit the branch to the remote

The git push origin

command is used to commit a branch to a remote location.

git push origin dev
Copy the code

The dev branch is created locally, but not remotely, so this command pushes the dev branch to remote. The process for creating and committing the Test branch is the same as the dev branch.

Iv. Submit and merge

1. Submit

We are developing in the dev branch, and the usual commit steps are:

$ git pull
$ git add .
$ git commit -m "commit message"
$ git push
Copy the code
  • Be sure to firstgit pullOtherwise, the local code will conflict.
  • git add .Adds a file to the staging area.
  • git commit - Adds the staging area contents to the repository.
  • git pushUpload remote code and merge.
2. Merger

Need to merge dev branch code into Test branch

The git merge

command is used to merge the current branch and simply move the pointer forward in fast-forward mode

$ git checkout test
$ git pull
$ git merge dev
Copy the code
  • If the currentdevBranch, you need to switch totestbranch
  • git pullOrder first is to consider the multiplayer development scenario, there is a possibility that someone else has submittedtestBranch code.
  • git merge devCommand willtestBranches andtestBranches merge, pushing the pointer forwardfast-forward

Note: If the merged file is merged into one, the merged file will be merged into the unmerged state. If the merged file is merged into the unmerged state, use git status command to view the merged file, and then manually resolve the merged file. If the merged file is merged into the unmerged state, use git add to resolve the merged file.

Four, git cherry – pick

Finally, I’ll introduce the Git cherry-pick command, which has been used recently.

The git cherry-pick

command merges the specified commit (commitHash) into the current branch.

Such a merge merges only code from one commit into the current branch, and does not merge other commits.

The scene I encountered went something like this:

  • devBranches include all the features of the project, such asModule A,Module BAnd the two of themThe moduleThe other content is calledCommon content.
  • Now the production environment exceptmasterBranch (includingModule AandModule B), andmasterABranch (includes onlyModule A) andmasterBBranch (includes onlyModule B).
  • Now the demand is to change partCommon content, and submit the changes intomasterABranch.

It goes something like this:

Let’s assume that

is b1359ab1.

$ git checkout masterA
$ git cherry-pick b1359ab1
Copy the code
  • To switch to the firstmasterAbranch
  • performcherry-pickThe command

This only merges the b1359AB1 commit from dev branch into masterA branch.