git notes

clone a remote repository

  • sshkey-gen -t rsa -C "email-addr", generate public and private keys in ~/.ssh/
  • copy public keys to github->settings->sshkey
  • git clone XXX , clone repository to your local machine.

create and push your local branch to remote

  • after you clone, enter the project and see which branch you are in, use git branch
  • to create a local branch and develop in the local branch, git checkout -b localbranch, this command will create a local branch named localbranch as switch to it. git branch you will see you are in localbranch now.
  • git branch to see all the branches, git branch -a or git branch -r will show all the remote branches.
  • to trace the remote branch, git branch --set-upstream-to=origin/oracle, this will create relations between localbranch and origin/oracle, and git status will show the trace.
  • now, if you git add and git command, and git push, you will push the changes to the remote branch origin/oracle.
  • git pull to fetch remote branch

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

git pull origin/oracle to pull remote branch oracle.


  1. Git log – Displays all commit logs for the current version. If you cannot view the log information before the rollback, pass the git log commandgit reflogView all commit ids
  2. The fallback versiongit reset --hard HEAD^Git commit, HEAD refers to the current version, that is, after every git commit, a new version will be generated, and HEAD refers to the new version.HEAD^Represents the previous version,HEAD^^Is used to indicate that the current version is two versions backHEAD~2It’s a little bit more concise.
  3. Git init creates a.git hidden folder. This is the staging area of Git, and outside is the workspace where you edit files. Git add adds changes to the staging area. Each new version is dependent on the previous version, and each change is recorded. Git commit is to commit everything to the staging area, create a version record, and point the HEAD to the new version, the current branch.
  4. Git add add, git commit, git checkout cancel, git rm delete
  5. Git reset unsets the staging, and git checkout unsets the changes
  6. Git diff HEAD — code.txt This command compares code. TXT in the current branch HEAD with the current modified code.txt.
  7. Git config –global
git config --global user.name ""
git config --global user.email ""
git config --list # show all the configs
Copy the code

Save the user name and password, which can be used when using HTTP.

git help credentials # show help info
git config credential.helper store # store password next time you pull
Copy the code

Viewing remote branches

git branch -a
git branch -r
Copy the code

You can view remote branches

git remote show origin
Copy the code

You can view remote addresses, remote branches, and the relationship between local branches and other information

git remote prune origin
Copy the code

You can delete branches that do not exist remotely (local branches still exist)

Modify comments

git log
Copy the code

View the submitted log information

To modify the submitted logs, run the

git commit --amend
Copy the code

Use vim to modify the logs and git log to view the modified logs

Tags operate

add tags

Git tag v1.0 # add light weight tag git tag -a v1.0 -m "release 1.0" # add tag with commentCopy the code

show tags

Git show v1.0 git tag -lCopy the code

share tag – push to remote repository

Git push Origin v1.0 # Push one tag to remote Git push Origin --tags # push multi tagsCopy the code

delete tag

Git tag -d v1.0 # git tag v1.0Copy the code

use git push <remote> :refs/tags/<tagname> to delete remote tag, for example

Git push origin: v1.0Copy the code

checkout tag

Git checkout v1.0Copy the code

Modify the content of the label

When I checkout, I separate the HEAD pointer, and the modified content does not belong to any branch and cannot be committed directly.

1 Create a branch and modify it

Git checkout -b newbranch v1.0 # modify git add. Git commit -m "v1.1"Copy the code

2 Delete the remote tag

Git push origin: v1.0Copy the code

3 Create a new tag in the new branch and push it to remote

Git tag -a v1.1 -m "release-1.1" Git push Origin v1.1Copy the code

Git rolls back to a commit

Git reset --hard HEAD~3 git reset --hard HEAD~3 Git reset --hard commit_id Resets to the SHA code specified for the commitCopy the code

View a line of code in a file

Git blame -l n1,n2 filename # git blame -l n1,n2Copy the code

The child module submodules

Addition of submodules

Adding a submodule is as simple as following:

git submodule add <url> <path>
Copy the code

In the command, URL is the path of the submodule, and path is the directory path of the submodule.

Git status will see that.gitModules is modified and a new file is added (the path you just added).

Git diff –cached Git diff –cached git diff –cached git diff –cached git diff –cached git diff –cached git diff –cached

Git commit adds submodules

Use of submodules

After cloning the project, there is nothing in the submodule directory by default. To download submodules, run the following command in the root directory of the project:

git submodule init
git submodule update
Copy the code

Or:

git submodule update --init --recursive
Copy the code

After executing, you have the source code in the submodule directory, and then execute the corresponding makefile.

Updates to submodules

After the maintainer of the submodule commits an update, projects using the submodule must be manually updated to include the latest commit.

In the project, go to the submodule directory, perform git pull updates, and view the Git log to see the commit.

When you’re done, go back to the project directory and see the submodule updates that need to be committed. Use git add to commit.

Deleting a Submodule

Sometimes the project maintenance address of a submodule changes, or the submodule needs to be replaced, you need to delete the original submodule.

Deleting a submodule is complicated. The procedure is as follows:

Rm -rf submodule directory Delete submodule directory and source code vi. Gitmodules Delete submodule entries in the. Gitmodules file vi. Git /config Delete submodule entries in configuration items rm. Git /module/* Delete submodule directories under modules. Each submodule corresponds to a directoryCopy the code

After the command is executed, run the add submodule command again. If an error message is displayed, run the following command:

Git rm --cached submodule name

After the deletion is complete, it can be submitted to the warehouse

The error message

.gitmodules No URL found for submodule pathCopy the code

There may be a problem with the.gitsubmodules file at the root of the current repository. Modify the file, for example

[submodule "parser"] path = rule/SRC/camphor/parser url = HTTP: / / http://192.168.20.28/wuzhenyu/dbproxy-interface-api.gitCopy the code

NOTE: NOTE that the path path is relative to the current repository root directory and cannot end with a slash

And then execute

git submodule update --init --recursive
Copy the code

You can update submodules