Tutorial on using git and github

This article will introduce how to use git and github.

1. About git version control system
1.1 Version control system
  1. What it means: A Version Control System (VCS) is software that helps developers on a software team work together and archive a complete history of their work

  2. Classification:img

1.2 Git

Git is an open source distributed version control system that can effectively and quickly handle version management of projects from small to very large. It is now the most popular version control system.

2. Download and install Git
2.1 Download git

[Download address]: https://git-scm.com/download

Just download the version corresponding to your system. windowsDownload the following version for 64-bit systems:

image-20231006231322553
2.2 Click to install ==>Next
image-20231006232524128

The rest is almost all the way to next. If you find it troublesome, you can skip this part. After the installation is completed, go directly to the third part.

2.3 Select components
  • Optional: Add a desktop icon
  • Optional: add a git option in windows terminal
image-20231006232650948
2.4 Select Git’s default editor
image-20231006233045532
2.5 Select the branch name created when executing the git init command
  • Just leave it as default
image-20231006233212287
2.6 Set environment variables
  • Just leave it as default
image-20231006233413263
2.8 Select SSH connection tool
  • Just leave it as default
image-20231006233504694
2.9 Select the certificate to use when encrypting the connection
  • Just leave it as default
image-20231006233603249
2.10 Configure end-of-line symbol conversion
  • Just leave it as default
image-20231006233649909
2.11 Configuring a terminal emulator for use with Git Bash
  • Default is also available

  • Option one: "Use MinTTY (the default terminal for MSYS2)"

    • Pros: Window resizable, non-rectangular text selection and display of Unicode fonts.
  • Option two: "Use Windows' default console window." Git will use Windows' default console window cmd.exe

    • Advantages: Can be used with Windows console programs such as interactive Python or node.js
    • Disadvantages: The default scrollback is very limited, Unicode fonts need to be configured to display non-ASCII characters correctly, and before Windows 10, its window cannot be freely resized and only allows rectangular text selection
image-20231006233703962
2.12 Default mode when using git pull command
  • Just leave it as default
image-20231006234014039
2.13 Whether to enable credential helperthe Login Credential Management Assistant
  • Just leave it as default
image-20231006234046976
2.14 Configuring additional options
  • Just leave it as default
image-20231006234141332
2.15 Whether to enable new experimental features. Clickinstall
  • It is not selected by default. If you want to try new features, just check the official website for information.
image-20231006234151855
2.16 Start the installation, wait for a moment, and clickfinish
image-20231006234446935

3. Connect Git and Github

3.1 Enter Git Bash

Right-click the mouse on the desktop ==> click 显示更多选项==>Open Git Bash here

image-20231006234432693 image-20231006234633065 image-20231006234744546
3.2 Configure Git user information
  • Username: githubUsername
  • Email: githubAccount email
  • Note: Quotes must also be added
$ git config --global user.name "用户名"
$ git config --global user.email "邮箱"
3.3 Check whether the configuration is successful

You can use the following two commands to view Git user information

  • Get current username
$ git config user.name
  • Get current email
$ git config user.email
3.4 Configure ssh connection

Git-based code hosting platforms such as Github, Gitlab, Gitee, etc. all provide Git services based on the SSH protocol. Using the SSH public key allows your computer to establish a secure connection with the Git remote repository.

3.4.1 windows system

Enter the following command in git bash to generate SSH KEY

  • The content within the quotation marks can be filled in as desired. Just copy the following command without making any modifications.
$ ssh-keygen -t rsa -C "XXXXX.com"
  • Here it shows that you need to enter three contents, just enter them all and press Enter!

    image-20231213112246760
  • Enter the C:/users/username/.ssh directory. Found two more files: id_rsaandid_rsa.pub

    image-20231006235724437
  • id_rsa.pubOpen the file with notepad

    image-20231006235842982
  • Copy the contents of id_rsa.pub to Github’s SSH keys

    • Entering interface github_setting

    • Click on the leftSSH and GPG keys

    • ClickNew SSH key

    • Paste the content you just copied from id_rsa.pub into Github's Key , set it title, and clickAdd SSH key

      • TitleYou can fill it in yourself
      image-20231007000250797
3.4.2 Linux system
  • UbuntuInstall ssh in the environment

    sudo apt install ssh
    
  • Check if there is a hidden file **.ssh** in the home directory

    ls -ah
    
  • If not, generate SSH KEY (the content within the quotation marks can be filled in as you like)

    ssh-keygen -t rsa -C "XXXXX.com"
    
  • Enter the /root/.ssh directory and view the id_rsa and id_rsa.pub files

  • Copy the contents of id_rsa.pub to Github's SSH keys (same as windows)

4. Commonly used git commands

image-20231007005114934
4.1 git addAdd workspace modifications to the local staging area
  • Add all edits
$ git add .
  • Add changes to a file
$ git add <文件名>
4.2 git commitSubmit the modifications in the staging area to the local repository
  • The description of this modification needs to be filled in in quotation marks.
$ git commit -m "添加修改信息"
4.3 git pushSubmit changes in the local repository to the github remote repository
$ git push
4.5 git statusCheck the status of files
$ git status
4.6Manage git branchbranches
  • list all branches
$ git branch
  • Create new branch
$ git branch <branch_name>
  • delete branch
$ git branch -d <branch_name>
4.7 git checkoutSwitch branches
$ git checkout <branch_name>
4.8 Clone files from remote repository to local folder
$ git clone <url> [directory]
  • The url can be obtained through ssh and http
    • Enter the project ==> Code==> SSH/Http==> Just copy
  • If you do not fill in the directory, the cloned files will be stored in the current folder by default.
image-20231007002558235
4.9 git fetch
  • Retrieve all updates from a remote host locally
$ git fetch <远程主机名>
  • Only pull updates from specific branches
$ git fetch <远程主机名> <分支名> //注意之间有空格
  • View the latest status of the remote host branch
$ git log -p FETCH_HEAD
4.10 git pull

Retrieve updates from a certain branch on the remote host and merge them with the local specified branch.

Equivalent to git fetch+git merge.

$ git pull <远程主机名> <远程分支名>:<本地分支名>

5. Other git commands

5.1 Create a local warehouse
5.1.1 Select a suitable location on the local computer to create an empty directory as a warehouse
$ mkdir learngit
5.1.2 Enter the directory, right-click to open the Git bash window, and execute git initto turn it into a warehouse that can be managed by Git
$ git init
  • At the same time, you can find that there is an additional .gitdirectory (usually a hidden file) in the current directory. This directory is used by Git to track and manage the repository. Do not manually modify the files in this directory if nothing happens.
5.2 Remote warehouse
5.2.1 Create a warehouse on github, for example namedAndroid
5.2.2 git remoteAssociate remote library

The name of the remote library isorigin

$ git remote add <shortname> <url>
  • Example:
$ git remote add origin [email protected]:fograinwater/Android.git
//$ git remote add origin https://github.com/tugenhua0707/testgit
5.2.3 git pushPush the current main/master branch to the remote library
$ git push -u <short_name> <your_branch_name>

Example:

$ git push origin main:main
$ git push –u(第一次要用-u 以后不需要) origin master//把当前master分支推送到远程库

Since the remote library is empty, masterwhen we push the branch for the first time, we add -uparameters. Git will not only push the local masterbranch content to the remote new masterbranch, but also associate the local masterbranch with the remote masterbranch . In the future, You can simplify the commands when pushing or pulling

5.2.4 Clone files from remote repository
  • You can use ssh
$ git clone [email protected]:fograinwater/Android.git
  • You can also use http
$ git clone https://github.com/fograinwater/Android.git
5.2.5 View remote library information
$ git remote -v
5.2.6 Delete remote library
$ git remote rm <name>

"Delete" actually removes the binding relationship between local and remote, and does not physically delete the remote library.

5.2.7 Clone the remote library to the local library
$ git clone [email protected]:fograinwater/Android.git
5.3 Branch Management (Simplified)
$ git branch //查看本地所有分支 

$ git branch -r //查看远程所有分支

$ git branch -a //查看本地和远程的所有分支

$ git branch <branchname> //新建分支

$ git branch -d <branchname> //删除本地分支

$ git branch -d -r <branchname> //删除远程分支,删除后还需推送到服务器
$ git push origin:<branchname>  //删除后推送至服务器

$ git branch -m <oldbranch> <newbranch> //重命名本地分支
5.4 Others
5.4.1 View file modification details

git diff <filename>

5.4.2 Display commit log

git log

  • Show only brief log information

git log --pretty=oneline

(The string of numbers shown earlier is commit id)

5.4.3 Roll back files to historical versions
  • Use HEAD to roll back

    HEAD:current version

    HEAD^:last version

    HEAD^^: Previous version

    HEAD~100: First 100th version

    To roll back to the previous version:

    git -reset --hard HEAD^

  • Take advantage of commit id fallback

    git reset --hard <commit id>(Generally speaking, you only need to write the first few digits of the commit id, and git can automatically find it)

5.4.4 Display historical version records that can be cited

git reflog

What does it mean to be able to quote historical commit versions?

  • You can only view the HEAD pointer and its previous version information using git logthe command. If the version has been rolled back, it may happen that there are still historical commit versions after the HEAD pointer, and these commit version information cannot be seen through the command git log. of.

    [That is: git logthe command is to display the current HEADand its ancestors, and the recursion is along the father of the current pointer, the father of the father,..., this principle]

  • git reflogWe can view all historical version information by using the command. Since the purpose of viewing all historical version information is mostly used for version rollback or recovery operations to find the required commit index, this command is named reflog(reference log), that is, reference log.

  • git logSchematic diagram of commands and git reflogcommand scope:

img
  • reflogIt is not part of the Git repository, it is stored separately, it is purely local

  • git reflogThe content displayed by the command should be stored in .git/logs/HEADa file or .git/logs/refsa file in a directory, which means that git reflogthe command retains all the user's operations in the local library starting from the clone warehouse.

5.4.5 Undo all modifications to files in the workspace

(Accidentally modified or deleted files in the workspace)

git checkout -- readme.txt

5.4.6 Unstage the modifications in the temporary storage area and put them back into the workspace

(The file was mistakenly modified in the workspace and added to the cache area)

git reset HEAD <file>

Then delete the modifications in the workspace according to step 6

6. Work area and temporary storage area

image-20230521145914378 image-20230521143744413 img
  1. Workspace: the directory (folder) of the current operation
  2. Repository: .git directory
  • Temporary storage area (stage/index): stores all modifications made by add
  • HEAD
  • master

git commitIt is only responsible for submitting the modifications to the staging area. If some modifications are not added git addto the staging area, they cannot be git commitsubmitted to the local library.

7. Branch Management

1. View branches

git branch

2. Create a branch

git branch <name>

3. Switch branches

git checkout <name>orgit switch <name>

4. Create + switch branches

git checkout -b <name>orgit switch -c <name>

5. Merge a branch into the current branch

git merge <name>

  • When Git cannot merge branches automatically, it must resolve conflicts first. After resolving the conflict, submit again and the merge is completed.
  • To resolve the conflict, we need to manually edit the files that failed to merge with Git to the content we want, and then submit it.
  • git log --graphYou can see the branch merge diagram with the command
  • When merging branches, --no-ffyou can merge in normal mode by adding parameters. The merged history has branches and you can tell that the merge has been done. However, with fast forward merge, you cannot tell that the merge has been done.
6. Delete branch

git branch -d <name>

7. Store the modifications of the current workspace (branch) to clear the workspace

git stash

8. To develop a new feature, it is best to create a new branch
  • If you want to discard a branch that has not been merged, you can git branch -D <name>forcefully delete it
9. Multi-person collaboration
  • To view remote library information, use git remote -v;
  • If a newly created branch is not pushed to the remote location, it will not be visible to others;
  • Push the branch locally and use it. git push origin branch-nameIf the push fails, use it to git pullgrab the new remote submission first;
  • Create a local branch corresponding to the remote branch and use it. git checkout -b branch-name origin/branch-nameThe names of the local and remote branches should be the same;
  • To establish the association between local branches and remote branches, use git branch --set-upstream branch-name origin/branch-name;
  • Fetch the branch from the remote and use it git pull. If there is a conflict, resolve the conflict first.

9. Tag management

The tag is actually a snapshot of the repository, tied to a commit

Tags are created and deleted instantly

The default label is placed on newly submitted commits

1. Create tags
  • Create a normal label

    • First switch to the branch that needs to be tagged

      git checkout master

    • Tag

      git tag <tagname>

  • Create a label with a description

    git tag -a <tagname> -m <说明文字>

  • Create tags on historical commits

    git tag <name> <commit id>

2. View all tags

git tag

3. View label information

git show <tagname>

4. Delete tags

git tag -d <tagname>

5. Push the tag to the remote warehouse
  • push a single label

    git push origin <tagname>

  • push all local tags that have not been pushed to the remote

    git push origin --tags

6. Delete the tag in the remote warehouse

git push origin :refs/tags/<tagname>

Guess you like

Origin blog.csdn.net/m0_63428773/article/details/134970596