Detailed explanation of all common git commands

Detailed explanation of all common git commands

[[142349]]

Preface

After studying Git for a while, I feel that the operations are git commit, git pull, git push, git add, git submodule, git stash, git branch, git checkout, git merge, etc. The following summary is classified and relatively clear.
create

Clone an already created repository:

$ git clone ssh://[email protected]/blog.git

Create a new local repository:

$ git init

Local Modifications

Display the modified files in the working directory:

$ git status

Show the differences between the last submitted version of the file:

$ git diff

Add all current changes to the next commit:

$ git add .

Add changes to a file to the next commit:

$ git add -p <file>

Commit all local changes:

$ git commit -a

Commit previously marked changes:

$ git commit

Additional message submission:

$ git commit -m 'message here'

Submit and set the commit time to a previous date:

git commit --date="`date --date='n day ago'`" -am "Commit Message"

Modify last commit: Do not modify published commits!

$ git commit --amend

Move uncommitted changes in the current branch to another branch

git stash
git checkout branch2
git stash pop

search

Search for text in all files in the current directory:

$ git grep "Hello"

To search for text within a revision:

$ git grep "Hello" v2.5

Submission History

Starting from commit ***, display all commit records (show hash, author information, commit title and time):

$ git log

Show all commits (show only the commit's hash and message) :

$ git log --oneline

Show all commits for a user:

$ git log --author="username"

Show all changes to a file:

$ git log -p <file>

Who modified what in the file, when, and how?

$ git blame <file>

Branches and Tags

List all branches:

$ git branch

Switch branches:

$ git checkout <branch>

Create and switch to a new branch:

$ git checkout -b <branch>

Create a new branch based on the current branch:

$ git branch <new-branch>

Create a new traceable branch based on the remote branch:

$ git branch --track <new-branch> <remote-branch>

Delete the local branch:

$ git branch -d <branch>

Tag the current version:

$ git tag <tag-name>

Updates and Releases

List currently configured remotes:

$ git remote -v

Display remote information:

$ git remote show <remote>

Add a new remote:

$ git remote add <remote> <url>

Download the remote version but do not merge it into HEAD:

$ git fetch <remote>

Download the remote version and automatically merge it with the HEAD version:

$ git remote pull <remote> <url>

Merge the remote version into the local version:

$ git pull origin master

Publish the local version to the remote end :

$ git push remote <remote> <branch>

Delete the remote branch:

$ git push <remote> :<branch> (since Git v1.5.0)
or
git push <remote> --delete <branch> (since Git v1.7.0)

Release Tags:

$ git push --tags

Merge and reset

Merge the branch into the current HEAD:

$ git merge <branch>

Reset the current HEAD revision into the branch: Do not reset published commits!

$ git rebase <branch>

Exit Reset:

$ git rebase --abort

Continue with the reset after resolving the conflict:

$ git rebase --continue

Use the configured merge tool to resolve conflicts:

$ git mergetool

After manually resolving conflicts in the editor, mark the file as resolved

$ git add <resolved-file>
$ git rm <resolved-file>

Revocation

Abandon all changes in the working directory:

$ git reset --hard HEAD

Remove all files from the stage (ie undo the last git add):

$ git reset HEAD

To discard all local changes to a file:

$ git checkout HEAD <file>

Reset a commit (by creating a new commit that is distinct from the previous one)

$ git revert <commit>

Reset HEAD to the specified revision and discard all changes after that revision:

$ git reset --hard <commit>

Reset HEAD to the last committed version and mark subsequent changes as modifications not added to the stage:

$ git reset <commit>

Reset HEAD to the last committed revision, keeping uncommitted local modifications:

$ git reset --keep <commit>

Use of git submodule

During the development process, there are often some common parts that you want to extract and make into a public library to provide to other projects for use. In this way, the git submodule command of git is used.
Add to

To add a submodule to the current project, use the following command:

git submodule add warehouse address path

For example:

git submodule add helloworld.git
git commit -m "Add submodules helloworld.git"

Others collaborate

git clone /path/to/repos/helloworld_parent.git
git submodule init
git submodule update

Remove

1. Delete git cache and physical folders

2. Delete the contents of .gitmodules (or the entire file) Since this example only has two submodules, delete the file directly

3. Delete the submodule configuration source file of .git/config

4. Commit changes

<<:  Programmers: How to protect your eyes

>>:  How to survive programming 80+ hours a week?

Recommend

Nokia X2 hands-on: It's the same level as a thousand-yuan phone

With the official release of Nokia X2, what change...

Twitter may be dying, and it's dying because of a problem we're all facing

Twitter is one of the largest Internet companies ...

Tik Tok promotion and operation | Practical skills of Tik Tok short videos!

With the rise of short videos such as Douyin, sho...

Xcode 7: Test your app on a real device without spending $99

In Xcode 7, Apple changed its licensing policy. P...

How can UGC products achieve operational differentiation?

In recent years, more and more product forms have...

Form costs reduced by 30%, home decoration advertising case!

With the further deepening of regulation and cont...

E-commerce platforms create their own private domain traffic?

Private domain traffic is not a new term, nor did...