Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Git commands required for front-end engineers and their application scenarios

I want to push a local project folder (not an empty folder) to my Git repository

$ cd local-project path            Enter the local project folder
$ git clone <git-url> tmp          Clone the new git repository to the TMP directory of the local project folder
$ mv tmp/.git .                    Move the.git file to the root of your local project folder
$ git reset --hard HEAD            Keep workspace, temporary access, and HEAD consistent
Copy the code

Git reset –hard HEAD

Git reset HEAD aligns the staging area with the HEAD commit

Git reset — Hard HEAD aligns the workspace, staging area, and HEAD

I want to clone a Git project

  • Clone a project and its entire code history
$ git clone <url>
Copy the code
  • Clone a branch of a project
$ git clone -b <branchname>  <url>
Copy the code

Git repository changed its name. What should I do with my local pull code?

  • Change the local repository name to be the same as the code repository namemv local-git-name new-git-name
  • Connect the local warehouse to the remote warehouse
$ cd new-git-name
$ git remote set-url origin <git-url> Set the remote warehouse address
$ git remote -v # check the result
Copy the code

Every time I submit the code, I need to enter the user name and password

  • Changing the SSH Protocol
$ git remote rm origin
$ git remote add origin [email protected]:project/project.git
$ git push -u origin master
Copy the code

I want to create my own Feature branch based on the master branch. What should I do?

$ git branch View the current branch
$ git checkout master If it is not the master branch, use this command to switch. If it is the master branch, ignore this command
$ git pull origin master # pull the latest branch code for the remote master
$ git push origin HEAD:feature/your-feature-name Create remote feature branch based on master branch (current code
$ git checkout -b feature/your-feature-name origin/feature/your-feature-name Create a local feature branch and synchronize it with the remote branch
Copy the code

I want to submit my code

  • The workspace: workspace
  • Index /Stage: temporary storage area
  • -sheldon: Well, that’s a Repository.
  • Remote: Remote repository

Basic commands

$ git add .  Commit the modified file to the staging area
$ git commit -m"Submit content description"  Submit the staging area to the warehouse area
$ git push Commit code to a remote repository
Copy the code

Git add,The staging area

Add all files from the current directory to the staging area
$ git add .

Add the specified file to the staging area
$ git add [file1] [file2] ...

Add the specified directory to the staging area
$ git add [dir]

Confirm changes before adding
$ git add -p 

Delete the workspace file and place the deletion in the staging area
$ git rm [file] / [dir]

The deletion is only in the staging area but is retained locally
$ git rm --cahed [file] / [dir]

Modify the file name and submit it to the staging area
$ git mv [file-original-name] [file-renamed]
Copy the code

Git commit,Store block

Submit the staging area to the warehouse area
$git commit -m "commit description"

Submit the temporary storage area specified file to the warehouse area
$ git commit [file1] [file2] ...  -m "commit description"
All changes made to the commit workspace since the last commit go directly to the repository.
$ git commit -a
$ git commit -am"commit description" Add. && git commit -m" MSG"
View all diff information when submitting
$ git commit -v
What should I do if MY submission message (submission description message) is wrong?
$ git commit --amend -m"re commmit description" Overwrite the commit information from the last commit
Rework the last commit and include new changes to the specified file
git commit --amend [file1] [file2] ...
Copy the code

Common commands for branching operations

I want to see all my local branches

git branch

I want to see all the remote branches

git branch -r

I want to look at both the local branch and the remote branch

git branch -a

Create a new branch without switching

git branch [branch-name]

Create a new branch and switch to it

git checkout -b [branch-name]

Create a new branch and establish trace management with the specified remote branch

git branch --track [branch] [remote-branch]

Switch to the specified branch

git checkout [branch-name]

Switch to the previous branch

git checkout -

Between the existing branch and the specified remote branch

git branch --set-upstream [branch] [remote-branch]

I want to merge the specified branch into the current branch

git merge [branch]

Select a COMMIT to merge into the current branch

In this scenario, party A and Party B develop a module respectively. Party A develops a function but cannot merge it into master until the test is completed. In this case, Party B needs to use the code of Party A commit

git cherry-pick [commit]

Delete the branch

git branch -d [branch-name]

Deleting a Remote Branch

git push origin --delete [branch-name]

git branch -dr [remote/branch]