Learning links

  • Git Basic tutorial
  • ProGit (Chinese Version)

Install Git

To use Git on Windows, you can download the installation program from the Git official website and use the default options to install Git.

SSH Key

ssh-keygen -t rsa -C "[email protected]" Create an SSH Key with your own email address
Copy the code

In the user home directory, check whether there is. SSH directory, if there is, then check whether there are id_rsa and id_rsa.pub files in this directory, if there is already, you can skip to the next step. If not, open the Shell and create an SSH Key

Adds the public Key of the SSH Key to the remote warehouse server

Example flow: Log in to GitHub, open “Account Settings”, “SSH Keys” page: then click “Add SSH Key”, fill in any Title, paste the contents of id_rsa.pub in the Key text box: Click “Add Key” and you should see the keys that have been added

Configuring Git Information

git config --global user.name "Your Name"   # username
git config --global user.email "[email protected]" # email (use your own email address)
Copy the code

Create a version library

git init        Create a Git repository. Create a Git repository. Create a Git repository.
Copy the code

Add changes to the Git repository

git add readme.txt Add changes to the specified file to the staging area
git add .          Add all changes to the staging area
Copy the code
git commit -m "Instructions for this submission" Commit changes stored in the staging area to the current branch
Copy the code

View the status of the warehouse

git status Check the state of the workspace
Copy the code
git diff What was changed
Copy the code

View the history of the commit

git log                  View the complete record information
git log --pretty=oneline View the profile information
git log --abbrev-commit  # Record the shorthand id in the message
git log --graph          # check the merge status of branches
git log --graph --pretty=oneline --abbrev-commit View branch merges and display only brief information
Copy the code

Version back

git reset --hard HEAD^         Back to the previous commit version, two on HEAD^^, three on HEAD^^^,......
git reset --hard 1094a8c...... Back to the specified version based on the version ID generated during the commit. The id number is long, and usually the first seven digits are matched automatically
Copy the code

Note: If you want to go back to the last commit, you can no longer see the version ID of the last commit. What should I do? As long as the command line window above is not closed, you can go up to find the corresponding version ID, and then back up according to the version ID. What if the command line window is closed? Each commit corresponds to a version ID, and the question is whether we can find that version ID and then roll back according to that ID. $$$$$$$$$$$$$$$$$$$$$$ You can run the git reflog command to view the records of the git commands that have been executed. The records contain version IDS.

Undo modify

git checkout -- readme.txt Undo the changes in the workspace
Copy the code

Reading: Git checkout — readme. TXT: git checkout — readme. TXT: git checkout — readme. One is that readme.txt has been added to the staging area and then changed, and now undoing the changes will return to the state after it was added to the staging area. Return the file to the state it was at the last Git commit or git add.

git reset HEAD readme.txt Undo the changes in the staging area and put them back into the workspace
Copy the code

Local and remote warehouses

git remote add origin https://gitee.com/friendlysxw/learn-git.git    Associate the local repository with the remote repository
git remote -v                                                        View more detailed information about the remote repository
Copy the code
git clone https://gitee.com/friendlysxw/learn-git.git  Clone the remote repository directly locally
Copy the code
git push origin <branchName> Push the specified branch to the remote repository
git push -u origin master    The -u parameter will associate the local master branch with the remote master branch
git push                     Short for pushing the master branch to remote
Copy the code

Branch management

git branch                 # view branch
git branch <branchName>    Create a branch
git branch -d <branchName> # delete branch
git branch -D <branchName> # delete branch forcibly
git branch --set-upstream-to=origin/<branchName> <branchName> 
       
        
       
      
Copy the code
git checkout <branchName>  # Switch branches
git switch <branchName>    # Switching branches is recommended
Copy the code
git checkout -b <branchName> origin/<branchName> Create remote branch to local
Copy the code
git checkout -b <branchName> Create and switch branches
git switch -c <branchName>   # Create and switch branches are recommended
Copy the code
git merge <branchName>     # merge the specified branch to the current branch. If the commit is fast forward, the branch is deleted and the branch information is lost in the record
git merge <branchName> --no-ff -m "Fast forward merging is prohibited." Add -m to fast forward merge
Copy the code

Work site (currently uncommitted changes in workspace)

git stash                   Store the current work site
git stash list              # Review stored work site records
git stash apply 0           Restore the specified work site according to the record index
git stash drop 0            Delete specified work site records according to record index
git stash pop               Restore the last stored work scene and delete the last record from the stored record
Copy the code

Copy specifies commit to the current branch

git cherry-pick <commitId>
Copy the code

Label management

git tag <tagName>                       Create a tag on the latest submission
git tag -a <tagName> -m "describe info" Create a tag with instructions
git tag <tagName> <commitId>            # create a tag on the specified submission
git tag                                 View all tag names
git show <tagName>                      # check the details of the specified tag
git push origin <tagName>               Push the specified tag to the remote repository
git push origin --tags                  Push all tags to the remote repository
Copy the code

View the git commands that have been executed

git reflog
Copy the code

Write in the last

This article when review, first start, hey hey ~