preface

Master Git command is a necessary basis for every programmer, I used to use smartGit tools until I saw that all the big guys are using Git command operation, recall, found some Git commands I forgot, so I wrote this blog, review ~

Github.com/whx123/Java…

Public number: a boy picking up snails

The article directories

  • What is Git?
  • Related theoretical basis of Git
  • Git basic commands for daily development
  • Git advanced branch processing
  • Git advanced conflict handling
  • Git advanced undo and rollback
  • Git advanced tag tag
  • Git some other classic commands

What is Git

Git: Git Git: Git Git

What is version control?

Baidu Encyclopedia definition is maozi ~

Version control is one of the core ideas of software configuration management, which refers to the management of changes in various program codes, configuration files and explanatory documents during software development.

Those years, our graduation thesis, in fact, is the true portrayal of the version of the change… Imagine that version control is the management of changes to these papers

What is a centralized version control system?

What about a centralized version control system? In other words, there is a centrally managed central server that holds the change history of all files, and co-developers connect to this server via a client to synchronize updates or upload their own changes from the server.

What is a distributed version control system?

A distributed version control system is a remote repository that synchronizes all version information to local users. Hee hee, here are three points to elaborate:

  • Users can view all historical version information locally, but occasionally need to update it remotely because other users may have file changes submitted remotely.
  • Users can submit locally even if they are offline, and push to a remote server requires networking.
  • Each user has saved the historical version, so as long as one user’s device is ok, the data can be restored

What is a Git?

Git is a free, open source distributed version control system that can handle project version management from very small to very large projects efficiently and quickly.

Related theoretical basis of Git

  • Git’s four working areas
  • Git workflow
  • Four states of Git files
  • A diagram explains how Git works

Git’s four working areas

Git works in several areas:

  • Workspace: Files and directories seen locally on your computer, under Git version control, form a Workspace.
  • Git/Index is a temporary repository for changes that you have not committed. For example, if you execute git add, the changes are added to this area.
  • Git clone is a remote Repository that you can clone to a local Repository. It is a repository of locally stored versions, with HEAD pointing to the latest repository release. When you execute git commit, the file changes are made to the local repository
  • Remote: Remote warehouse, is similar to github, code cloud and other sites provided by the warehouse, can be understood as the Remote data exchange warehouse ~

Git workflow

This section introduces how to work with Git and how to work with Git. The following section introduces how to work with Git and how to work with Git.

Git’s forward workflow looks like this:

  • Pull file code back from remote repository
  • In the working directory, add, delete, change and check files;
  • Place the changed files in the temporary storage area;
  • Submit the files in the staging area to the local warehouse;
  • Push files from the local repository to the remote repository

Four states of Git files

State Tracked or Untracked depending on whether a file has been versioned or not. Tracked can have three working states: Unmodified, Modified, or Staged

  • Untracked: The file is not yet added to the Git repository and not yet involved in version control, that is, not tracked. In this case, files can be transacted into Staged states by git Add state
  • Unmodified: The file has been added to the Git repository, but has not been modified. A file that is Unmodified becomes Modified. If you move the library using Git remove, it is an Untracked file.
  • Modified: A file must have been Modified. This state can be entered into staged states by using the stage command
  • Passage Ten: Staged state Git commit synchronizes the changes to the repository, and the repository and the local files become Unmodified again.

A diagram explains how Git works

Git basic commands for daily development

  • git clone
  • git checkout -b dev
  • git add
  • git commit
  • git log
  • git diff
  • git status
  • git pull/git fetch
  • git push

This diagram just simulates the basic git command flow

git clone

When we want to do development, the first step is to clone the remote version library to the local

Git clone URL To clone the remote version libraryCopy the code

git checkout -b dev

After cloning, to develop new requirements, we need to create a new development branch, such as dev

Create a branch:

Git checkout -b dev creates the branch dev and switches to itCopy the code

git add

Git add

Git add [dir] Adds a specified directory to the staging area, including subdirectories git add [file1] adds a specified file to the staging areaCopy the code

Now that we have the dev branch, we’re ready to start development. Assuming we’re done with HelloWorld.java, we can add it to the staging area

Git add hello.java add the helloWorld.java file to the staging areaCopy the code

git commit

Git commit

Git commit [file1] -m [message] Commit the specified files in the temporary storage area to the local repository git commit -- amend-m [message] replaces the previous commit with a new commitCopy the code

After adding the helloWorld.java file to the staging area, we can commit to the local repository

Git commit -m 'HelloWorld'Copy the code

git status

Git status, to check the workspace status, use the following command format:

Git status --show-stash Check whether there are stash files in your workspace.Copy the code

Git status can be used when you forget whether you have added your code file to the staging area or committed it to your local repository

git log

Git log git log git log

Git log --oneline displays the commit history of a specified file in a single listCopy the code

Lol, check out the commit history on the Dev branchTo roll back code, often use it to meow submit history

git diff

Git diff filepath git diff filepath Git diff HEAD Filepath Comparison between workspace and temporary git diff HEAD Filepath Comparison between workspace and HEAD git diff branchName Filepath Comparison between files in the current branch and files in branchName Git diff commitId Filepath is different from a commitCopy the code

Git Diff if you want to compare what you’ve changed, you can use Git diff to compare the changes

git pull/git fetch

Git pull pulls updates from all branches of the remote repository and merges them into the local branch. Git pull origin master merge the remote master branch with the local master branch. Git fetch --all Git fetch origin master git fetch originCopy the code

We usually use Git pull to pull the latest code to see, resolve the conflict, and then push the code to the remote repository.

Some partners may be confused about whether to use Git pull or Git fetch

Git pull = git fetch+ git merge For pull, the remote branch is pulled and merged with the local branch. Fetch only pulls the remote branch. You can choose how to merge.

git push

Git push allows you to push local branches, tags, and delete remote branches.

Git push origin master pushes all updates from the local branch to the remote master branch. Git push origin -d < branchName > Delete the remote BranchName branch git pushCopy the code

Git push origin dev~ git push origin dev~ git push origin dev~

Git advanced branch processing

Git usually has multiple branches, such as development branch, regression test branch, and trunk branch, so you need to be familiar with the commands handled by the Git branch

  • git branch
  • git checkout
  • git merge

git branch

Git branch has many uses for creating branches, viewing branches, deleting branches, and so on

New branch:

Git checkout -b dev2 creates a new branch and switches to the new branchCopy the code

View branches:

Git branch View all local branches. Git branch -r View all remote branches. Git branch -a View all remote branches and local branchesCopy the code

Delete branch:

Git branch -d <branchname> Delete the local branchname branchCopy the code

git checkout

Switch branches:

Git Checkout Master switch to master branchCopy the code

git merge

Git merge git merge git merge git merge git merge git merge

Git merge --no-ff origin/dev Git merge --abort And return to the state before mergeCopy the code

For example, after you have developed the requirements, you need to merge the code into the trunk master branch, as follows:

Git advanced conflict handling

Git version control, or many people together, multiple branches coexist, this will inevitably have conflicts ~

Git merges branches and conflicts arise

When merging branches of the same file, if the same line is modified by multiple branches or different people, conflicts will occur during merging.

For example, in the dev branch, we now modify the helloworld.java file, assuming the third line is changed, and commit to the local repository with the following changes:

Public class HelloWorld {public static void main(String[] args) {system.out.println ("Hello, Hello, Hello! ); }}Copy the code

Let’s go back to the master branch and change the same location as helloWorld.java as follows:

Public class HelloWorld {public static void main(String[] args) {system.out.println ("Hello, jay!!" ); }}Copy the code

Then, we commit the change to the master branch and merge the dev branch, as shown in the following figure:

Git conflict Resolution

Git conflict resolution steps are as follows:

  • View the contents of conflicting files
  • Determine which parts of the conflict content to keep and modify the file
  • Resubmit, done

1. View the contents of conflicting files

Git merge error: git merge error: git merge error: git merge

2. Determine which parts of the conflict content to retain and modify the file

  • Git marks the contents of different branches with <<<<<<<, =======, and >>>>>>>,
  • <<<<<<>>>>>> dev refers to changes made on the dev branch

So, we decide which branch to keep, or both, and then modify the conflicting files

3. After modifying the content of the conflict file, we submit it again, conflict done

Git advanced undo and rollback

Git undo and rollback are frequently used in daily work. For example, if you want to undo a modified file to a previous version, or if you want to undo an unwanted commit, git’s undo and rollback operations will be used.

What commands are used to undo or rollback code in each Git workspace, as shown below:

There are Git undo and rollback, generally the following core commands

  • git checkout
  • git reset
  • git revert

git checkout

If the file is still in your workspace and has not yet been added to the staging area, you can use Git Checkout to undo it

Git checkout [file] Discard a file called git checkout. Discard all filesCopy the code

Git checkout — test.txt undoes the test.txt staging

git reset

Git reset

Git reset changes the location of the HEAD to a previous version.

In order to understand git reset, let’s review git version management and HEAD understanding

All Git commits are connected to a timeline called a branch. If the current branch is master, the HEAD pointer generally points to the current branch as follows:

Git reset is not available in version 2.

Git reset

Git Reset can be used in several ways

Git reset HEAD --file Resets a file in the staging area to the current version of the workspace. Git reset -- mixed allows you to revert a commit from a repository to a workspace. Git reset -- hard allows you to revert a commit from a repository completely.Copy the code

Git demo: add git to temporary storage, do not commit

Git checkout file unset git checkout fileCopy the code

Git commit = git commit = git commit = git commit = git commit

Git reset --hard commit_id Want to go back to the past, go back to the past commit_idCopy the code

If your code has already been pushed to a remote repository, you can use reset to rollback it

git log
git reset --hard commit_id
git push origin HEAD --force
Copy the code

git revert

Unlike Git reset, Revert copies the historical version you want to go back to and adds it to the front of the current branch.

Revert before: Revert after:

Of course, you can consider reverting if your code has already been pushed to a remote location

Git log gets the commit ID that you need to rollback a commit. Git Revert -n <commit_id> Undo the specified version. The undo is also saved as a commitCopy the code

Git advanced tag tag

To tag a released version is to tag it with a version number, and if there is a problem with the release, pull it out, fix the bug, and put it back together.

Git tag List all the tags git tag [tag] create a new tag at the current commit git tag [tag] [commit] Create a new tag at the specified commit git tag -d [tag] delete the local tag git push Git checkout -b [branch] [tag] Create a branch that points to a tagCopy the code

Git some other classic commands

git rebase

Rebase, also known as derivatives, is an alternative to consolidation.

Let’s say I have two branches, master and test

      D---E test
      /
 A---B---C---F--- master
Copy the code

Git merge test results

       D--------E
      /          \
 A---B---C---F----G---   test, master
Copy the code

Git rebase test

A - B - D - E - C '-' F - test, masterCopy the code

The benefit of rebase is that you get a more elegant commit tree that sees every commit in a linear manner, with no additional commit nodes. Git pull –rebase. Git pull –rebase

git stash

The stash command can be used to temporarily save and restore changes

Git stash list Displays a list of saved work progress. Git stash pop stash@{num} Restore the work progress to the workspace git stash show: Git stash drop Stash @{num} : Deletes a saved progress git stash clear Deletes all cached Stash files.Copy the code

git reflog

Displays the most recent commits for the current branch

git blame filepath

Git Blame records the change history of a file and the person who changed it

git remote

Git remote Add URL Add a remote repository git remote show [remote] Displays information about a remote repositoryCopy the code

Reference and thanks

Thank you for your article:

  • Learn Git in one hour
  • [Git] (1)– Workspace, staging area, version repository, remote repository
  • Git Reset
  • Git can be used to reset or revert a previous version.
  • Git Undo & Rollback (Git reset and Get Revert)
  • Why git pull –rebase?

The public,

  • Welcome to pay attention to my personal public number, make friends, learn together ha ~
  • If there are any mistakes in this article, please point them out