1. Introduction to Git

  • Git is a distributed code version management tool that enables more efficient collaborative programming
  • It’s easy to forget how to write a command at work, so take some time to summarize common commands

Git has four very important concepts

  1. Workspace: Folder where projects are stored on the local computer
  2. Git repository: when you use Git to manage project files, there is an extra.git folder in the local project file. Git folder contains two parts, one is the staging area (Index or Stage), which is a temporary place to store files, usually using the add command to add files from the workspace to the staging area
  3. The git folder also contains the master branch that git automatically creates, and the HEAD pointer points to the master branch. You can use the commit command to add files from the staging area to the local repository.
  4. Remote repository: The project code is stored on the remote Git server, such as Github and Gitlab. Usually, clone command is used to copy the remote repository to the local repository and push it to the remote repository after development

During daily development, the code is actually placed in the workspace, i.e. local files such as xxx. go. The code is submitted to Index/Stage through commands such as add, which means that the code is fully managed by Git. After that, the staging area is committed to the local repository branch through commands such as commit. That means to play a version. In addition, the team collaboration process naturally involves interaction with remote warehouses

Git commands can be divided into several categories:

  1. Configuration management
  2. Work area and staging area
  3. Staging areas and local warehouses
  4. Local and remote warehouses

2. Configure commands

1. Set the configuration

  1. Configuring a User Name
git config --global user.name "your name";
Copy the code
  1. Configuring User Email Addresses
git config --global user.email "[email protected]";
Copy the code

2. Query the configuration

  1. The current configuration
git config --list;
Copy the code
  1. Local configuration:
git config --local --list;
Copy the code
  1. Global configuration
git config --global --list;
Copy the code
  1. The system configuration
git config --system --list;
Copy the code
  1. Querying configuration Sources
git config --global --list --show-origin
Copy the code

3. The other

  1. Configure which difference analysis tool to use for conflict resolution, such as VIMdiff
git config --global merge.tool vimdiff;
Copy the code
  1. Configure git command output to be colored
git config --global color.ui auto;
Copy the code
  1. Configure the text editor used by Git
git config --global core.editor vi;
Copy the code

3. Working area

1. Initialize the warehouse

git init
Copy the code

2. Clone the remote repository

gti clone <url>
Copy the code

3. Submit to temporary storage

1. Submit all files in the workspace to the staging area

git add .
Copy the code

2. Submit the specified file in the workspace to the staging area

git add <file1> <file2>
Copy the code

3. Commit all files in a folder in the workspace to the staging area

git add [dir];
Copy the code

4. Undo the modification

1. Discard temporary storage modification (unissued version)

git reset HEAD <file>... ;Copy the code

2. Discard workspace changes (not committed)

git checkout --<file>
Copy the code

5. Delete files

1. Delete the workspace file and also delete the record of the corresponding file from the staging area

git rm <file1> <file2>;
Copy the code

2. Delete the file from the staging area, but the file still exists in the workspace

git rm --cached <file>;
Copy the code

6. Storage (must be added to temporary storage area)

1. Save the current work progress and restore the working area and temporary area to the original one.

Git stash git stash save [message] // Save informationCopy the code

2. View the work progress list

git stsah list
Copy the code

3. View specific work progress information (related documents)

git stash show stash@{num}
Copy the code

4. Apply storage and restore work progress to workspace

Git stash apply stash@{num} git stash pop stash@{num} // Simultaneously remove the stashCopy the code

5. Remove the specified storage

git stash drop stash@{num}
Copy the code

6. Clear all work progress

git stash clear
Copy the code

7. Rename the file

git mv [file-old] [file-new];
Copy the code

8. Check the status

git status
Copy the code

9. Compare the current file in the workspace with the file in the staging area

git diff <file-name>;
Copy the code

4. Temporary storage area

1. Commit Commit to the local repository

1. Submit all update files (version)

Git commit -mCopy the code

2. Modify the last submission information

Git commit -- amend-mCopy the code

3. The leakage

Git add missed-file // missed-file git commit --amend --no-editCopy the code

2. View information

1. Compare the exceptions in the staging area with those in the previous version

git diff --cached
Copy the code

2. Specify the difference between the files in the staging area and the local warehouse

git diff <file-name> --cached
Copy the code

3. View the submission records

git log -p 
Copy the code

3. Play tag

1. List all labels

git tag
Copy the code

2. Create a label

Git tag -a v1.0 -m 'message'Copy the code

3. Check labels

Git show v1.0Copy the code

4. Push the label to the remote repository

Git push origin v1.0Copy the code

5. Push all labels

git push origin --tags
Copy the code

4. Branch management

1. Create a branch

git branch <branch-name>
Copy the code

2. Switch branches

git checkout <branch-name>
Copy the code

3. Create a branch and switch to the branch

git checkout -b <branch-name>
Copy the code

4. Delete the branch

git branch -d <branch-name>
Copy the code

5. Merge the current branch with the specified branch

git merge <branch-name>
Copy the code

6. Display all branches of the local warehouse

Git branch git branch -a // local + remote git branch -r // remoteCopy the code

7. Create a local branch based on the remote branch

git checkout -b <branch-name> <remote-name>/<branch-name>
Copy the code

8. Delete the remote branch

git push origin --delete dev
Copy the code

9. Pull branches

Git pull fatal: refusing to merge Suggested Origin Master -- Allow-suggested -historiesCopy the code

10. Push branch

Git push -f origin master force push git push git push git push origin qdbank:qdbank// push local branch to remote branchCopy the code

11. Merge a file of the branch

Git checkout minProgram -- git checkout minProgram -- git checkout minProgramCopy the code

5. Roll back the version

Overwrites the staging area and workspace with the contents of the specified COMMIT

Go to the previous version: git reset --hard HEAD^ Go to the specified version: git reset --hard commit_idCopy the code

The workspace is not changed, but the staging area is overwritten with the specified COMMIT, and all previously staging content becomes unstaged

Git reset - mixedCopy the code

The staging area is not changed, only the COMMIT is rolled back to the specified commit

Git reset - softCopy the code

To undo an operation, the commit and history before and after the operation are retained, and the undo is treated as the latest commit

git revert  HEAD
Copy the code

2. Query command history

git reflog
Copy the code

5. Local warehouse

  1. View the remote library
Git remote git remote -v git remote show originCopy the code
  1. Adding a remote repository
git remote add [remote-name] [url]
Copy the code
  1. Retrieves updates from the remote repository that are not present in the local repository
git fetch [remote-name]
Copy the code
  1. Remove a remote library
git remote rm [remote-name]
Copy the code

6. Ignore files

There will always be files that are not allowed or desired to be included in Git version management and do not appear in the untracked files list. In this case, you can create a.gitignore file that lists the file patterns to ignore

Git will ignore all.exe files *. Exe # except a.exe! BBB /*. TXT BBB /*. TXT # ignores all files with TXT extension in doc/ bbb/**/*.txtCopy the code

7, at the end

Write a bad place to correct, and you encourage it together