Git commits all files
1.git add .

Git add xx can be used to add xx files to the staging area, or get add -a if there are many changes. Add all the changed files at once. Notice there’s A period after the -a option. Git add -a adds everything. Git add. Adds new and edited files, excluding deleted files. Git add -u is used to add edited or deleted files, excluding newly added files.

(2),git commit -m“Submit comments”
3,git push originBranch name, commonly used:git push origin master
4. Normally, these three steps are enough.

Git is introduced

  1. Distributed: Git version control system is a distributed system and a command line tool for saving the history state of project source code.
  2. Savepoints: Git savepoints can track files in the source code and get the status of the entire project at a point in time; Multiple source submissions can be merged at this savepoint or reverted to a single savepoint.
  3. Git offline: Git can commit code offline, so it is completely distributed. This means that Git is much faster than tools like SVN, which need to be online and can be slow to commit code if the network is bad.
  4. Git is snapshot-based: while older version control tools like SVN save commit points as patch files, Git commit points to a snapshot of the project at the time of commit, which contains some metadata (author, date, GPG, etc.).
  5. Git branch and merge: The branch model is the most significant feature of Git, because it changes the development mode of developers. Version control tools such as SVN put each branch in a different directory, and Git can switch different branches within the same directory.
  6. Branch immediateness: Branches are created and switched almost simultaneously. Users can upload some branches and hide others locally without having to upload all branches to GitHub.
  7. Branch flexibility: users can create, merge and delete branches at any time. Multiple people can create multiple branches for development and merge branches later, which makes development fast, simple and safe.

Git Workflow

The general working process is as follows:

  1. Clone Git resources from remote repositories as local repositories.
  2. Checkout code from the local repository and make code changes
  3. Commit the code to the staging area before committing.
  4. Commit changes. Commit to local repository. Historical versions of changes are stored in the local repository.
  5. When the changes are complete and you need to share code with team members, you can push the code to a remote repository.

Git workflow is shown below:

The installation of the Git

Download from git-scm.com/download

Git client installation process

1. Double-click the installation program git-2.23.0-64-bit. exe. The following screenshot is displayed:



2. Click “Next”, the screenshot is as follows:



According to your situation, choose the installation directory of the program.

3. Continue to click “Next”, the screenshot is as follows:



Description:

(1) Addition ICONS: Select whether to create desktop shortcuts.

(2) Desktop browsing (Windows Explorer Integration) : to browse the source code, use bash or use Git GUI tools.

(3) Associated configuration file: Whether to associate git configuration file, which mainly displays the style of text editor.

(4) Associated shell script file: whether to associate the script file executed by the Bash command line.

(5) Use TrueType coding: Whether to use TruthType coding in the command line, which is a common code developed by Microsoft and Apple.

4. After selecting, click “Next”, the screenshot is as follows:



Start menu shortcut directory: Set the directory name of the shortcut in the Start menu. You can also choose not to create a shortcut in the Start menu.

5. Click “Next”, the screenshot is as follows:

Select the editor, you can choose Vim, practice instructions



6. Click “Next”, the screenshot is as follows:



Setting environment Variables

Git Bash is the default command line tool to use:

(1) Git native: Use Git Bash command line tool.

(2) The system has CMD: Use the Windows command line tool.

Exe and sort.exe will overwrite the find.exe and sort.exe tools in Windows. If you do not know these tools, do not select them.

7, after selecting, continue to click “Next”, the display is as follows:





Select a line break format for submission

(1) Check the conversion of Windows format to Unix format: convert a Windows-formatted newline to a UNIx-formatted newline before submitting.

(2) Check out the original format to Unix format: no matter what format, must be converted to Unix format newline before submission.

(3) No format conversion: no conversion, check out what, submit what.

8. After selecting, click “Next”, the screenshot is as follows:



9. After selecting, click “Next”, the screenshot is as follows:



10. After selecting, click “Install” to start the installation. The screenshot shows as follows:



11. After the installation, the following screenshots are displayed:



12, After the installation is complete, you need to set the last step, enter the following command line:



Because Git is a distributed version control system, you need to fill in a user name and email address as an identity.

Note:git config --globalWith this parameter, all Git repositories on your machine will use this configuration, although you can specify different usernames and mailboxes for each repository.

With that, our Git client is downloaded and installed.

What is Git

Git is the most advanced distributed version control system in the world.

2. The main differences between SVN and Git

The SVN is a centralized version control system. The version library is stored on the central server in a centralized manner. Therefore, users need to obtain the latest version from the central server first and then work on their own computers. Centralized version control systems must be connected to the Internet to work, if it works on a LAN, bandwidth is big enough, speed is fast enough, if it works on the Internet, if the Internet speed is slow, you wonder.

Git is a distributed version control system, so there is no central server. Everyone’s computer is a complete version library, so there is no need to work online, because the versions are on their own computer. Since everyone’s computer has a complete version library, how can multiple people collaborate? For example, if you change file A on the computer, and someone else changes file A on the computer, you can just push your changes to each other, and then you can see each other’s changes.

Three, how to operate
1. Create a version library.

What is a version library? Git keeps track of all changes and deletions of files in a repository, so that they can be traced at any time in history or “restored” at any time in the future.

So creating a version library is also very simple, as I did belowEDisc – >gitCreate a new one in the directorytestgitVersion of the library.



The PWD command is used to display the current directory.

(1) Pass the commandgit initMake this directory into a repository git can manage like this:



Git is a directory where you can track and manage your own versions of the testGit repository. As follows:



(2) Add the file to the repository.

Must first clear, all of the version control system, can track the change of a text file, such as TXT file, page, all the procedures of code, etc., and such is Git, changes in the version control system can tell you every time, but the pictures and video of these binary file, although can also can be managed by a version control system, but not the change of the trace file, You can only string together every change to the binary file, that is, the image changes from 1KB to 2KB, but version control doesn’t know what the change is.

Let’s first look at the demo as follows:

In the testGit directory, I created a notepad file readme. TXT with the following content: 11111111

Step 1: Use commandsgit add readme.txtAdd to the staging area. As follows:



If, as above, there is no warning, the addition is successful.

Step 2: Use commandsgit commitTell Git to commit the file to the repository.



Now that we have submitted a readme.txt file, we can use the commandgit statusTo see if there are any uncommitted files, see the following:



There are no uncommitted files, but I will continue to change the readme. TXT content, for example, I add a line 22222222 below, continue to usegit statusTake a look at the results, as follows:



The command above tells us that the readme.txt file has been modified, but not committed.

Next I want to see what has been changed in the readme.txt file. You can use the following command:

git diff readme.txtAs follows:



As you can see above, the readme. TXT file is changed from one line 11111111 to two lines, adding one line 22222222.

Now that we know what changes have been made to the readme.txt file, we can safely commit to the repository. Submitting changes is the same 2 steps as submitting filesgit addStep two is:git commit). As follows:

2. Version Rollback

Now that we’ve learned how to modify the file, I’ll go ahead and add another line to the readme.txt file

The content is 333333333333. Continue to run the following command:



Now that I have made three changes to the readme.txt file, I want to check the history. How do I check? We can now use commandsgit logThe demo looks like this:



git logThe command displays the display log from the most recent to the most recent. We can see the last three commits. The most recent one is the increment of 333333. The last time we added 222222, the first time the default is 111111. If the above information is too much, we can use the commandThe git log - pretty = onelineThe demo is as follows:



Now I want to use the version rollback operation. I want to roll back the current version to the previous version. What command do I use? You can use the following two commands. The first is:git reset --hard HEAD^So if you want to go back to the previous version you just putHEAD^toHEAD^^And so on. If you want to go back to the first 100 versions, using the above method is definitely not convenient, we can use the following simple command:git reset --hard HEAD~100Can. The contents of readme. TXT before rollback are as follows:



To roll back to the previous version, run the following command:



The readme. TXT file contains the following contentscat readme.txtTo view



As you can see, the content has fallen back to the previous version. We can keep using itgit logTo view the historical information, see the following:



We have seen the addition of 333333 content, which we have not seen, but now I want to go back to the latest version, such as: how to restore the content of 333333? We can use the following command to roll back the version number:

Git reset -- Hard version numberBut the question now is what if I’ve already turned off the command line once or the version number of 333 content and I don’t know it? How do I know the version number to add 3333 content? You can run the following command to obtain the version number:git reflogThe demo is as follows:



From the above display, we can know that the version number of adding content 3333 is B8a7CF3. We can now use commands

git reset --hard b8a7cf3To recover. The demo is as follows:



As you can see, this is the latest version.

3. Understand the difference between workspace and staging area

Workspace: the directory you see on your computer, such as the files in the testGit directory (except for the.git hidden directory version library). Or the directory files that need to be created again later belong to the workspace category.

Git: Repository: a workspace has a hidden directory. Git does not belong to the workspace, it is a Repository. Git automatically creates the first branch, master, and a pointer to master, HEAD.

We mentioned earlier that there are two steps to using Git to commit files to a repository:

The first step is to use git add to add the file to the staging area.

Step 2: Usegit commitBy committing changes, you essentially commit the entire contents of the staging area to the current branch.



Let’s continue using demo to demonstrate:

Add a new line of “4444444” to readme. TXT and create a new file “test” in the directorygit statusCheck the status as follows:



Now let’s usegit addGit status command to add both files to the staging area, and then use git status to check the status as follows:



And then we can usegit commitCommit once to the branch as follows:

Git undoes and deletes files

1. Undo the modification

For example, I now add a line of content to readme. TXT file: 555555555555



Before I submitted it, I found that adding 55555555555 was wrong, so I had to restore the previous version immediately. Now I can make changes in the following ways:

One: if I know what to delete, I just manually remove the files I need, and thenaddAdd to staging area, finallycommitIt off.

Second: I can revert directly to the previous version as before. Git reset –hard HEAD^

But now I don’t want to use the above two methods, I want to directly use the undo command how to operate? First of all, before we do undo, we can usegit statusCheck the current status. As follows:



Git restores — file to discard workspace changes. Git restores — file to discard workspace changes.

git restore -- readme.txt, as follows:



The commandgit restore -- readme.txtUndo all changes made to the readme. TXT file in the workspace.

  1. The readme. TXT file is not in the staging area, and the readme. TXT file is in the same state as the readme. TXT file.
  2. The other option is readme. TXT, which is already in the staging area and then modified, and then unmodified to return to the status after the staging area was added.

For the second case, I think we’ll go ahead and do a demo, but now if I add a line to readme.txt 66666666666, Igit addAfter I add it to the staging area, I add 7777777, and I want to undo it to get it back to the staging area. As follows:



Note: Commandsgit restore-- readme.txtIn the--It’s important if not--Then the command becomes create branch.

2. Delete files

Suppose I add a file to the testGit directory and commit it. As follows:



As shown in the preceding command, you can delete the file directly from the file directory or run the rm command as shown in the preceding command.rm b.txtIf I want to delete the file from the repository completely, I can do it againcommitCommand to submit, and now the directory looks like this,



As long as nocommitPreviously, what if I wanted to restore this file in the repository?

You can run the following commandgit restore-- b.txt, as follows:



Let’s take a look at our testGit directory and add a file. As follows:

5. Remote warehouse

Now that you’ve created a Git repository locally and want others to collaborate, you can sync the local repository to the remote repository and add a backup of the local repository.

A common remote repository is github: github.com/. Next we’ll show you how to synchronize local code to Github.

Create the repository on Github

The first thing you need to do is create an account on Github. Then create a repository on Github:







Click on the"Create the repository"The button repository is created.

Github supports two synchronization modes: HTTPS and SSH.

It’s easy to use HTTPS without configuration, but you need to enter your username and password every time you submit or download code.

If SSH is used, the client must form a key pair, that is, a public key and a private key. Then you need to put the public key on Githib’s server.

Both methods are used in real development, so we need to master them. Let’s look at SSH mode first.

What is the SSH
SSH is short for Secure Shell and formulated by the Network Working Group of the IETF. SSH is a reliable protocol that provides security for remote login sessions and other network services. The SSH protocol can effectively prevent information leakage during remote management.123
Copy the code
Key based security authentication
When SSH is used for communication, key-based authentication is recommended. You must create a pair of keys for yourself and place the public key on the server you want access to. If you want to connect to an SSH server, the client software sends a request to the server for security authentication using your key. When the server receives the request, it looks for your public key in your home directory on that server and compares it with the public key you sent. If the two keys match, the server encrypts the "challenge" with the public key and sends it to the client software. The client software receives the "challenge" and decrypts it using your private key and sends it to the server.1234
Copy the code
SSH Key Generation

Before you know it, register your Github account first. Since the transfer between your local Git repository and Github repository is encrypted by SSH, you need to set up a few Settings:

Step 1: CreateSSH Key. In the user’s home directory, check to see if there is a.ssh directory, and if so, check to see if there is one in that directoryid_rsaandid_rsa.pubFor these two files, if they exist, skip the following command. If not, open the command line and type the following command:

git bashExecute the command, life public key and private key, command:ssh-keygen -t rsa



After the command is executed, log in to the window local user. SSH directoryC:\Users\ username \.sshGenerate the public and private keys with the following names:

SSH Key Configuration

After a key is generated, you need to configure the local key on Github for smooth access.





In the key sectionid_rsa.pubAdd the file content and click"Add SSH key"Button to complete the configuration.

Synchronize to remote repository

Synchronization to a remote repository can be done using Git bash.

The TestGit repository on GitHub is currently empty, but GitHub tells us that you can clone a new repository from the repository, or associate an existing local repository with it, and then push the contents of the local repository to GitHub.

GitHub prompts you to run the following command in the testGit repository (E:\git\testgit) :

Git remote add origin https://github.com/18515592159zhu/testgit.git git push -u origin master all of the following:

If the following error occurs:



You can run the following command before running the preceding command

git remote rm origin



To push the contents of the local library to remote, usegit pushThe master command actually pushes the current branch master to the remote.

Since the remote library is empty, we push it for the first timemasterWhen I branch, I add- uGit will not only push the contents of the local master branch to the remote new master branch, but also associate the local master branch with the remote master branch, which can simplify the command in the future push or pull. The github user name and password must be entered as follows:



From now on, whenever a local commit is made, the following command can be used:git push origin master

Push the latest changes from your local master branch to Github, and now you have a truly distributed repository.

How do I clone from a remote library

Above we learned how to associate remote libraries with local libraries before remote libraries.

Now let’s say I have something new in the remote library, and I want to clone it locally how do I clone it?

First, log on to Github and create a new repository calledtestgit2. As follows:



As follows, we see:



Now that the remote library is ready, the next step is to use the commandsgit cloneCloned a local library. As follows:



I’m going to build it in my local directoryTestgit2 directory, as shown below:

Create and merge branches

In version backfill, as you already know, Git strings each commit into a timeline, and that timeline is a branch. So far, there is only one timeline. In Git, this branch is called the master branch. HEAD doesn’t technically point to the commit, it points to the master, and the master points to the commit, so HEAD points to the current branch.

First, let’s create the dev branch, and then switch to the dev branch. The operations are as follows:



git checkoutThe command andB -The parameter indicates create and switch, which is equivalent to the following two commands:

git branch dev

git checkout dev

Git branch looks at branches and lists all branches, with the current branch preceded by an asterisk. Then we’ll continue with the demo on the dev branch, for example we’ll now add another line 77777777777 to readme.txt

Read the readme. TXT file and add 77777777 as follows:



Now that the dev branch is complete, let’s switch to the master branch and continue with readme.txt as follows:



Now we can merge the contents of the dev branch into the master branch, using the following commandgit merge devAs follows:



Git merge the git merge command is used to merge the dev branch into the dev branch.

Git is going to Fast forward this merge by pointing the master directly to dev’s current commit, so it’s going to be very Fast.

Now that the merge is complete, we can remove the dev branch as follows:

Summary create and merge branch commands as follows:

Check out the branch: Git Branch

Git branch name

Git checkout name

Git checkout — b name

Git merge name: git merge name

Git branch -d name

1. How to resolve conflicts

So let’s do it one step at a time, so let’s create a new branch, let’s call itfenzhi1Add a line 8888888 to readme.txt and submit as follows:



Again, let’s switch to the master branch and add 99999999 on the last line as follows:



Now we need to merge fenzhi1 on the master branch as follows:



Git with<<<<<<<.= = = = = = =.>>>>>>>Mark out the contents of different branches, where<<<HEADThat’s what the main branch changes,>>>>>fenzhi1It refers to the modified content of fenzhi1, we can modify the following and save:





If I want to see the branch merge, I need to use the commandgit log.The command line is shown as follows:

3. Branch management strategy

Git uses the “Fast Forward” mode to merge branches. In this mode, the branch information will be discarded when the branch is deleted. First of all, let’s do a demo:

  1. Create a dev branch.
  2. Modify the readme.txt content.
  3. Add to staging area.
  4. Switch back to the master branch.
  5. Merge the dev branches using the commandGit merge -- no-ff -m
  6. Viewing Historical Records

Screenshot below:



Branch strategy: First of all, the master branch should be very stable, that is, used to release new versions. It should not be allowed to work on it. It should work on the new dev branch, after it is done, for example, the dev branch should be released, or the dev branch can be merged into the master branch once the code is stable.

Bug branch

In Git, branches are very powerful, and each bug can be fixed by a temporary branch. After the repair is complete, merge the branches, and then delete the temporary branch.

For example, when I receive a 404 bug in development, we can create a 404 branch to fix it, but the current work on the dev branch has not yet been committed. For example:

It’s not that I don’t want to submit it, but we still can’t submit it halfway through the work. For example, I need 2 days to complete the branch bug, but I need 5 hours to complete the issue-404 bug. What to do? Fortunately, Git also provides a Stash feature that can “hide” the current work site and resume work later when the site is restored. As follows:



So now I can fix the bug by creating the issue-404 branch.

First we need to determine which branch to fix the bug on. For example, I am fixing the bug on the master branch. Now I want to create a temporary branch on the Master branch.



Once the fix is complete, switch to the Master branch, complete the merge, and finally remove the issue-404 branch. The demo is as follows:



Now we’re back to working on the Dev branch.



The work area is clean, so where do we go to work site? We can use commandsgit stash listLet’s take a look. As follows:



The workspace is still there, Git has stash content in place, but you need to restore it using the following 2 methods:

  • git stash applyStash content is not deleted after the restore, you need to use the commandgit stash dropTo delete.
  • Another way is to usegit stash popAnd stash content was also deleted when restoring.

The demo is as follows:

Eight, multi-person cooperation

When you clone from a remote repository, Git actually automatically maps the local master branch to the remote master branch, and the default name of the remote repository is Origin.

  • To view information usage for remote librariesgit remote
  • To view details of remote library usageGit remote - v

As shown below:

1. Push branch:

To push a branch, you need to specify a local branch so that Git will push the branch to the corresponding remote branch of the remote library: run the Git push origin master command

For example, my current readme. TXT code on Github looks like this:



The local readme.txt code is as follows:



Now I want to push the locally updated readme. TXT code to the remote library using the following command:



We can see the above, push successfully, we can continue to screenshot the readme. TXT content on Github as follows:



We can see that the push succeeded. If we now want to push to another branch, such as the dev branch, we will still use the same commandgit push origin devWhich branches should be pushed in general?

  • The master branch is the primary branch and therefore synchronizes with the remote at all times.
  • Some bug-fixing branches do not need to be pushed remotely, but can be merged to the master branch and then pushed to the remote branch.
2. Grab branch:

When people collaborate, everyone pushes their changes to the Master branch. Now we can impersonate another colleague, either on another computer (note that SSH keys are added to Github) or on the same computer from another directory, creating a new directory called testgit2

But first I want to push the dev branch to the remote as well, as follows:



Then go to testgit2 and clone the remote library to the local directory as follows:



Now the generated directory looks like this:



Dev branch = dev branch = dev branch = dev branch = dev branch = dev branch = dev branch = dev branch = dev branch = dev branch = dev branch = dev branch = dev branch = dev branch = dev branch = dev branchGit checkout -- b dev origin/dev

Now you can develop on the dev branch and push the dev branch to the remote library. As follows:



The origin/dev branch has been pushed to commit to the origin/dev branch, and I have made changes to the same file in the same place in my directory file, and also tried to push to the remote library, as follows:



From the above we know: push failed, because the latest submission of my friend and I tried to push conflict, the solution is also very simple, the above has reminded us, use firstgit pullGrab the latest submission from Origin /dev, merge it locally, resolve the conflict, and push it again.



Git pull fails because the origin/dev branch is not linked to the origin/dev branch. Git pull fails because the origin/dev branch is linked to the origin/dev branch.



This time,git pullYes, but the merge has conflicts that need to be resolved manually in the same way that conflicts are resolved in branch management. After settlement, submit againpush: We can start with the readme.txt content.



Now that manual work is done, I need to submit it again and push it to the remote library. As follows:



Thus: The general mode of multi-person collaborative work looks like this:

  • First of all, you can try to usegit push origin branch-namePush your own changes.
  • If the push fails, the remote branch needs to be used first because it updates earlier than your local branchgit pullTrying to merge.
  • If the merge has conflicts, they need to be resolved and committed locally. Then usegit push origin branch-namePush.

Git commands are as follows:

  • Mkdir XX (create an empty directory XX refers to the directory name)

  • PWD Displays the path of the current directory.

  • Git init Turns the current directory into a manageable Git repository, generating hidden.git files.

  • Git add XX add XX to the staging area.

  • Git commit -m “XX” commit file -m

  • Git status Displays the status of the repository

  • Git diff XX look at the XX file and change those contents

  • Git log Displays historical records

  • Git reset –hard HEAD^ or git reset –hard HEAD~ rollback to the previous version

    • If you want to go back to 100 versions, useGit reset - hard HEAD ~ 100 )
  • Cat XX View the contents of the XX file

  • Git reflog Specifies the version ID of the historical record

  • Git checkout — XX undoes all changes to the XX file in the workspace.

  • Git rm XX Delete the XX file

  • Git Remote add Origin github.com/18515592159… Associate a remote library

  • Git push -u origin master push the origin master branch to the remote repository

  • Git clone github.com/18515592159… Clone from a remote library

  • Git checkout — b dev create the dev branch and switch to the dev branch

  • Git branch views all current branches

  • Git Checkout Master switch back to master branch

  • Git merge dev Merges the dev branch on the current branch

  • Git branch -d dev Deletes the dev branch

  • Git branch name Creates a branch

  • Git stash hides the current work until the site is restored

  • Git Stash List See a list of all the files that are hidden

  • Git Stash apply restores hidden files, but does not delete the contents

  • Git stash drop Deletes files

  • Git Stash pop Deletes files while restoring them

  • Git remote Displays information about a remote repository

  • Git remote -v displays details about the remote library

  • Git pushes the origin master branch to the corresponding remote branch of the remote library

[link](blog.csdn.net/weixin_4495…)

Big guy’s articles are written well, we can go to see