Collation of GIT operation commands under Linux system

Time:2021-5-31

1. Display the current configuration information

Copy code

The code is as follows:

git config –list

2. Create repo
Get it from somewhere else

Copy code

The code is as follows:

git clone git://git.kernel.org/pub/scm/git/git.git

Build your own

Copy code

The code is as follows:

mkdir test
cd test
git init

 
3. Display status

Copy code

The code is as follows:

git status

 
4. commit
Git add file. 1 file. 2 first add the file to the index. Only in this way can we know which files to commit.
perhaps

Copy code

The code is as follows:

git add -p

Used to interactively select which changes need to be committed

Copy code

The code is as follows:

git commit -m “log message”

 
perhaps

Copy code

The code is as follows:

git commit -a

Automatically check what files should be committed. If it is a new file, you still need to use git add to add it.
 
5. Display previous work

Copy code

The code is as follows:

git log

Output format

Copy code

The code is as follows:

git log
Copy code

The code is as follows:

git log -p

     Show patch

Copy code

The code is as follows:

git log –stat

  A summary of display changes

Copy code

The code is as follows:

git log –graph

  Only the current branch’s

Copy code

The code is as follows:

git log –graph –all

  Show all branches

Copy code

The code is as follows:

git log –graph –all –decorate

Show the name of the branch

Copy code

The code is as follows:

git log –pretty=oneline, short, full, fuller

The form of output is different

Copy code

The code is as follows:

git log –pretty=format:”%h – %an, %ar : %s”

Output in the specified format.
For other options of — pretty and the specific format, please refer to the part of preparation format in Git log — help.

Copy code

The code is as follows:

git log –follow file.c

This feature is very interesting, especially when file. C is moved.
Usually we move a file to a directory. If you do this, GIT log cannot display the records before the directory is moved.
Then add — follow.

Screening of GIT log
git log -2 -p    Display the log and diff of the last two commit
Git log — author = “author name” filter logs of specific authors
Git log — since = “2012-2-23” — before = “2012-2-24” screening period
Git log — grep = “key word” look up keywords in the message of commit
Git log branch — not master view the records on branch but not on master.

git log -S”func_ name”   Find the commit that a character appears or moves out. For example, you can find out when a function was added or deleted.

git show sha1    This SHA1 is the SHA1 of each commit, which displays the complete information of a commit, including diff
 
6. Revocation of changes

Copy code

The code is as follows:

git checkout — file.1

Undo this change to file. 1. Just undo the unstaged changes
The middle — indicates that this is a file, not a branch name

Copy code

The code is as follows:

git reset –hard HEAD

Undo all changes without commit, including those without stage and those without stage.
The result of this command is the same
git checkout HEAD file.1
Both staged and unstaged will be removed.

Sometimes we find that there is something wrong with making a commit before. We don’t want to make a commit, we want to get rid of it.
git revert HEAD       Automatically make a new commit and return the last commit.
git revert HEAD^      Automatically make a new commit and return the last and second commit.
7. Delete a commit

Copy code

The code is as follows:

git reset –hard HEAD~1

Removed the most recent commit

8. Modify the latest commit

Copy code

The code is as follows:

git commit –amend

 
7. Show changes

Copy code

The code is as follows:

git diff

Show all changes. There is no add to index.

Copy code

The code is as follows:

Git diff — staged or git diff — cached

Display the staged changes, that is, the contents of add, that is, the contents to be committed.

Copy code

The code is as follows:

git diff commit1 commit2

Show the change between the two commits, from commit1 to commit2.

Copy code

The code is as follows:

git diff commit1..commit2

Two points. The effect is the same as above

Copy code

The code is as follows:

git diff commit1…commit2

The three dots represent the changes that took place in the commit2 branch until the common father of commit1 and commit2.

Copy code

The code is as follows:

git blame -C file1.c

Show specific changes to the file…. Well, it seems to be used to find out whose fault it is?

Copy code

The code is as follows:

git blame -Ln,m file1.c

Check the changes between N and m lines.

Copy code

The code is as follows:

git blame commit1~1 -Ln,m file1.c

Check the changes before commit1 version. Trace the previous log.

Copy code

The code is as follows:

git blame commit1~1 -Ln,m — old/file.c

If the file has been renamed or moved, enter the name of the old file.
And it’s got to be added – it’s got to be.
 
8. Delete a file

Copy code

The code is as follows:

git rm file-name

Delete this file from the library and the current working directory

Copy code

The code is as follows:

git rm –cached file-name

Only delete from the library, keep the current local file.

9. Rename a file

Copy code

The code is as follows:

git mv file file-new

10. Apply patch

Copy code

The code is as follows:

git apply patch-file

Do this and apply the patch from the patch file. The effect is similar to the patch command.
But the advantage is that git apply is either successful or unsuccessful. If you don’t want to patch, there may be some patches, but some don’t.
After git apply, a commit is not generated automatically

git apply –check   It can be used to detect whether the patch will conflict or fail.

Copy code

The code is as follows:

git am patch-file

This is a command designed specifically for GIT. Patch file is generated by git format patch.
It contains author information and a brief description.
After git am, a commit will be generated automatically

Copy code

The code is as follows:

git am –resolved

There may be a conflict in Git am. If you encounter a conflict, you need to modify the code manually. After git add
Using git am — resolved

11. Delete some files without git add;

Copy code

The code is as follows:

git clean

parameter
-N displays the files and directories to be deleted
-F delete file
-DF delete files and directories

Copy code

The code is as follows:

git clean -df alps/

git remote

Git is a distributed code management tool, so it can support multiple warehouses. In git, the warehouse on the server is locally called remote.
Directly clone a warehouse:

Copy code

The code is as follows:

$: git clone [email protected]:projects/search.git

Another clone mode:

Copy code

The code is as follows:

#Create directory and initialize local warehouse
$: mkdir search && cd search
$: git init
#Add remote warehouse Path
$: git remote add github [email protected]:yyfrankyy/search.git
#In fact, pull is a fetch + merge
$: git pull github –all –tags

Move the working directory to GitHub

Copy code

The code is as follows:

$: git remote add github [email protected]:yyfrankyy/search.git
$: git push github –all –tags

Show all remote warehouses

Copy code

The code is as follows:

$: git remote -v
origin [email protected]:projects/search.git (fetch)
origin [email protected]:projects/search.git (push)
github [email protected]:yyfrankyy/search.git (fetch)
github [email protected]:yyfrankyy/search.git (push)

Rename remote warehouse

Copy code

The code is as follows:

$: git remote rename github gh
$: git remote
origin
gh

Delete remote warehouse

Copy code

The code is as follows:

$: git remote rm github
$: git remote
origin

Capture data from remote warehouse and update local warehouse:

Copy code

The code is as follows:

$: git fetch origin

remote: Counting objects: 58, done.
remote: Compressing objects: 100% (41/41), done.
remote: Total 44 (delta 24), reused 1 (delta 0)
Unpacking objects: 100% (44/44), done.
From git://search.ued.taobao.net:projects/search.git
 * [new branch]      product     -> origin/product
View the remote warehouse information, which can be used to track other people’s push:

Copy code

The code is as follows:

$: git remote show origin

* remote origin
  Fetch URL: [email protected]:projects/search.git
  Push  URL: [email protected]:projects/search.git
  HEAD branch: master
  Remote branches:
    master  tracked
    p4popt  tracked
    prepub  tracked
    product tracked
  Local branches configured for ‘git pull’:
    master  merges with remote master
    p4popt  merges with remote p4popt
    prepub  merges with remote prepub
    product merges with remote product
  Local refs configured for ‘git push’:
    master  pushes to master  (up to date)
    p4popt  pushes to p4popt  (up to date)
    prepub  pushes to prepub  (up to date)
    product pushes to product (up to date)