This article will provide detailed answers to most of the common git instructions you will need to work and study with git.

Feel useful C friends can point to support, your support is my biggest motivation ~

This article is summarized from teacher Liao Xuefeng’s document

1. Preliminary understanding of Git

2. Git add and Git Commit

3. Git status and Git diff

4. Version rollback git reset and check the git log/git relog

5. Undo git checkout and git reset

6. Delete files

7. Manage branches and switches

Use git tag and Git show

  • Create a label
  • Check the label
  • Remove the label
  • Push the label to the remote library

9. Remote warehouse connection

  • Establish a connection to the remote library
  • Output files in the local library to the remote library
  • Deleting a remote library

10. Establish a file transfer mechanism with Github or Gitee

1. Preliminary understanding of Git

First of all, the Git in the local workspace, the staging area and the local store block When we modify the code is called the workspace at ordinary times, when you complete the project to want to save a schedule, so you can to save the code as a staging area, the staging area is similar to the auto-saving word file, when you confirm the code no problem to save registers, You can transfer the code from the temporary storage area to the local warehouse area.

So how do you turn a directory into a repository that Git can manage? (local warehouse, not just the remote warehouse) and the answer is: in the current directory to run the git init command to create a repository, and then all files in this directory can be git management, each file to modify, delete, can track git, so that any time can track history, or can be “reduction” at some time in the future. If you do not see the.git directory, it is because this directory is hidden by default.

2. Git add and Git Commit

Since Github is more widely used, subsequent demonstrations will be conducted on Github

In general, putting a file in your local Git repository is a two-step process.

  • Step one, use commandsgit addTell Git to add a file from the workspace to the staging area.
  • git add <file>
$git add readme.txt// Add the readme.txt file to the staging areaCopy the code

Run the preceding command. If nothing is displayed, the command is running correctly.

  • Step 2: Use commandsgit commitTell Git to add all files in the staging area from the staging area to the local warehouse at one time.
  • git commit -m <message>
$git commit -m "write a readme file"// Add the readme.txt file to the local repositoryCopy the code

-m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m = -m Git commit: 1 file changed: 1 file changed (readme.txt); 2 insertions: insert two lines of content (readme.txt has two lines of content).

Why do you need to add and commit files in Git? Because commit can commit many files at once, you can add different files multiple times, such as:

$git add file.txt $git add file.txt $git add file.txt $git add file.txt $git add file.txt $git add file.txt $git add file.txt $git add file.txt $git add file.txtCopy the code




3. Git status and Git diff

The git status command checks the code status of all files in the current directory. If a file is red, it has been modified but has not been committed yet. The git diff command shows the difference between the working tree and the temporary storage. The diff command shows the difference between the working tree and the temporary storage




4. Version rollback git reset and check the git log/git relog

You keep making changes to the file, and then commit them to the repository, just like when you play a game, you automatically save the state of the game every time you pass a level, and if you don’t pass a level, you can read the state of the previous level. Sometimes, before you hit the Boss, you manually save it so that in case you fail, you can start over from the nearest place. Git is the same. Whenever you think a file has changed enough, you can “take a snapshot”. This is called a commit in Git. Once you mess up your files, or delete files by mistake, you can recover from the last commit and continue working instead of losing months of work. This is a version rollback.

Git commit -m

= “git commit -m

“;

Version 1: Write a Readme file Version 2: Add Distributed Version 3: Append GPL

  • git log

In practice, of course, it’s impossible to remember in your head what changes have been made to a file of thousands of lines. The version control system must have some kind of command that tells us the history. In Git, we use the Git log command to see:

1094 $git log commit adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD - > master) / / the latest modify the Author: Michael Liao <[email protected]> Date: Fri May 18 21:06:15 2018 +0800 append GPL commit e475afc93c209a690c39c13a46716e8fa000c366 Author: Michael Liao <[email protected]> Date: Fri May 18 21:03:36 2018 +0800 add distributed commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 Author: Michael Liao <[email protected]> Date: Fri May 18 20:59:18 2018 +0800 wrote a readme fileCopy the code

If the output is too much, try adding the –pretty=oneline parameter

$git log - pretty = oneline 1094 adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD - > master) append the GPL / / the latest revision e475afc93c209a690c39c13a46716e8fa000c366 add distributed eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 wrote a readme fileCopy the code

: Boom: Good warning: you’ll see a bunch of things like 1094ADB… Commit ID = commit ID = commit ID = commit ID = commit ID = commit ID = commit ID Why does the COMMIT ID need to be such a long string of numbers? Because Git is a distributed version control system, we’ll be looking at multiple people working in the same repository. If everyone uses 1,2,3… As a version number, that must be a conflict. It is not necessary to write the full version number, the first few digits are ok, Git will automatically find the plan to back readme. TXT to the last version, that is, the version of add Distributed, how to do? First, Git must know which version it is. In Git, the current version is referred to as HEAD, which is the latest commit 1094adb… The last version was HEAD^, the last version was HEAD^^, and the last version was HEAD^^. Of course, it’s easier to write 100 ^ for the first 100 versions, so I’ll write HEAD~100.

  • git reset

You can use Git Reset to roll back the current version of Append GPL to the previous version of Add Distributed

$git reset --hard HEAD^// Go to previous version HEAD is now at e475afc add distributedCopy the code

You can also specify a commit version for restore

$git reset --hard 1094a $git reset --hard 1094a $git reset --hard 1094aCopy the code

If you forget the commit version, you can find it with git reflog, which keeps track of every command you run

$ git reflog
e475afc HEAD@{1}: reset: moving to HEAD^
1094adb (HEAD -> master) HEAD@{2}: commit: append GPL
e475afc HEAD@{3}: commit: add distributed
eaadf4e HEAD@{4}: commit (initial): wrote a readme file
Copy the code

:boom: Process diagram:Conclusion:

  • HEADThe version pointed to is the current version, so Git allows you to travel through the history of versions using commands

Git reset –hard commit_id.

  • Before the shuttle, usegit logYou can view the commit history to determine which version you want to fall back to.
  • To return to the future, usegit reflogView command history to determine which version of the future you want to go back to.




5. Undo git checkout and git reset

  • Undo workspace changes

When you have made changes to your workspace file, you can either select Git Add to commit to the staging area or select Git Checkout to undo the changes

$ git status
On branch master
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.txt

no changes added to commit (use "git add" and/or "git commit -a")
Copy the code

The git checkout — readme.txt command is used to undo all changes made to the readme.txt file in the workspace. There are two ways to undo the changes. One is that readme.txt has been modified since it was added to the staging area, and now, undoing the changes returns it to the state it was in when it was last committed to the staging area. In short, it is to return the file to the state it was in when it was last committed or git added.

  • Undo changes to the staging area

When you have made changes to your workspace file and committed to the staging area, you can select Git Commit to your local repository or Git Reset to undo the staging area changes

$git reset HEAD readme.txt $git reset HEAD readme.txt $git reset HEAD readme.txt $git reset HEAD readme.txt $git reset HEAD readme.txtCopy the code

To be seen,git resetThe command can either roll back the version or undo the staging changes to the workspace.

  • Undo modifications to the local warehouse district

When do you need to undo changes in the local warehouse district? Git reset –hard (git reset –hard




6. Delete files

In Git, delete is also a modification operation. In general, you can delete unnecessary files directly from the file manager or using the rm

command

$ rm test.txt
Copy the code

At this point, Git knows that you deleted the file, so the workspace and repository are inconsistent

  • If you’re just deleting files by mistake, then yesgit checkoutInstruction restore (undo this change)
$ git checkout -- test.txt
Copy the code
  • If you do want to remove the file from the repository, use the commandgit rmDelete,git commitSubmitting the modification
$ git rm test.txt
rm 'test.txt'

$ git commit -m "remove test.txt"
[master d46f35e] remove test.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 test.txt
Copy the code

Tip: Manually delete files and then use Git RM and Git add.




7. Manage branches and switches

What do branches do in practice? Let’s say you’re trying to develop a new feature that will take two weeks to complete, and you write 50% of the code in the first week. If you commit it immediately, because the code hasn’t been written yet, the incomplete code base will cause people to stop working on it. If you wait until all the code has been written and commit again, you run a huge risk of losing your daily progress. Now that we have branches, we don’t have to be afraid. You create a branch of your own, no one else can see, continue to work on the original branch, and you work on your own branch, commit as much as you want, until the development is finished, and then merge on the original branch, so that it is safe, and do not affect the work of others.

:boom: Branch related operations with the branch series instruction

  • See the branch

Use the git branch command to list all branches in your local repository, with the current branch preceded by an asterisk

$ git branch
* dev
  master
Copy the code
  • Create and merge branches
  • create

Git checkout dev switch HEAD pointer to dev branch

$ git branch dev
$ git checkout dev
Switched to branch 'dev'
Copy the code

Git checkout -b dev git checkout command with -b to create and switch. This is equivalent to creating a branch and then switching to it

$ git checkout -b dev
Switched to a new branch 'dev'
Copy the code




  • merge

When you make changes to readme.txt on dev branch and want to merge the changes to master branch, there are two steps:

Change git checkout branch name to master branch name

$ git checkout master
Switched to branch 'master'
Copy the code

Merge the dev branch with the name of the branch to be merged

$ git merge dev Updating d46f35e.. b17d20e Fast-forward readme.txt | 1 + 1 file changed, 1 insertion(+)Copy the code

When the merge is complete, you can delete the dev branch. Instead of adding a branch to git checkout dev, add -d for delete

$ git branch -d dev
Deleted branch dev (was b17d20e).
Copy the code

:boom: Branch related operations with the switch series instruction

Note that the branch switch uses git checkout

, while the previous undo change is git checkout —

. The same command has two functions, which can be a little confusing.

In fact, the action of switching branches is easier to understand with switch. Therefore, the latest version of Git provides a new Git switch command to switch branches:

Create and switch to a new dev branch using:

$git switch -c dev// replace git checkout -b devCopy the code

To switch directly to an existing master branch, use:

$git switch master// replace git branch devCopy the code

: boom: summary:

Check out the branch: git branch

Git branch

Git checkout

or git switch

Git checkout -b

or git switch -c

Git merge

Git branch -d

Use git tag and Git show

Preface: Why use labels?

When we release a version, we usually start with a tag in the repository so that we uniquely identify the version at the time of the tag. Whenever you take a commit version of a tag in the future, you take the history version of that tag at that point in time. So, the tag is also a snapshot of the repository. Git has a commit version, so why introduce tags? Because the commit version is something like 6A5819e… A large string of characters, hard to find, discern if 6a5819e… With V1.2, it’s a lot easier. So tag is a meaningful name that’s easy to remember, and it’s tied to a commit version number.




Create a label

git tag <tagname> git tag -a <tagname> -m "blablabla..."



  • Label the entire branch

First, switch git Branch to the branch that needs to be tagged

$ git branch
* dev
  master
$ git checkout master
Switched to branch 'master'
Copy the code

Git tag

$git tag v1.0Copy the code
  • Tag a past commit release

git tag <tagname> <commid id>

The default tag is on the most recent commit. For history, find the commit ID and type it

For example, to label the add merge commit, its commit ID is F52C633

$git tag v0.9f52C633Copy the code

Git tag -a

-m “blablabla…”

$git tag -a v0.1-m "version 0.1 released" 1094ADBCopy the code








Check the label

Git tag You can use the git tag command to view all tags

$git tag v1.0Copy the code

The git show

tags are not listed chronologically, but alphabetically. Git show

$git show v0.9 commit f52c63349bc3c1593499807e5c8e972b82c8f286 (tag: v0.9) Author: Michael Liao <[email protected]> Date: Fri May 18 21:56:54 2018 +0800 add merge diff --git a/readme.txt b/readme.txt ...Copy the code








Remove the label

  • The git tag -d

    command is used to delete a local tag.

  • The git push origin :refs/tags/

    command removes a remote tag.




Push the label to the remote library

  • Git push origin

    can push a local tag;

  • The git push origin –tags command pushes all unpushed local tags.








9. Remote warehouse connection

Two well-known remote repositories are Github and Gitee(code Cloud).

Github: The most popular and widely used international code management platform

Gitee: If you want to experience Git’s speed, you can use Gitee (gitee.com), a local Git hosting service.

Goal: After creating a Git repository locally, you want to create a Git repository on GitHub and synchronize the two repositories remotely.




Establish a connection to the remote library

Git remote add remote repository name [email protected]: remote repository path // Remote repository path as shown in the figure below

$ git remote add origin [email protected]:michaelliao/learngit.git
Copy the code

When the connection is established, the name of the remote library is Origin, which is the default Git name. You can change it to something else, but the name origin is a clear sign of the remote library.



Output files in the local library to the remote library

Git push -u Remote repository Name Indicates the name of the local branch to be pushed

$ git push -u origin master Counting objects: 20, done. Delta compression using up to 4 threads. Compressing objects: 100% (15/15), done. Writing objects: 100% (20/20) and 1.64 KiB | 560.00 KiB/s, done. Total 20 (delta) 5, reused zero (0) delta remote: Resolving deltas. 100% (5/5), done. To github.com:michaelliao/learngit.git * [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'origin'.Copy the code

Git push = git push = git push = git push = git push

Git will not only push the contents of the local master branch to the new remote master branch, but also associate the local master branch with the remote master branch. This will make it easier to push or pull the remote master branch.

From now on, whenever a commit is made locally, you can run the following command:

$git push origin master//Copy the code








Deleting a remote library

Before deleting the git remote database, you are advised to use git remote -v to check the remote database information

$ git remote -v
origin  [email protected]:michaelliao/learn-git.git (fetch)
origin  [email protected]:michaelliao/learn-git.git (push)
Copy the code

Git remote rm

delete the name of the remote repository

$ git remote rm origin
Copy the code

Tips: “Delete” here actually means to remove the binding relationship between local and remote, not to physically delete the remote library. Nothing has changed in the remote library itself. To actually delete the remote repository, log on to GitHub and find the delete button on the background page. : boom: summary:

  • To associate a remote repository, use the command git remote add remote repository name [email protected]: remote repository path

  • When you associate a remote library, you must specify a name for the remote library. Origin is the default common name.

  • After the association, run the git push -u remote repository name command to push the name of the local branch to push all the contents of the master branch for the first time.

  • After that, after each local commit, you can use the git push -u command to push the latest changes to the local branch name to be pushed as long as necessary.








10. Establish a file transfer mechanism with Github or Gitee

There are two ways to transfer between your local Git repository and your GitHub repository, either over HTTPS or SSH

HTTPS: Using HTTPS URL cloning will be more convenient for beginners, copy HTTPS URL and then go to Git Bash and directly use the Clone command to clone to the local, but each fetch and push code needs to input the account and password, This is where the trouble with HTTPS comes in (finding an HTTPS no-password login).

SSH: Using SSH URL cloning requires configuring and adding SSH keys before cloning. Therefore, if you want to use SSH URL cloning, you must be the owner or administrator of the project. Otherwise, you cannot add SSH keys. In addition, by default, no account and password are required for each FETCH and push code in SSH. As for how to establish SSH connection between the local device and Github and Gitee to facilitate push without entering email password, please refer to the following link:

  • How to implement SSH encrypted transmission on Github: SSH on Github platform
  • Github: SSH for Gitee platform








This is my first swastika blog! I hope you enjoyed it and learned a lot from it. If you found this article helpful, please follow me and click the “like” button in the lower right corner. Your support is my greatest motivation ~~