The basic configuration
Git --version git version 2.20.1.windowsCopy the code
The user information
Configure the user name and email address of the individual:
git config --global user.name "your_name"
git config --global user.email "[email protected]"
Copy the code
If you use the –global option, the configuration file you change is the one in your user’s home directory, and all your future projects will default to the user information configured here.
To use a different name or email for a particular project, simply remove the –global option and reconfigure it. The new Settings are stored in the.git/config file of the current project.
The three scopes of config are equal to local by default
git config --local
git config --global
git config --system
Copy the code
Local applies only to the warehouse. Global applies only to all warehouses of the user who logs in. System Applies only to all users of the system
Viewing Configuration Information
Git config –list git config –list
git config --list
http.postbuffer=2M
user.name=your_name
[email protected]
Copy the code
Git Basic commands
Create warehouse command
git init
Initialize the warehouse
Two ways:
- You already have project code before you use Git
CD Project code folder git initCopy the code
- There was no project code before Git
Git init your_project # will create a folder named CD your_project in the current pathCopy the code
git clone
To copy a remote repository is to download an item
Git clone [url] git clone [url] another-name # This is usually the project name after the last/of the URL. If you want a different name, you can add the desired name to this command. Git clone --depth=1 [url] #--depth=1 git clone --depth=1Copy the code
Submission and Modification
git add
Add files to the repository
git add [file1] [file2] ... Git add [dir] git add.Copy the code
git status
View the current status of the warehouse to display the changed files
Git status git status -s #Copy the code
git diff
Compare the differences between files, that is, the staging area and the workspace.
Git diff --cached [file] git diff --cached [file] Git diff commitId1 commitId2 -- file # Git diff commitId1 commitId2 -- file # Git diff commitId1Copy the code
git commit
Submit staging area to local warehouse.
Git commit -m [message] # git commit [file1] [file2]... -m [message] # commit the file from the staging area to the repository. Git commit -am "add and commit "git commit --amend # Git commit -- allow-empty-m "empty commit" #Copy the code
git reset
Roll back the version.
#Syntax format
git reset [--soft | --mixed | --hard] [HEAD]
#-- Mixed is the default and can be used without this parameter. It is used to reset the file in the staging area to be the same as the last commit, while the workspace file content remains unchanged.Git reset [HEAD] git reset HEAD^ <file Rollback to the specified version
#The soft parameter is used to roll back to a version:
git reset --soft HEAD
#The --hard parameter undoes all uncommitted changes in the workspace, returns the staging area and workspace to the previous version, and deletes all previous information commitsGit reset --hard HEAD git reset --hard [commitId] Git reset --hard origin/masterCopy the code
HEAD note:
- HEAD indicates the current version
- HEAD^ Previous version
- HEAD^^ Previous version
- HEAD^^^ the previous version
- And so on…
The value can be a ~ number
- HEAD to 0 indicates the current version
- HEAD~1 Previous version
- The previous version of HEAD^2
- HEAD^3 before the previous version
- And so on…
git rm
Delete the workspace file.
Git rm -f <file> # git rm -f <file> You must use the force-delete option -f git rm --cached <file> # to remove the file from the staging area but still want to keep it in the current working directoryCopy the code
git mv
Move or rename workspace files.
git mv
Git mv [file] [newfile] git mv -f [file] [newfile] #Copy the code
Commit log
git log
View historical submission records
Git log --oneline #--oneline option to view a concise version of the history. Git log --reverse --oneline #--reverse Git log --all --grep= 'MergeCopy the code
git blame
View the historical modification records of a specified file in a list
git blame README
Copy the code
Remote operation
git remote
Remote warehouse operation
Git remote show [remote https://github.com/tianqixin/runoob-git-test git remote add [shortname] [url] # add remote repository #Shortname is the local version libraryGit remote add origin [email protected]: tianqixin/runoob - git - test. Git git remote rm name # delete remote warehouse git remote rename Old_name new_name # Change the warehouse nameCopy the code
git fetch
Git merge Git merge git merge git merge git merge
#Suppose you have configured a remote repository and you want to extract updated data, you can do this first:
git fetch [alias]
#This command tells Git to retrieve data that it has that you don't, and then you can do it:
git merge [alias]/[branch]
Copy the code
git pull
Download the remote code and merge. Git merge FETCH_HEAD
Git pull < remote host name > < remote branch name >Copy the code
git push
Upload and merge the remote code
Git push < remote hostname > < local branch name >#If the local branch name is the same as the remote branch name, the colon can be omitted:Git push < remote host name >
#If the local version is different from the remote version, but you want to force a push, you can use the --force parameter:
git push --force origin master
#Delete the master branch of the Origin host using the --delete parameter:
git push origin --delete master
Copy the code
Branch management
git branch
Create branch command
git branch (branchname)
#With no arguments, Git Branch lists your local branches.
#If we want to create a branch manually. Run git branch (branchname).Git branch * master testing git branch -d (branchname) #Copy the code
git checkout
Branch switch command
Git checkout (branchname) git checkout. # Discard all files in your workspace and make them the same as the sum store
#Create and switch to the iss53 branchGit checkout -b git branch iss53#The preceding command is the abbreviation of the following two commandsGit Checkout iss53# switchCopy the code
git merge
Merge branch command
git merge
Copy the code
You can merge multiple times into the unified branch, or you can choose to delete the merged branch directly after the merge.
git cherry-pick
The specified commit is applied to the other branches
git cherry-pick <commitHash>
Copy the code
Common commands
Git config --add --local <key> <value> git config --get <key Git init myProject // create myProject folder GIT add [filename] GIT add -u // Add all changes to the staging area to be committed GIT mv file1 file2 // Rename the file GIT rm <filename> // delete files from the staging and workspace git log --oneline git log --all Git checkout -- [filename] Git checkout -b <branch_name> <remote_repo> Git cat-file -t [hashcode] git cat-file -p [hashcode] git diff [commit1] [commit2] git diff [commit1] Git diff -- [filename] git diff -- [filename] git diff -- Git resethead -- <filename> git resethead -- <filename> Git reset --hard <hashcode> git reset --hard <hashcode> Git branch -d [branchName] Git commit -am commit all changes git rebase -i [hashCode] Git rebase -- Abort // Discard the outstanding rebase operation git stash Git stash pop // Stash pop // stash pop // stash pop // stash pop // stash pop // stash pop // stash pop // stash pop // stash pop // stash pop // Git clone <projectUrl/. Git > <backupname.git> backup git project to local git clone --bare Git remote add <remote_repo_name> <url> Git push -f origin git push -f origin git push -f origin git push -f origin Git pull // Pull the changes from the remote branch and update the local branch, Git merge < branch 1> < branch 2> git merge < remote branch > // Merge remote branchCopy the code