Git command line operation

1. Initialize Git’s local library

Local library initialization, which is to create the local library

Command: git init

Create a folder, open the Git Bash Here window from the folder right-click menu, and enter the command

image3617860c5d4415a7.png

You can see that an empty Git repository is initialized in the folder we created. You can check view Hidden files to see the generated folder.

Note: the.git directory contains local library directories and files. Do not delete or modify them.

2. Set a signature

Use this string to distinguish developer identity.

Form:

User name:chenjiaxing



Email address: 2609076192 @qq.com

Copy the code

The signature set here has nothing to do with the password of the account used to log into the remote library (that is, the code hosting center).

Command:

git config --global user.name chenjiaxing



git config --global user.email2609076192 @qq.com

Copy the code
  • Project level/repository level: valid only within the scope of the current local library

  • git config

  • System User level: specifies the user level for logging in to the current OPERATING system

  • git config –global

Level priority: The project level takes precedence over the system user level. Both use project-level signatures. If only the system user level is used, the system user level prevails.

Neither is allowed!

The general configuration information of the user level is in the. Git folder, while the user level is in the. Gitconfig file under the current system user directory. You can use CD ~ name to switch to the current user folder, and then view it.

Git basic commands

1) Status check

git status   

View the status of working area and temporary storage area

Copy the code

When entering commands in a newly initialized project:

2) add

git add [file name]

Adds New/Modify of the workspace to the staging area

Copy the code

3) submit

git commit -m "commit message" [file name]

Commit the contents of the staging area to the local library

Copy the code

4) View history

Multi-screen display control mode:

  • Space to turn the page down
  • B Turn the page up
  • Q exit

git log:

git log --pretty=oneline:

git log --oneline:

git reflog:

Tip: HEAD@{how many steps to move to the current version}

5) Forward and backward

Essence: Pointer movement

  • Index-based operations [recommended]

    git reset --hard [local index value]



    Such as: gitreset --hard a6ace91

    Copy the code
  • Use ^ symbol: only backward

    git reset --hard HEAD^



    Note: one ^ means one step back, n means n steps back

    Copy the code
  • Use the ~ symbol: only back

    git reset --hard HEAD~n



    Note: indicates n steps back

    Copy the code

Comparison of the three parameters of the reset command:

  • --soft: Argument: moves the HEAD pointer only in the local library
  • --mixedThe: argument moves the HEAD pointer in the local library and resets the staging area
  • --hard: Parameters: Move the HEAD pointer in the local library, reset the staging area, reset the workspace

6) Retrieve deleted files

Prerequisite: The existing state of the file is committed to the local library before deletion. Operation:

git reset --hard [pointer position]

Copy the code

The delete operation has been committed to the local library: the pointer position points to the history record

git reset --hard a6ace91

Copy the code

Delete operation not committed to local library: HEAD is used for pointer position

git reset --hard HEAD

Copy the code

7) Compare file differences

Operation:

Compare the files in the workspace with the staging area

git diff [File name]  

Copy the code

Compare files in your workspace with local library history Compare multiple files without file names

git diff [Historical version in local library] [File name]

Copy the code

Git branch

1. What are branches

In version control, multiple lines are used to advance multiple tasks simultaneously.

For example, our main branch is master, and now we need to develop a blue skin feature called Feaure_blue and a game feature called Feature_game. At this time, the two branches can be developed at the same time, no one has to wait for who, when the function development is completed, you can apply to merge into the main branch.

If we encounter a bug during development, we can directly create a bug fix branch, hot_fix, to fix the bug without affecting the operation of the main program.

2. Branch operations

  • Create a branch

    git branch[Branch name]

    Copy the code
  • See the branch

    git branch -v

    Copy the code
  • Switch branch

    git checkout[Branch name]

    Copy the code
  • Merging branches

  • Step 1: Switch to the branch where the change is being made (merge, add new content)

  • Git merge

Conflict: When we create a branch, we can modify both branches. When we modify the same place of the two branches, the two changes are inconsistent, which will cause the problem of version conflict.

Conflict:

Conflict resolution:

  • Step 1: Edit the file and delete the special symbols
  • Step 2: Modify the file to a satisfactory degree, save and exit
  • Step 3:Git add
  • Step 4:Git commit -m(Note: commit must not have a specific file name)

3. Git remote operation

1. Create a remote repository on Github

Step 1: Create an account on Github and log in

The second step:

Step 3:

2. Create an alias for the remote library address

git remote -vView all current remote address aliases

git remote add [alias] [Remote address]

Copy the code

3. Push

git push [alias] [Branch name]

Copy the code

Cloning of 4.

git clone[Remote address]

Copy the code

Function:

  • Download the complete remote library to the local
  • Example Create the origin remote address alias
  • Initialize the local library

5. Team member invitation

Click properties, enter the Github user name for the new member, click Add Collaborator,

You can also copy the invitation address and send it to new members. If you agree to enter the address, you can join the team

6. Pull

pull=fetch+merge

git fetch[Remote library address alias] [Remote branch name]

git merge[Remote library address alias/remote branch name]

Git pull [remote branch name]

Copy the code

7. Conflict resolution

  • If the changes are not based on the latest version of the GitHub remote library, they cannot be pushed and must be pulled first.
  • If a conflict occurs after being pulled down, perform “Branch Conflict Resolution” to resolve the conflict.

4. Cross-team operation

This means that non-team members want to collaborate on development without joining the team.

1.Fork

Outside team members click the fork button to copy the repository to their accounts

2. Modify the Settings locally and push them to remote Settings

3.pull request

Make a merge request to your team’s code

4. Enter the title to describe the modification

5. The warehouse creator reviewed and integrated the code

The warehouse creator can click pull Request to view the merge request

Can be audited code:

There is no problem in the audit. You can agree to merge:

5. Set SSH password-free login

1. Access the home directory of the current user

cd ~

Copy the code

2. Delete the. SSH directory

rm -rvf .ssh

Copy the code

3. Run the. SSH key directory command to generate the

// replace [email protected] with your own email address



ssh-keygen -t rsa -C "[email protected]"

Copy the code

Note: the -c argument is a capital C

4. Access the. SSH directory to view the file list

ll -la

Copy the code

Pub and id_rsa are generated. The id-rsa.pub file stores the generated key information. Let’s take a look

cat id_rsa.pub

Copy the code

5. Add the key to Github

Copy the id_rsa.pub file, log in to GitHub, click user profile picture →Settings→SSH and GPG keys

6. Create an SSH alias

git remote add origin_ssh git@github.com:2609076192/mydoc.git

Copy the code

You can then use this alias to commit and pull code

7. Submit tests