This is the 7th day of my participation in the August More Text Challenge

preface

At work, our code management is done by Git, and the use of Git can also be said to be a necessary skill for our developers. Here are 50 git commands that I think are commonly used.

How do I view your Git configuration

The following command returns your Git configuration information, such as your username and email address:

$ git config -l
Copy the code

How to set your Git username

Configure your username by using the following command.

$ git config --global user.name "Jason"
Copy the code

Cache login credentials

You can store the login credentials in the cache and don’t have to enter them every time.

$ git config --global credential.helper cache
Copy the code

Clone the remote branch repository

If you do not specify a branch when Git clones a remote repository project, only the contents of the default branch are cloned.

$git clone -bCopy the code

Initialize the Git repo

The project root initializes a new Git repository.

$ git init
Copy the code

Adds a single file to the staging area

Add a single file to the staging area folder name/filename and use git status to see where the changed file is located.

$ git add <path/to/file>
Copy the code

View files in the staging area

$ git ls-files
Copy the code

Add all the files that have changed to the staging area

With the. Wildcard, you can add all files that have changed to the staging area with one click.

$ git add .
Copy the code

Adds all modified files under a folder to the staging area

The following command uses the * sign to move all the modified files in the ‘fil’ folder to the staging area.

$ git add fil*

# or

$ git add fil/*
Copy the code

Check Git repository status

This command is used to see if files have been modified again since your last submission, or to display files that have been added to the staging area.

$ git status
Copy the code

Adds staging area contents to the local repository

Git commit commits the staging area to a local repository. The ‘message’ can be some remarks.

$git commit -m 'message'Copy the code

Skip the staging area and commit the changes

-a Parameter Settings After modifying the file, you do not need to run git add to directly submit the file.

$ git commit -a -m
Copy the code

Checking Git Logs

This command displays the commit history of the current repository:

$ git log
Copy the code

View historical commits, including Git changes

Using this command, you can view historical submission records and the files corresponding to each submission record

$ git log -p
Copy the code

View historical operation records

The following command can view all action records for all branches (including deleted commit records and reset actions)

$ git reflog
Copy the code

View specified commits

View all changes to the specified COMMIT hashID

$git show commit - id # $2998 f8a6e19011d9f62xx1ce1f126e8d326ffac9 git show for exampleCopy the code

View log statistics in Git

This command displays a history of the commit, including the file to which the changes were committed and how many lines were added or removed from the file

$ git log --stat
Copy the code

‘diff’ views the changes made before the change

Git diff by default displays all changes that have not been temporarily saved since they were made.

Passage Passage Compare staged staging files to the last COMMIT

$ git diff
$ git diff App.js
$ git diff --staged
Copy the code

Delete trace files from Git’s current working tree:

Run git rm to delete files from the repository:

$ git rm filename
Copy the code

Note: After deleting files, git commit is required to delete all files in the repository.

Ignore files in Git

Create a.gitignore file for the project’s further directory and commit it.

Restores a modified file

Restore files in the working directory that have not been cached (git add has not been executed) and have not been committed (Git commit has not been executed)

$git checkout $git checkoutCopy the code

Version back

Rollback to previous version

$git reset --hard HEAD^ #Copy the code

To rollback to a certain version, you need to know the commit-id

$ git reset --hard commit-id
Copy the code

Also, to undo all temporary files:

$ git reset .
Copy the code

Git reset

Rolling back a version will restore the staging area, where Git Add temporarily commits, to its unstaging state, as well as locally committed content. The unpending state marks these files for submission

$ git reset(--mixed) HEAD~1
Copy the code

Rollback a version, without emptying the staging area, restores committed content to the staging area, without affecting the original local files (uncommitted files are also not affected) these files are still in the staging area

$ git reset --soft HEAD~1
Copy the code

Rollback a version, clear the staging area, restore the version of the submitted content to the local, local files will be replaced with the restored version

$ git reset --hard HEAD~1
Copy the code

Git reset Resets to a version. The next git submission deletes the version later than the previous version.

Cancel the submission

# create a new commit that undoes the most recent commit: $git Revert HEAD Create a new commit $git REVERT COMMIT-id that undoes the specified commit versionCopy the code

Create a new branch

By default, you have one branch, the main branch. Using this command, you can create a new branch. Git doesn’t switch to it automatically; you’ll need to do it manually using the next command.

$ git branch branch_name
Copy the code

Switch branch

$ git checkout branch_name
Copy the code

View all branches

You can use Git Branch to see the status of all branches that have been created, and the current branch is marked with an asterisk and highlighted in green.

$ git branch
Copy the code

Create and switch to the newly created branch

$ git checkout -b branch_name
Copy the code

Delete the branch

When you have finished working on a branch and merged it, you can remove it using the following command:

$ git branch -d branch_name
Copy the code

Merging branches

To merge a specified branch into the current branch, use the following command

$ git merge branch_name
Copy the code

Use git rebase

Git merge and Git rebase both merge the contents of the two branches and the end result is no different, but rebasing makes the commit history cleaner.

$ git rebase branch_name_here
Copy the code

You can see rebase here

The commit log is displayed as a chart

–graph gets the commit log as a graph, and –oneline limits the commit message to oneline.

$ git log --graph --oneline
Copy the code

End conflict merge

To abort the merge and start again, you can run the following command:

$ git merge --abort
Copy the code

Adding a remote repository

$ git add remote https://repo_here
Copy the code

View remote warehouse address

$ git remote -v
Copy the code

View more information about remote repositories

$ git remote show origin
Copy the code

Push to remote repository

$ git push
Copy the code

Push to the specified remote repository

$ git push origin
Copy the code

Push to the specified branch

$git psuh origin test:masterCopy the code

Pull remote branch changes

We need git pull before each Git push to keep the remote branch and local code up to date and to avoid conflicts

$ git pull
Copy the code

View the remote branch that you are tracking

$ git branch -r
Copy the code

Of the remote repository

This command downloads the changes from the remote repo, but does not merge on the local branch (Git pull does merge)

$ git fetch
Copy the code

Contrast the git pull

Git Fetch compares to Git pull, which is equivalent to fetching the latest version locally from a remote location, but does not merge automatically. If you need a selective merge Git fetch, it’s a better choice. Git pull is faster with the same effect.

Specifies to view remote branch commit logs

$ git log origin/master
Copy the code

Merge remote and local repositories

$ git merge origin/master
Copy the code

Push the new branch to the remote repository

Git push origin master git push origin master

$ git push -u origin branch_name
Copy the code

Deleting a Remote Branch

When you don’t need a remote branch:

$ git push --delete origin branch_name_here
Copy the code

Forced to push

This command is used to push your own local repository code directly to the repository, based on your submission, the previous submission of others will be overwritten.

$ git push -f
Copy the code

Add cache changes

This command is commonly used in daily code submission, the local branch is not clean (there are file modifications), such as switching branches, will prompt the local branch has changed cannot switch, etc.

$git stash saveCopy the code

Viewing the cache list

You can use the following command to see if the added cache was saved successfully

$ git stash list
Copy the code

Application of storage

Once the cache is done, we can switch to another branch, and when the other branch is done, we can go back to the previous branch. You can then use the following command to restore the previously stored code.

$git stash pop # Restore all stash code to the workspace with $git stash apply. Stash_id is $git stash pop stash@{1}Copy the code

Delete the storage

$git stash drop [stash name] $git stash clearCopy the code

Type version number (TAG)

The project on-line needs to type the version number, and help the operation and maintenance side, this process is indispensable. The code needs to be pulled and pushed to the remote repository before it can be tagged.

$git tag 'version number: v1.0.1' # Set the tag to a remote $git push origin' branch 'Copy the code

Delete files in the remote repository

You accidentally uploaded some files to a remote repository and want to delete them:

$git rm --cached filename $git rm --cached filenameCopy the code

The last

After the introduction of some basic common commands, you need to solve problems according to the problem. Occasionally I use graphical tools to make it easier to see, and I use Sourcetree tools to make it easier to use.