Basic use of Git and uploading to GitHub

1. Install and configure GIT

1. Install GIT Connect

GIT installation package link

2. Open GIT

Right mouse click on Git Bash Here

Please add a picture description

After installing Git, the first thing to do is to set up your username and email address. Because when version management is performed on the project through Git, Git needs to use these basic information to record who has operated on the project:
the code is as follows:

git config --global user.name "用户名"
git config --global user.email "邮箱地址"

If the --global option is used, the command only needs to be run once to take effect permanently.

3. Check configuration information

View global configuration

git config --list --global

View the specified global configuration

git config user.name
git config user.email

For example:

Please add a picture description

Two, the basic operation of Git

1. Execute git initthe command to convert the current directory into a Git warehouse

2. Check the status of the file through git statusor git status -sor commandgit status -short

Please add a picture description

At this time, the file is under Untracked files (untracked files) or is preceded by two question marks using a simple status query.

Please add a picture description

Please add a picture description

3. Use git addto track a file, or git add .track all files

For example:

git add index.html

Run git statusthe command , and you will see that the index.html file is under the Changes to be committed line, or the status of the file is displayed in a simplified way. There is a green A mark in front of the newly added file in the temporary storage area, indicating that it has been tracked , and in staging state:

Please add a picture description

Please add a picture description
4. Use to git commit -m "提交消息"submit the update, the submission message after -m must be filled in
. For example:

git commit -m "新建了index.html文件"

For the first submission or the file has an updated submission, as shown below

Please add a picture description

For files that have been submitted but not updated, the following image will appear

Please add a picture description

5. Modification of the submitted file After
modifying the file, check the file status at this time, and you will see that the content of the tracked file has changed, but it has not been placed in the temporary storage area.

Please add a picture description

Modified files that have not been placed in the temporary storage area are marked with a red M in front of them.

At this point we execute again git addand git committhe command submits the modified file again

6. View submission history
List all submission histories in order, with the most recent submission at the top

git log

For example:

Please add a picture description

Only the last two submission histories are displayed, the number can be changed, and the last few entries will be displayed when the number is changed

git log -2 --pretty=oneline

Please add a picture description

3. Use GIT to upload to GitHub

1. Create a new blank remote warehouse
Please add a picture description

2. Create a new blank remote warehouse successfully

Please add a picture description

Randomly choose one of the following 3 and 4

3. Upload the local warehouse to Github based on HTTPS

Please add a picture description

4. Upload the local warehouse to GitHub based on SSH

SSH consists of two parts:

  • id_rsa (private key file, stored in the client computer)
  • id_rsa.pub (public key file, needs to be configured in Github)

Generate SSH key

  • Open Git Bash
  • Paste the following command, and replace [email protected] with the email address you filled in when you registered your Github account:
    ssh-keygen-t rsa -b 4096 -C "[email protected]"
  • Press Enter three times in a row to generate two files, id_rsa and id_rsa.pub, in the C:\Users\username folder.ssh directory

Configure SSH keys

  • Open the id_rsa.pub file with Notepad and copy the text inside
  • Log in to Github in the browser, click on the avatar -> Settings -> SSH and GPG Keys -> New SSH key
  • Paste the contents of the id_rsa.pub file into the text box corresponding to Key
  • Fill in any name in the Title text box to identify where the Key comes from

Detect whether Github's SSH key is configured successfully

Use ssh -T [email protected]to detect

The configuration is successful as shown below

Please add a picture description

After the configuration is successful, you can upload it

Please add a picture description

Four, Git branch

View the branch list : git branch
Add a new branch : git branch 分支名
Switch branches : git checkout 分支名
Merge the code on the branch into the main branch :

  • Switch to the master branch first:git checkout main
  • Merge branch:git merge 分支名

Delete branch :git branch -d 分支名

Push the local branch to the remote repository :

//如果希望远程分支和本地分支名称不一样
git push -u 远程仓库的别名 本地分支名称:远程分支名称
例如:
//此时本地分支payment上传到GitHub时,分支名字为pay
git push -u payment:pay
//如果希望远程分支和本地分支名称一样
git push -u origin payment

Note: Only when you push the branch for the first time, you need to bring -u

git push origin mainThe following main can be replaced with any branch name, which can realize GitHub update upload

View the list of all branches of the remote warehouse : git remote show 远程仓库名
pull the latest code of the remote branch : git pull
delete the remote branch :git push 远程仓库名称 --delete 分支名

The general remote warehouse name is origin

Guess you like

Origin blog.csdn.net/weixin_53912712/article/details/129433352