Git stupid tutorial

reference resources
reference resources

Getting started with Git

1.1 what is Git

The world's most popular distributed version control system

In short, the code is cloned to the local warehouse and output to the workspace
After the staging area is completed, the work area is sent to the local warehouse and pushed to the work area (gitee/github)

Compared with SVN, Git has the advantage of no networking

The installation process is very simple. Note that you should set the name and mailbox and keep in mind:

git config --global user.name "xxx"
git config --global user.email "xxx@xx.com"

1.2 create warehouse (version Library)

The following code block implements adding a repository to the directory under disk D, and the last line is to change the directory into a git repository

cd D:
cd www
mkdir testgit 
cd testgit
pwd
git init

You can go to this computer to check. There will be one more git file
1.3 adding files to the version Library

All things like Git can track changes to text files, such as txt html java css
Binary files are not good, such as pictures and videos.

demonstration:

  • Create a new file in the warehouse Directory: readme Txt content: 11111111
  • Use the command git add readme Txt to the staging area (index)
git add readme.txt
  • Use the command git commit to tell Git to submit the file to the staging area
git commit -m 'readme.txt Submit'
  • Use the command git status to check whether any files have not been submitted
git status

Show nothing It's all submitted

Return the text file and continue to change readme Txt, add a line of 22222222, and use git status to see the results

git status

If it is red, it has been modified
There will be an English prompt, which means that it has been modified but not submitted

Use the command git diff readme Txt to see the process of adding 22222222

git diff readme.txt

I just added a line 22222222, in English

After seeing that it has been changed, start submitting it from the staging area to the warehouse git add git commit, and check it with git status,
Check twice, the first err, the second ok

git add readme.txt
git status
git commit -m "File add 22222222 content"
git status

1.4 version rollback

In readme Txt add a line 33333333
Repeat the above operation, and then git Log view history

git add readme.txt
git commit -m "add to readme.txt The content of the document is 33333333"
git log -pretty=oneline

Then, perform version rollback. The current version reverts to the previous version. There are two commands

git reset --hard HEAD^ 
# 33333333 no
# Previous version HEAD^^
# perhaps
git reset --hard HEAD~100 
# Fallback 100 versions

We want to restore 33333333

git reflog 
# This step is to look at the historical records and the Chinese description of commit
# The leftmost one is the version number, and then use the command to restore
git reset --hard xxx Version number xxx

1.5 understand the difference between work area and staging area
Workspace: directories seen on the computer
Warehouse: Git hides the directory. This is the version library, also known as the repository. There is a stage (staging area) in the version library. Git creates a branch master, and the pointer to the master is HEAD
Git submits files to the repository in two steps:

  • git add file, interpretation: submit the file to the temporary storage area
  • git commit to submit the staging area file to the warehouse branch

The following two steps are implemented:
Put readme Txt add 4444444444 and create a test txt
git add file
git commit file
Show nothing is success

git status
git add readme.txt 
git add test.txt
git status
git commit -m "Submit all files at once, including new files test.txt"
git statusgit status

1.6 undo modifying and deleting files

In readme Txt add a line 5555555, and then undo the operation

  • Modify it manually, and then add commit
  • Return to previous version reset
  • git checkout -- readme.txt ensure no commit
  • rm xxx delete file

The following is the implementation:
Add a.txt, add it to the cache, delete a.txt, undo the deletion, and submit

cd
git add a.txt
git status
rm a.txt
git status
git checkout -- a.txt
git status

1.7 remote warehouse

Register the gitee account, because SSH encryption is transmitted between the local warehouse and the gitee warehouse

  1. Create an SSH key in the home directory to see if there is one ssh directory. If yes, check the id_rsa and id_rsa.pub these two files, if any, are skipped, and neither is created
ssh-keygen -t rsa -C "15253909357@163.com"

id_rsa is the private key and cannot be disclosed. id_rsa.pub is a public key. This is random

  1. Log in to gitee, open the ssh public key page in settings, click add ssh key, fill in any title, and paste ID in the key text box_ rsa. Pub public key content

Click OK

  1. Add a remote warehouse on the gitee page, then associate the local warehouse with the remote warehouse and push it
git remote add origin https://gitee.com/xx/xx.git
git push -u origin master
# Push the local warehouse branch master content to the remote warehouse

As long as the submission is made locally, you can use the following command:
The git push origin master pushes local branches to gitee, and has a truly distributed version Library
git push origin master reports an error, and the push fails:
error: failed to push some refs to 'https://gitee.com/fu-guangjian/test-git.git'
resolvent

  1. Clone from remote library
    git clone https://gitee.com/xx/xx
    Take a look at the locally generated xx warehouse directory

Easy command line tutorial:
Git global settings:

git config --global user.name "fuguangjian"
git config --global user.email "15253909357@163.com"
To create a git Repository:

mkdir test-git
cd test-git
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/fu-guangjian/test-git.git
git push -u origin "master"
Existing warehouse?

cd existing_git_repo
git remote add origin https://gitee.com/fu-guangjian/test-git.git
git push -u origin "master"

1.8 creating and merging branches
In version rollback, GIT strings them into a line each time they are submitted. This line is the branch. In git, this branch is the main branch, that is, the master branch. Head - > Master - > submit
First create and switch the dev branch

git checkout -b fufufu
# git checkout -b xx yes create and switch 
# Equivalent to git branch dev + git checkout dev
git branch
# View the current branch. It should be a master

Add a line of 5555555, then take a look at cat, commit, switch to the master of the main branch, and see readme Txt does not have 5555555. This is because the dev branch content is not merged into the master. git merge dev
Merge is used to merge the specified branch to the current branch. It can be viewed by cat and deleted after merging
dev branch

cat readme.txt
git add readme.txt
git commit -m "dev Branch added content 5555555"
git checkout master
git merge fufufu
cat readme.txt
git branch -d fufufu
git branch

1.9 conflict resolution
Add 66666666 to any new branch
Switch to master and add 77777777
Merging branches, conflicts occurred: <<<<<<, ======, > > > > >
To be modified into trunk content, the trunk shall prevail

Summary of common commands:

1, New code base

# Create a Git code base in the current directory
$ git init

# Create a new directory and initialize it as a Git code base
$ git init [project-name] # Download a project and its entire code history
$ git clone [url]

2, Configuration
# Show current Git configuration
$ git config --list

# Edit Git profile
$ git config -e [--global] # Set user information when submitting code
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"

3, Increase/Delete file
# Add the specified file to the staging area
$ git add [file1] [file2] ... # Add the specified directory to the staging area, including subdirectories
$ git add [dir] # Add all files in the current directory to the staging area
$ git add . # Ask for confirmation before adding each change # For multiple changes of the same file, multiple submissions can be realized
$ git add -p

# Delete the workspace file and put the deletion into the staging area
$ git rm [file1] [file2] ... # Stops tracking the specified file, but the file remains in the workspace
$ git rm --cached [file] # Rename the file and put the rename in the staging area
$ git mv [file-original] [file-renamed]

4, Code submission
# Submit staging area to warehouse area
$ git commit -m [message] # Submit the specified files in the staging area to the warehouse area
$ git commit [file1] [file2] ... -m [message] # Submit the changes in the workspace since the last commit, and go directly to the warehouse area
$ git commit -a

# Show all diff information when submitting
$ git commit -v

# Use a new commit,Replace last submission # If there are no new changes in the code, it is used to rewrite the submission information of the last commit
$ git commit --amend -m [message] # Redo the last commit and include the new changes of the specified file
$ git commit --amend [file1] [file2] ...

5, Branching
# List all local branches
$ git branch

# List all remote branches
$ git branch -r

# List all local and remote branches
$ git branch -a

# Create a new branch, but still stay in the current branch
$ git branch [branch-name] # Create a new branch and switch to it
$ git checkout -b [branch] # Create a new branch and point to the specified commit
$ git branch [branch] [commit] # Create a new branch and establish a tracking relationship with the specified remote branch
$ git branch --track [branch] [remote-branch] # Switch to the specified branch and update the workspace
$ git checkout [branch-name] # Switch to previous branch
$ git checkout - # Establish a trace relationship between an existing branch and a specified remote branch
$ git branch --set-upstream [branch] [remote-branch] # Merge the specified branch to the current branch
$ git merge [branch] # Select a commit to merge into the current branch
$ git cherry-pick [commit] # Delete branch
$ git branch -d [branch-name] # Delete remote branch
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

6, Label
# List all tag s
$ git tag

# Create a new tag in the current commit
$ git tag [tag] # Create a new tag and specify a commit
$ git tag [tag] [commit] # Delete local tag
$ git tag -d [tag] # Delete remote tag
$ git push origin :refs/tags/[tagName] # View tag information
$ git show [tag] # Submit specified tag
$ git push [remote] [tag] # Submit all tag s
$ git push [remote] --tags

# Create a new branch to point to a tag
$ git checkout -b [branch] [tag]

7, View information
# Show changed documents
$ git status

# Displays the version history of the current branch
$ git log

# Displays the commit history and the files that change each time a commit occurs
$ git log --stat

# Search submission history by keyword
$ git log -S [keyword] # All changes after a commit are displayed, and each commit occupies one line
$ git log [tag] HEAD --pretty=format:%s

# Display all changes after a commit. The "submission description" must meet the search criteria
$ git log [tag] HEAD --grep feature

# Displays the version history of a file, including file renaming
$ git log --follow [file]
$ git whatchanged [file] # Display each diff related to the specified file
$ git log -p [file] # Show last 5 submissions
$ git log -5 --pretty --oneline

# Display all submitted users, sorted by submission times
$ git shortlog -sn

# Displays who modified the specified file and when
$ git blame [file] # Show differences between staging and workspace
$ git diff

# Display the difference between the staging area and the previous commit
$ git diff --cached [file] # Displays the difference between the workspace and the latest commit of the current branch
$ git diff HEAD

# Show differences between submissions
$ git diff [first-branch]...[second-branch] # Show how many lines of code you wrote today
$ git diff --shortstat "@{0 day ago}" # Display metadata and content changes of a submission
$ git show [commit] # Show files that have changed in a submission
$ git show --name-only [commit] # Display the contents of a file at a time of submission
$ git show [commit]:[filename] # Displays the most recent commits of the current branch
$ git reflog

8, Remote synchronization
# Download all changes of remote warehouse
$ git fetch [remote] # Show all remote warehouses
$ git remote -v

# Display information for a remote warehouse
$ git remote show [remote] # Add a new remote warehouse and name it
$ git remote add [shortname] [url] # Retrieve changes from the remote warehouse and merge with the local branch
$ git pull [remote] [branch] # Upload specified local branch to remote warehouse
$ git push [remote] [branch] # Forcibly push the current branch to the remote warehouse, even if there are conflicts
$ git push [remote] --force

# Push all branches to remote warehouse
$ git push [remote] --all

9, Revocation
# Restore the specified files in the staging area to the workspace
$ git checkout [file] # Restore the specified file of a commit to the staging area and workspace
$ git checkout [commit] [file] # Restore all files in the staging area to the workspace
$ git checkout . # Reset the specified file in the staging area to be consistent with the last commit, but the workspace remains unchanged
$ git reset [file] # Reset the staging area and work area to be consistent with the last commit
$ git reset --hard

# Reset the pointer of the current branch to the specified commit, and reset the staging area, but the workspace remains unchanged
$ git reset [commit] # Reset the HEAD of the current branch to the specified commit, and reset the staging area and work area at the same time, which is consistent with the specified commit
$ git reset --hard [commit] # Reset the current HEAD to the specified commit, but leave the staging area and workspace unchanged
$ git reset --keep [commit] # Create a new commit,Used to undo the assignment commit # All changes in the latter are offset by the former and applied to the current branch
$ git revert [commit] # Remove uncommitted changes temporarily and move in later
$ git stash
$ git stash pop

Posted by kedarnath on Thu, 02 Jun 2022 07:00:33 +0530