This is the 18th day of my participation in the August Genwen Challenge.More challenges in August

Introduction to the

  1. Git, one of the most advanced distributed version control systems, was originally developed by Linus, the father of Linux, to better manage the open source Linux system.

  2. Download: npm.taobao.org/mirrors/git…

  3. The installation

    • Linux:sudo apt-get install git
    • For Windows and macOS, download the installation package from the official website and install it.
  4. Set up the

    Once the installation is complete, set the signature and email:

     $ git config --global user.name "user name"
     $ git config --global user.email "[email protected]"
     #Optionally, let Git command line display colors
     $ git config --global color.ui true
    Copy the code

Version library creation and management

  1. How do I create a repository

    • $mkdir gitlearn creates an empty folder, which is not described on Windows but can be created using the $mkdir gitlearn command on Linux or macOS.

    • Make the directory you just created into a manageable repository with $git init.

  2. How do I add a file to the repository

    • In the empty folder you just created, add your own files, using readme.md as an example.

    • Add files to the repository with the git add command;

        #Add a single file
        $ git add readme.md
        #Add all files
        $ git add .
      Copy the code
    • Commit files to the repository with git commit

       $ git commit -m "this is a readme.md file."
      Copy the code
  3. How to view the contents of warehouse modifications

    • Use git status to check the current status of the repository.

    • Git diff filename to see what is changed in the file.

        $ git status
        $ git diff readme.txt
      Copy the code
    • Git log Displays the most recent and farthest commit logs

        $ git log --pretty=oneline
      Copy the code
    • If you want to go back to a future version, use git reflog to check the command history to confirm the commit_id. Commit_id, HEAD: Run the git resrt –hard commit_id command

        $ git reset --hard HEAD^
      Copy the code
    • To directly discard a file change in the workspace, use the git checkout — filename command

        $ git checkout -- readme.md
      Copy the code
    • To delete a file, run the git rm command in the workspace to delete the file from the repository, and then commit the file using git commit

        $ git rm readme.md
        $ git commit -m "delete readme.md"
      Copy the code

Remote warehouse

  1. If you connect Git to Github, you can perform the following operations:

    • Pub. If yes, copy the id_rsa and id_rsa.pub files and add them to Github SSH keys to establish a connection between the two files.

    • If no, run the following command to generate it:

        $ ssh-keygen -t rsa -C "[email protected]"
      Copy the code
  2. How do I add a remote repository

    • Run the following command to create a local and remote repository for management, and then push the contents of the local repository to the Github repository.

        $ git remote add origin [email protected]:cunyu1943/LeetCode.git
      Copy the code
    • Clone island local from remote repository:

        $ git clone https://github.com/cunyu1943/LeetCode.git
      Copy the code
  3. Push local repository content to remote repository;

     #First push
     $ git push -u origin master
     #Later push
     $ git push origin master
    Copy the code

Branch management

  1. Create and switch branches

     $ git switch -c dev
     #An equivalent
     $ git checkout -b dev
     #An equivalent
     $ git branch dev
     $ git checkout dev
    Copy the code
  2. Check the current branch, where ∗*∗ indicates the current branch, and switch back to the master branch;

     $ git branch
     $ git checkout/switch master
    Copy the code
  3. Merge dev branch into Master branch;

     $ git merge dev
    Copy the code
  4. Delete branches;

     $ git branch -d dev
    Copy the code
  5. Forcibly delete unmerged branches;

     $ git branch -D dev
    Copy the code
  6. View the branch merge diagram

     $ git log --graph
    Copy the code

undo

Undo changes are made up of the bottom part (a separate file or fragment in the staging area) and the top part (exactly how the change was undone);

git reset

Undo changes by rolling back the branch record to several commit records, mainly for the local branch you developed, i.e. the content of the commit, is not valid for the remote branch;

#Go back to HEAD's parent record
git reset HEAD~1
Copy the code

git revert

Git Revert is used when you want to undo and share changes that have been pushed.

#Uncommit a HEAD commits a new record that is in the same state as the HEAD parent
git revert HEAD
Copy the code

collaboration

  1. Git remote -v;

  2. If a local branch is not pushed to a remote branch, it is invisible to others.

  3. Git push origin branch-name (); git pull ();

     $ git push origin master
     #If the push fails, run the following command
     $ git pull
    Copy the code
  4. Git checkout -b branch-name origin/branch-name

     $ git checkout -b dev origin/dev
    Copy the code
  5. Git branch –set-upstream branch-name origin/branch-name

     $ git branch --set-upstream dev origin/dev
    Copy the code
  6. Use Git pull to grab branches from remote locations. If there are conflicts, resolve them first and then process them again.


Label management

  1. Git switch branch-name;

  2. Label and view;

     #Type a file called"v1.0"The label of the
     $Git tag v1.0
     #View all labels
     $ git tag
    Copy the code
  3. Git show

     $Git show v1.0
    Copy the code
  4. Create a label with a description. Type the label name with -a and specify the description with -m.

     $Git tag-a v1.0-m"version 1.0 released"
    Copy the code
  5. Git push origin :refs/tags/

     #Deleting a Local Label
     $Git v30.0 tag - d
     #Deleting a Remote Label
     $Git push origin: refs/tags/v30.0
    Copy the code
  6. Git origin

     #Push a label
     $Git push origin v1.0
     #Push all labels
     $ git push origin --tags
    Copy the code

Establish a remote connection between Github and Gitee

  1. Git remote rm

     $ git remote rm origin
    Copy the code
  2. Git remote add

    Gitee

    /

    .git


     $ git remote add gitee https://gitee.com/cunyu1943/LeetCode.git
    Copy the code
  3. Git remote add

    https://github.com/

    /

    .git


     $ git remote add github https://github.com/cunyu1943/LeetCode.git
    Copy the code
  4. Push to Gitee and Github

     #Pushed to the Gitee
     $ git push gitee master
     #Push to making
     $ git push github master
    Copy the code

Relative references

By using relative references, you can start from an easy-to-remember place to calculate;

  • Move up 1 commit record:^Add it to the reference name to tell Git to find the parent of the specified commit record, such asmaster^saidmasterParent node of;
  • Move up N commit records:~N;
#Switch the branch parent node
git checkout newBranch^

#Take many steps back
git checkout newBranch~4

#Force the master branch to point to the level 3 parent commit of the HEAD
git branch -f master HEAD~3
Copy the code

Moving commit records

git cherry-pick

The command is in the following format:

Git cherry-pick...
#Merge B1, B2, b3 into master
git checkout master
git cherry-pick b1 b2 b3
Copy the code

Interactive rebase

Turn on interactive Rebase by using the rebase command with the –interactive argument, shortened to -i;

Here’s what you can do when you open interactive Rebase:

  • Adjust the order of submitting records (mouse drag and drop);
  • Delete unwanted commits (by switchingpickTo complete);
  • Merge commit (merge multiple commit records into one);

Other tips

  1. Ignore special files

    In this case, you need to write a. Gitignore file. The file should be put into the version library, and you can version manage the. Gitignore.

  2. Configuring command Alias

     #For example,
     $ git config --global alias status st
     $ git config --global alias checkout co
     $ git config --global alias commit cm
    Copy the code