Long text in thousands of words! Take you from getting started to mastering Git

Posted by private_guy on Sat, 18 Dec 2021 15:29:36 +0100

Long text in thousands of words! Take you from getting started to mastering Git
Catalog:

Preface
Introduction to Git
pragmatism
Deep Exploration
summary
Preface
Git is a development tool that programmers can learn and work with. Share a summary of common Git commands with you today.

Introduction to Git
Git is a distributed version control system that is not limited by network connections and has many other advantages. It is now the preferred version management tool for program developers. Non-developers can also use Git as their own document version management tool.

Maybe when I was a sophomore, I started to touch and use Git, and now I rely heavily on it from zero contact at the beginning. I'm really impressed by the power of Git.

Git has many api s, but in fact, 90% of the requirements in a project usually only need a few basic functions, so this article will talk about how to use Git in a project from two perspectives: pragmatism and in-depth exploration. Generally, after reading the section on pragmatism, you can start to use it in a project.

Note: This article is based on Mac system

pragmatism
Preparatory phase
Go to Git's website to download the appropriate installation package for you, install Git, open the command line tools, go to the working folder (to make it easier to understand what we're doing on the system desktop), and create a new demo folder.

Go to the Github website, register an account, log in, go to my blog, click Clone or download, click Use HTTPS, and copy the project address https://github.com/gafish/gafish.github.com.git Spare.

Back to the command line tools, you're ready to move on to the focus of this article.

Common operations
Pragmatism means that you can play Git with the following knowledge to easily meet more than 90% of your needs. Here's a list of the utilitarian Git commands, starting with a general look

git clone
git config
git branch
git checkout
git status
git add
git commit
git push
git pull
git log
git tag

Next, I'll walk through the entire process of submitting code using Git La substitution code by doing an example operation on my blog repository.

git clone

Pull substitution code from git server

git clone https://github.com/gafish/gafish.github.com.git

Once the code download is complete, there will be a gafish in the current folder. Github. COM directory, via CD gafish. Github. The com command enters the directory.

git config

Configure Developer User Name and Mailbox

git config user.name gafish
git config user.email gafish@qqqq.com

Each time the code is submitted, a submission record is generated that contains the currently configured user name and mailbox.

git branch

Create, rename, view, delete project branches. When you do project development through Git, you usually do it in the development branch, and merge the branches to the backbone when the development is complete.

git branch daily/0.0.0

Create a name daily/0.0. The daily development branch of 0, as long as the branch name does not include special characters.

git branch -m daily/0.0.0 daily/0.0.1

If you find the previous branch name inappropriate, you can rename the new branch to daily/0.0.1

git branch

View the current project branch list with the branch command without parameters

git branch -d daily/0.0.1

If the branch has completed its mission, it can be deleted by using the -d parameter. For the sake of proceeding to the next step, the deletion will not be performed yet.

git checkout
 Switch Branch
git checkout daily/0.0.1
 switch to daily/0.0.1 Branch on which subsequent operations will take place
git status
 View file change status

README in the project through any editor you like. Make some changes to the MD file and save it.

git status
 adopt git status Command to see the current state of the file Changes not staged for commit: (Change file not submitted to staging area)
On branch daily/0.0.1
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
    modified:   README.md
no changes added to commit (use "git add" and/or "git commit -a")
git add
 Add File Change to Staging Area
git add README.md
 By specifying the file name README.md You can add this file to the staging area if you want to add all the files available git add . Command, which can be passed git status See the current state of the file Changes to be committed: (File submitted to staging area)
On branch daily/0.0.1
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
    modified:   README.md
git commit
 Submit file changes to version Library
git commit -m 'Write reason for submission here'
adopt -m Parameters can enter submission description text directly from the command line
git push
 Push local code changes to the server
git push origin daily/0.0.1
origin Refers to the current git Server address, this line of commands means daily/0.0.1 Branch pushes to the server, and when you see the command line returning the following characters, the push succeeds.
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 267 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local objects.
To https://github.com/gafish/gafish.github.com.git
 * [new branch]      daily/0.0.1 -> daily/0.0.1
 Now let's go back Github Project Home Page of Web Site, Click Branch:master Pull down the button and you'll see what you just pushed daily/00.1 Branched
git pull
 Pull the latest code from the server locally
git pull origin daily/0.0.1
 If other project members make changes to the project and push it to the server, we need to update the latest changes locally. Let's simulate this.

Go to the project home page of the Github website, then daily/0.0.1 branch, online to README. Make some changes to the MD file, save it, and execute the above command in the command. It will pull the part you just modified online locally and open README in the editor. Md, you will find that the file has been synchronized with the online content.

If the code on the line changes and your local code changes, the pulled code may conflict with your local changes. Git normally handles this conflict merge automatically, but if you change the same line, you will need to merge the code manually, edit the file, save the latest changes, and add through git. And git commit-m'xxx'to submit the merge.

git log
 View version submission records
git log
 With the above command, we can view the version submission record of the entire project, which contains information such as the author, date, reason for submission, etc. The results are as follows:

commit c334730f8dba5096c54c8ac04fdc2b31ede7107a
Author: gafish <gafish@qqqq.com>
Date:   Wed Jan 11 09:44:13 2017 +0800
    Update README.md
commit ba6e3d21fcb1c87a718d2a73cdd11261eb672b2a
Author: gafish <gafish@qqqq.com>
Date:   Wed Jan 11 09:31:33 2017 +0800
    test
.....

Submit records may be very large, press J key to flip down, press K key to flip up, press Q key to exit viewing

git tag
 Mark milestones for projects
git tag publish/0.0.1
git push origin publish/0.0.1
 When we are ready to go online after completing a functional requirement, we should mark this complete project code and publish this marked version online. Here we will publish/0.0.1 For tag name and publishing, publishing is successful when you see the command line returning
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/gafish/gafish.github.com.git
 * [new tag]         publish/0.0.1 -> publish/0.0.1
.gitignore
 Set what does not need to be pushed to the server, this is a configuration file
touch .gitignore
.gitignore No Git Command, and a file in the project, by setting .gitignore Content tells Git Which files should be ignored and need not be pushed to the server to create one .gitignore File and open the file in an editor, each line representing a file or directory to ignore, such as:

demo.html
build/
What this means is Git Will be ignored demo.html Files and build/ Directory, which is not pushed to the server

Summary
By mastering these basic commands, you can start using them in your project. If you want to be practical, you can end up learning about Git. The occasional questions you encounter are basically answered by Google. If you want to explore Git's advanced features in depth, go on to the in-depth exploration section.

Deep Exploration
Basic concepts
Workspace (Working Directory)
It's the directory you see on your computer, like gafish above. Github. The com folder is a workspace

Local Repository
The workspace has a hidden directory. Git, this is not a workspace, but a version library for Git.

stage
There are many things stored in the local version library, the most important of which is a staging area called stage (or index), the first branch that Git automatically created for us, master, and a pointer to master called HEAD.

Remote Repository
Generally refers to the corresponding repository on the Git server, and the github repository where the example in this article is located is a remote version repository

The relationship between the above concepts
Several common Git processes between workspaces, staging areas, local version libraries, and remote version libraries are illustrated below:

Branch
Branching is to store the entire process of modifying records separately so that the separate branches are not affected by other branches, so you can make several different modifications simultaneously in the same database

Main branch (Master)
As mentioned earlier, master is the first branch that Git automatically created for us, also known as the main branch. When other branches are developed, they are merged into master.

Tag
Tags are used to mark a specific point or history of a submission, usually to mark the name or version number of a release (e.g. publish/0.0.1). Although tags look a bit like branches, tagged submissions are fixed and cannot be modified at will. See 1.0/2.0/3.0 in the figure above.

HEAD
HEAD points to the latest submission of the current branch

The concepts above are similar, so you can move on, and the higher-order uses of Git will be explained in terms of the specific types of operations below

Operation File

git add
 Add Files to Staging Area
git add -i
 This command opens the interactive subcommand system and you will see the following subcommands

Commands
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
By entering a serial number or initials, you can select the appropriate function, which is explained as follows:

status: functionally similar to git add-i, no bird
update: See git add -u below for details
revert: Removes files that have been added to the staging area from the staging area, similar to update
add untracked: You can add new files to the staging area in a manner similar to update
patch: See git add -p below for details
diff: Compares the differences between the staging area file and the local version Library in a manner similar to update
quit: Exit git add-i command system
Help: view help information
git add -p
Directly into the most useful patch mode of interactive commands

This is the most useful mode for interactive commands, which operate in a manner similar to update. When selected, Git displays the differences between the current contents of these files a n d the local version library. You can then decide whether to a d d these modifications to the staging area at the command line Stage deletion [y,n,q,a,d, /?]? Enter y,n,q,a,d, /? One of the options is how to operate, and the specific functions are explained as follows:

y: accept the changes
n: Ignore modifications
q: Exit the current command
a: add modifications
d: Discard changes
/: Modify content by regular expression matching
?: View Help Information

git add -u
 Go directly into the interactive command update Pattern

It lists the list of files modified or deleted by the workspace first, new files will not be displayed, enter the corresponding list serial number after the command line Update > to indicate that the item is selected, return to continue to select, if selected, return directly to the main command interface

git add --ignore-removal .
Add workspace modified or added file list, deleted files will not be added
git commit
 Submit files from the staging area to the local version Library
git commit -m 'Reason for first line submission'  -m 'Second line submission reason'
Instead of opening the editor, enter a multiline commit reason directly on the command line
git commit -am 'Reasons for submission'
Submit workspace modified or deleted files to the local version library, new files will not be submitted
git commit --amend -m 'Reasons for submission'
Modify the submission reason for the latest submission record
git commit -C HEAD
 Submit current file changes to HEAD Or the history of the current branch ID
git mv
 Move or rename files, directories
git mv a.md b.md -f
 take a.md Rename to b.md ,Add changes to the staging area, plus -f Parameters can be renamed forcefully, instead of using mv a.md b.md Command omitted git add operation
git rm
 Remove files from workspaces and staging areas
git rm b.md
 Remove files from workspaces and staging areas b.md ,Add changes to the staging area at the same time. rm b.md Command omitted git add operation
git rm src/ -r
 Allow directory removal from workspaces and staging areas
git status
git status -s
 View the workspace and staging area file status in a short way, as follows:

 M demo.html
?? test.html
git status --ignored
 View workspace and staging area file status, including ignored files
Operation branch
git branch

View, create, delete branches

git branch -a
 View Branch List on Local and Remote Version Libraries
git branch -r
 View the list of branches on the remote version library, and add -d Parameters can delete branches on remote version Libraries
git branch -D
 Force branch deletion before branch is committed to local version Library
git branch -vv
 View with final submission id,List of local version library branches for information such as reasons for recent submissions
git merge
 Merge other branches into the current branch
git merge --squash
 Will be merged on the branch commit Merge into a new commit Place in the current branch when submission records for merged branches do not need to be preserved
git merge --no-ff
 By default, Git implement"Fast forward consolidation"(fast-farward merge),Will directly Master Branch Pointing Develop Branch, use --no-ff After the parameter, the normal merge is performed, and Master A new node is generated on the branch to ensure a clearer version evolution.
git merge --no-edit
 Merge without conflicts. Instead of manually editing the submission reason, use Git Auto-generated similar Merge branch 'test' Text Direct Submission
git checkout
 Switch Branch
git checkout -b daily/0.0.1
 Establish daily/0.0.1 Branch while switching to this newly created branch
git checkout HEAD demo.html
 From the local version Library HEAD(Or submit ID,Branch name, Tag Name) checked out in history demo.html Overwrite the file in the current workspace, if omitted HEAD Is checked out from the staging area
git checkout --orphan new_branch
 This command will create a new branch with no history at all, but all the latest files on the current source branch are still there. It's good news for OCD patients, but this new branch must be done once git commit It really becomes a new branch.
git checkout -p other_branch
 This command is mainly used to compare the differences between two branches and to provide an interactive interface for selecting further operations. This command not only compares the differences between two branches, but also compares the differences between individual files.

.

git stash
 stay Git The current progress of modifications or deletions is saved in the stack of.When you are doing a feature development in a branch, you are notified to publish the code that was tested yesterday and is OK, but at that time you have added other uncommitted code to the branch, so you can store the uncommitted code in the stack.
git stash
 Save uncommitted files to Git Native Method Stacks

git stash list
 View the list saved on the stack

git stash show stash@{0}
Show one of the records in the stack

git stash drop stash@{0}
Remove one of the records from the stack

git stash pop
 from Git Check out the latest saved record in the stack and remove it from the stack

git stash apply stash@{0}
from Git One of the records is checked out of the stack, but not removed from the stack

git stash branch new_banch
 Check out the last record in the current stack and create a new branch

git stash clear
 Empty all records in the stack

git stash create
 Create a custom stack for the currently modified or deleted file and return a ID,Not actually stored on the stack at this time

git stash store xxxxxx
 take create Returned in method ID put to store Later, a record is actually created in the stack, but the currently modified or deleted file is not removed from the workspace

$ git stash create
09eb9a97ad632d0825be1ece361936d1d0bdb5c7
$ git stash store 09eb9a97ad632d0825be1ece361936d1d0bdb5c7
$ git stash list
stash@{0}: Created via "git stash store".

Operational History
git log
 Show Submission History

git log -p
 Show history with submitted variance comparison

git log demo.html
 display demo.html History of files

git log --since="2 weeks ago"
Show the history from 2 weeks ago to now, other times can be analogous

git log --before="2 weeks ago"
Show the history up to 2 weeks ago, and so on

git log -10
 Show the last 10 records

git log f5f630a..HEAD
 Show from submission ID f5f630a reach HEAD Between records, HEAD Can be empty or other submissions ID

git log --pretty=oneline
 Output a short history on one line

git log --pretty=format:"%h"
Format Output History

Git uses a variety of placeholder s to decide what to display, and I'll pick a few commonly used displays as follows:

%H: commit hash
%h: Shortened commit hash
%T: tree hash
%t: Shortened tree hash
%P: parent hashes
%p: Shortened parent hashes
%an: Author name
%aN: mailmap Author name
%ae: Author Mailbox
%ad: date (--date= Format Developed)
%ar: date, Relative format(1 day ago)
%cn: Submitter name
%ce: Submitter email
%cd: Date of submission (--date= Format Developed)
%cr: Date of submission, Relative format(1 day ago)
%d: ref Name
%s: commit message header
%b: commit information content
%n: Line Break
git cherry-pick
 One or more commits of a merged branch are recorded to the end of the current branch

git cherry-pick 170a305
 Consolidated submission ID 170a305 To the end of the current branch

git reset
 Reset the current branch ( reset)To the specified <commit> perhaps HEAD

git reset --mixed <commit>
--mixed Is the default parameter without parameters, it falls back to a version, preserves file content, and falls back on submitting history

git reset --soft <commit>
The contents of the staging area and the workspace are unchanged, simply changing the HEAD point <commit>

git reset --hard <commit>
Since <commit> Since then, any changes in the workspace have been discarded and HEAD point <commit>

git rebase
 Redefine Branch Version Library Status

git rebase branch_name
 Merge branches, this follows merge Very similar, but there are essential differences, see the following picture:

picture
 Conflicts may need to be resolved before execution during merge git rebase --continue

git rebase -i HEAD~~
Open the text editor and you will see the HEAD reach HEAD~~ Submitted below

pick 9a54fd4 Add to commit Description
pick 0d4a808 Add to pull Description
git revert
 Undo an operation before and after commit and history Will be retained and this withdrawal will be made as a recent submission

git revert HEAD
 Undo the previous submission

git revert HEAD --no-edit
 Undo the previous submission and use the default Revert "xxx" For reasons of submission

git revert -n HEAD
 Add when multiple operations need to be undone -n Parameter so that not every Undo is committed, but when all Undo is complete

git diff
 View file differences between workspaces, staging areas, and local version libraries and use a diagram to explain them

git diff --stat
 adopt --stat Parameters to view change statistics

 test.md | 1 -
 1 file changed, 1 deletion(-)
git reflog
reflog You can view all records of operations for all branches (including commit and reset Operation, deleted commit Record, follow git log The difference is that it cannot view deleted commit Record

Remote Version Library Connection
If the file already exists in the local directory before the GitHub project is initialized, you can initialize the local version library locally and then connect it to the remote version Library

git init
 Generated inside the local directory.git Folder

git remote
git remote -v
 Without parameters, list the remote branches that already exist, plus -v List details, remote after each name url

git remote add origin https://github.com/gafish/gafish.github.com.git
 Add a new remote repository and specify a name to reference the URL

git fetch
 Retrieve updates from remote version libraries back to local version Libraries

git fetch origin daily/0.0.1
 By default, git fetch Retrieve updates from all branches. If you only want to retrieve updates for a particular branch, you can specify the branch name.

Problem investigation
git blame
 View the history of each line of code block in the file

git blame -L 1,10 demo.html
 Intercept demo.html File 1-10 Row History Information

git bisect
 Binary Lookup History, Search BUG

git bisect start
 Start Binary Search

git bisect bad
 Mark current bipartite submission ID For problematic points

git bisect good
 Mark current bipartite submission ID Is the point of no problem

git bisect reset
 Submission found problematic ID Back to original branch

More actions
git submodule
 adopt Git A submodule tracks external version libraries, allows one version library to be stored in another, and maintains two version libraries completely independent

git submodule add https://github.com/gafish/demo.git demo
 take demo Warehouse added as sub-module

git submodule update demo
 Update Submodule demo

git gc
 Function Git Clean up redundant historical snapshots with garbage collection

git archive
 Will be added tag Package and extract a version of

git archive -v --format=zip v0.1 > v0.1.zip
--format Represents the format of the package, such as zip,-v Represents the corresponding tag Name followed by tag Name, such as v0.1. 

summary
This article just explores some of Git's useful features. Git is very powerful, and there are many more features to discover. Limited to the length of this article, let's stop here and use Google for more useful features.

END

Topics: node.js git