Git

  • Version control
    • What is version control?
    • Local and centralized version control
    • Distributed version control -Git
  • Git theory
    • The workspace
    • branch
    • The HEAD pointer works with reset and checkout
    • Fetch, Clone, and pull
  • Install the configuration
    • Git installed
    • Git configuration
  • Use the sample
    • Set up SSH
    • Associated remote warehouse
    • upload
    • download
  • The IDEA of integrated Git
  • Attached: Common Linux commands

Version control


What is version control?

Revision control is a software engineering technique used during development to manage the history of changes made to files, directories, or projects. It can be easily viewed and backed up to restore previous versions. Parallel development, improve development efficiency, reduce the burden of developers, save time, and reduce human error. To put it simply, it is a technology that facilitates the management of multi-person collaborative development projects.

Local and centralized version control

  • Local version control Local version control is simply like the backup or snapshot we make on our local computer, recording every update, suitable for personal use. (for example, common document modification 1 version, 2 version, final version, ultimate version, ultimate version, final version, and so on)
  • Centralized version control centralized means that data is stored on the server rather than locally, which makes it easier for multiple developers to collaborate and synchronize updates or upload their own changes from the server. In this case, the local user only has the version before the synchronization. Therefore, all data may be lost if the server is damaged, and regular backup is required.

Distributed version control -Git

The biggest difference between distributed and centralized is that developers can commit locally, and each developer can clone a complete Git repository on the local machine. In this way, you can view all version history locally, submit it offline locally, and push it when connected to the network. It will not be unable to work because of server damage or network problems. At the same time, as long as there is no problem with the user’s device, all the data can be restored.

Git theory


The workspace

Git has three local workspaces, plus the remote Git repository has four workspaces:

  1. A Working Directory workspace is where you normally store project code
  2. Stage/Index Staging area, used to temporarily store your changes
  3. A Repository is a place where data is stored securely
  4. Git repository (Remote Directory) A Remote repository that hosts code



The working process

  1. Adds or modifies files in the working directory
  2. Place files that need to be versioned in the staging area
  3. Submit files from the staging area to the warehouse

branch

New branches can be created for different purposes or for parallel development by people in different departments, such as development, testing, release, emergency fixes, and so on. Separate from the main line for additional operations without affecting the main line like threads, or parallel universes without affecting each other, merging branches and so on as needed. Common instructions:

# list all local branches
git branch
List all remote branches
git branch -r
Create a new branch, but remain in the current branch
git branch [branch-name]
Create a new branch and switch to that branch
git checkout -b [branch-name]
Merge the specified branch into the current branch
$ git merge [branch-name]
# Switch branches
$ git checkout [branch-name]
# delete branch
$ git branch -d [branch-name]
# delete remote branch
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]
Copy the code

The HEAD pointer works with reset and checkout

  • Git has a HEAD pointer that moves forward one bit on each commit and only exists in the staging area and warehouse. If we use git add, the HEAD pointer will enter the staging area. If we continue with git commit, the HEAD pointer will enter the repository.
  • Git reset git reset resets the HEAD pointer one bit. That is, if you are in the warehouse area, you go back one notch to the staging area, and if you are in the staging area, the pointer goes back to the previous staging area. The reset command provides three ways to use it. — soft –mixed –hard:
git  reset  --soft # move pointer
git  reset  --mixed # Move pointer + clear staging area
git  reset  --hard Move pointer + Clear staging area + Clear workspace
Copy the code
  • Git checkout is a commit that updates the current code to a HEAD pointer. Git checkout [branch-name] is a branch with a HEAD pointer that points to another branch and overwrites it. You can also git checkout [file-name] to update your content to a previous commit, but only if the HEAD pointer points to a different commit than the one you’re currently in.

Summary:

reset checkout
The principle of The head pointer falls back to the previous one Update the commit that the current code points to as a header pointer
Similarities and differences between Restoring a local file (Undo) Restoring a local file (replace)

Fetch, Clone, and pull

fetch clone pull
Suitable for the scene There are local warehouse main branches, download other branches No local warehouse Local and remote repositories are out of sync
Similarities and differences between Fetching the latest from remote to local does not automatically merg Clone repository locally, no need for local repository (git init) Get the latest from remote and merge to local

Git pull = git fetch + git merge. In this way, fetch has more advantages. You can choose whether to merge or not after downloading, while Clone is a process from scratch.

Install the configuration


Git installed

After downloading and installing the default step, the menu will have three programs:

  • Git Bash: Unix and Linux style command line, most used (recommended)
  • Git CMD: Windows style command line
  • Git GUI: Graphical Git interface

Git configuration

  • User Configuration (Set the user name and email address)
Check the current user configuration
git config --global --list
Copy the code
  1. Directly modifying

    C:\Users\Administrator\ .gitconfig

  2. Command Line modification
git config --global user.name "123"  # the name
git config --global user.email [email protected] # mailbox
Copy the code
  • System configuration (default)
Check the system configuration
git config --system --list
Copy the code

Git\etc\ gitConfig (gitconfig in Git installation directory)

Use the sample


Set up SSH

  1. Open Git Bash, create an SSH Key and enter the password as prompted, or press Enter
ssh-keygen -t rsa -C "Registered Email"
Copy the code



2. Open the id_rsa.pub file under. SSH and copy the key content in the file

3. Enter thegithub, avatar -> Settings ->SSH and GPG keys->new SSH key





Run the SSH -t [email protected], as shown in the picture successfully connected to Github.

Associated remote warehouse

  1. Creating a remote repository

    Click New in the upper left corner of the home page to create a New remote repository



    Fill in the warehouse name, description, etc., select language and certificate.



    2. The clone to the local

    Git clone SSH. Replace SSH with your repository SSH



3. Enter command association

git remote # check the associated remote repositoryGit remote RM Repository name# removeGit remote add your repository SSH# Add/associate
Copy the code

Can not duplicate the name of the repository, here the origin is repeated, you can take another name, I will show how to delete and then add.

upload

  1. Modify the local repository and create test. TXT

  2. Untracked file has test. TXT. You can say git add < file> commit. Commit all untracked files and synchronize the staging area.



    Git restore unstage unstage the staging of the test. TXT file. To commit, type git commit -m.

  3. Now that the file has been committed to the local repository, nothing to commit is displayed. Next we synchronize to the remote repository and type Git push -u Origin master.

  4. The test. TXT file has been synchronized to github remote repository.



    5. Notice that submission is ignoredThe.gitignore file is set to ignore file types (such as class, log, JAR, etc.).
symbol role example
The name begins with an exclamation point! Indicates that it will not be ignored ! Lib.txt (except lib.txt)
The name is preceded by the path separator / Indicates that files to be ignored are in this directory, but files in subdirectories are not ignored. /temp (only TODO files in the project root directory are ignored, excluding other directories temp)
The name is followed by the path separator / Indicates that subdirectories of this name in this directory are to be ignored, not files (default files or directories are ignored) Build/(ignore all files in the build/ directory)

Linux wildcards can also be used. For example, the asterisk (*) stands for any number of characters, and the question mark (?) Represents a single character, square brackets ([ABC]) represents the range of optional characters, and curly braces ({string1,string2… }) represents optional strings, etc. Example:.txt # ignore all.txt files, so upload will not be selected! Doc /.txt # ignores doc/notes.txt but excludes doc/server/arch.txt

download

  1. Delete the test. TXT file in the workspace and open Git Bash to check the status of the deleted file: test. TXT. Enter Git rm test.

  2. Similarly, submit to local warehouse, not to be described here. The local repository is now different from the remote repository (without test.txt).

  3. Force local synchronization of remote repository content.
git reset --hard origin/master # discard local changes
git pull  Git merge = git fetch + git merge
Copy the code



Refresh the workspace again and you will see the deleted test.txt.

The IDEA of integrated Git


  1. Set up Git

    Select the Git. Exe File for your Git installation and click Test to make sure the setup is successful.

  2. Log in to your GitHub account

    File -> Setting->Version Control -> GitHub, enter the account secret login

  3. Creating a local repository

    VCS — >Import Version Control — >Creat Git Respository, select the project directory

  4. IDEA changes as shown in the following figure:



    You can open Terminal and use the familiar command line for operation, or use the button integrated with IDEA in the upper right corner for operation.



    Without further ado about the command line, we continue with the IDEA integration.
  5. Add to staging area

    Right-click project — >Git — > Add (equivalent to Git add.)



    After adding, the file will turn green. You can also view it by typing git status.

  6. Commit Commit to the local repository

    You can right-click the project – >Git – > Commit Directory or in the upper right corner √ or navigation on the left



    Git commit -m

7. Synchronize data to the remote repository

VCS – >Import into Version Control – >Share Project on GitHub



Enter the repository name and description in the box, and click Share.



Synchronization succeeded:



Attached: Common Linux commands


instruction role
cd Change the directory
cd . . Go back to the previous directory and CD directly to the default directory
pwd : Displays the current directory path.
ls(ll) Both list all files in the current directory, except that LL (two LLS) lists more details.
touch Creating a new file such as touch index.js creates an index.js file in the current directory.
rm If you delete a file, rm index.js will delete the index.js file.
mkdir To create a directory is to create a folder.
rm -r To delete a folder, run rm -r SRC to delete the SRC directory
mv Move files
reset Re-initialize the terminal or clear the screen
clear Clear the screen
history View command history.
help help
exit exit
# Said annotation

Original is not easy, please do not reprint (this is not rich visits add insult to injury) blogger home page: blog.csdn.net/qq_45034708 If the article is helpful to you, remember to focus on the likes collection ❤