Distributed version control Git

Git has four working areas locally: Working Directory, stage/index, Repository or git directory, and git warehouse (Remote Directory). The conversion relationship between these four areas is as follows:

Workspace: the workspace is where you usually store the project code

Index / Stage: temporary storage area, which is used to temporarily store your changes. In fact, it is just a file to save the information to be submitted to the file list

Repository: the warehouse area (or version Library) is the safe place to store data, which contains the data you submit to all versions. Where HEAD refers to the latest version put into the warehouse

Remote: remote warehouse, a server hosting code, can be simply regarded as a computer in your project team for remote data exchange

Common commands in four areas

1. New code base

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

2. View file status

#View the specified file status
git status [filename]
#View all file status
git status

3. Workspace < – > staging area

# 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 .
# When we need to delete the file on the staging area or branch, and the workspace does not need this file, we can use( ⚠️)
git rm file_path
# When we need to delete files in the staging area or branch, but need to use them locally, there is no such file directly pushed at this time. If we add again before pushing, there will still be
git rm --cached file_path
# Directly add the file name to restore the file from the staging area to the workspace. If the file already exists in the workspace, it will be overwritten
# Adding [branch name] + file name means to pull the file from the branch name written by the branch name and overwrite the file in the workspace
git checkout

4. Workspace < – > resource library (version Library)

#The staging area -- > resource library (version Library)
git commit -m 'Description of this submission'
#If it occurs: commit unnecessary files or think it is wrong to submit them last time, or do not want to change the contents of the staging area, but just want to adjust the submitted information
#Remove unnecessary files added to the staging area
git reset HEAD file name
#Remove the last submission (it will directly change to the state before add)
git reset HEAD^
#Remove the last submission (after it becomes add, the status before commit)
git reset --soft  HEAD^

5. Remote operation

# Retrieve the changes of the remote warehouse and merge with the local branch
git pull
# Upload local designated branch to remote warehouse
git push

6. Other common commands

# Show current Git configuration
git config --list
git config -l
# Filter system configuration
git config --system --list
# Filter global configuration
git config --global --list
# Edit Git profile
git config -e [--global]
#Before the initial commit, you need to configure the user mailbox and user name, and use the following commands:
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
#Call up Git's help document
git --help
#View the help document of a specific command
git +command --help
#View git version
git --version
# Show the latest to farthest submission logs
git log
git log --pretty=oneline
# View command history
git reflog


git branch common commands

# 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]
git switch -c [branch]

# Switch to the specified branch and update the workspace
$ git checkout [branch-name]
$ git switch [branch-name]

# Switch to the previous branch
$ git checkout -

# Merge the specified branch to the current branch
git merge [branch]
# If the Fast forward mode is forcibly disabled, Git will generate a new commit when merging, so that the branch information can be seen from the branch history.
git merge --no-ff -m "merge with no-ff" dev

# Delete branch
$ git branch -d [branch-name]

# Delete remote branch
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

# Store current branch
git stash

git local project establishes connection with remote address

After establishing the remote warehouse and local project address

Initialize git in the local project folder

git init

Copy the remote project path address, and then execute:

git remote add origin Remote warehouse address

The connection is established, and the local is submitted to the cache

git add .

After caching, submit to the local git local warehouse

git commit -m 'Description of current submission'

Finally, it is pushed to the remote warehouse

git push -u origin master

Or force push

git push -u origin master -f

Problem solving

When git executes the command git push origin master, an error is reported: failed to push some refs to‘ https://gitee.com/ The root cause is that the content of remote warehouse is different from that of local warehouse. Just pull the different content in remote warehouse to the local. For example, I built a new remote warehouse. When I was ready to upload local content, I forgot to put the redme of the remote warehouse MD file synchronization error. Solution: git pull --rebase origin master will redme Synchronize the MD file locally, and then execute git push origin master again

Tags: IDE git Distribution github

Posted by lttldude9 on Tue, 09 Aug 2022 12:30:16 +0530