Have you ever used Git? Maybe you’ve been using it for a while, but its many mysteries are still confusing.

Git is a version control system that is a staple of any software development project. There are two main uses: code backup and code versioning. You can work through the code step by step, saving progress at each step as you need to roll back to a backup copy!

A common problem is that Git is hard to use. Sometimes versions and branches are out of sync, and you spend a lot of time trying to push code! Even worse, not knowing exactly how certain commands work can easily lead to accidentally deleting or overwriting parts of code!

That’s why I wrote this article, to learn how to use Git properly so that you can code together during development!

Installation and configuration

Git installed

First, we have to install Git to use it! Here is a Linux and Windows demo:

Install Git on Linux

We can do this quickly and easily with Yum:

sudo yum install gitCopy the code

Install Git on Windows

Download the latest version of Git from https://git-scm.com/downloads and install it by default.

After installing Git, go to Git->Git Bash in the Start menu and click on it to see what appears to be a command-line window.

Git configuration

You can save your Git username and E-mail so you don’t have to re-enter them in a later Git command.

Configure the account and mailbox of the local warehouse on the command line interface:

$ git config --global user.name "wupx"  
$ git config --global user.email "[email protected]"  Copy the code

A tip that a lot of people don’t know is that you can enable some extra colors for Git to make it easier to read the output of your commands!

git config --global color.ui trueCopy the code

Git basic version control

Initialize the Git

Now we can start versioning the project. Use the CD command to navigate to the directory where you want to set version control in your terminal. Now you can initialize your Git repository like this:

git initCopy the code

This creates a new subdirectory (hidden under Windows) named.git that contains all the necessary repository files (git repository framework). At this point, nothing has been tracked in your project.

Add and submit

To start versioning existing files, you should first trace them and make initial commits. To do this, you first need to add files to Git and attach them to your Git project.

git add <file>
git commit -m 'first commit'Copy the code

Remote backup

Great! You have now started versioning the project locally. If you want to save and back up your project remotely, you’ll need to create a remote repository on GitHub (it’s free!). . So first go to github.com and create a repository. You then use the repository link to add it as the source of your local Git project, where the code is stored.

# # sample git remote add origin \ https://github.com/wupeixuan/repo.git with one of my warehouse as an example the git remote add origin \ https://github.com/wupeixuan/JDKSourceCode1.8.gitCopy the code

You can then continue to push the code to GitHub! Wow, you have successfully backed up your code!

git push origin masterCopy the code

Handle file

State examination (mmse)

The git status command is used to determine which files are in which state. It allows you to see which files are committed and which are not. If you run this command after all files have been committed and pushed, you should see something like the following:

$ git status
# On branch master
nothing to commit (working directory clean)Copy the code

If you add a new file to your project that didn’t previously exist, you should see the untracked file when you run Git status, as follows:

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   README
nothing added to commit but untracked files present (use "git add" to track)Copy the code

Using Git status is useful for quickly checking what you’ve backed up and what you only own locally.

Advanced File Addition

There are more advanced ways to add files to Git to make your workflow more efficient. Instead of trying to find all the files that have changed and add them one by one, we can do the following:

Git add. # Select the changes you want to add (you can make all the changes by Y or N) git add -pCopy the code

Senior submitted

Git commit -m ‘Commit info’ is used to commit files to Git. This is all well and good for submitting short messages, but if you want to do something more subtle, you’ll need to learn more:

### Submit temporary file, Git commit filename -m 'commit message' ### Add file and commit temporary file git commit filename -m 'commit message' ### Commit -am 'insert commit message' ### change your latest commit message git commit --amend 'new commit message' Git rebase -i ### This will provide you with an interface on the core editor: # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shellCopy the code

Branch and merge

The Master branch of the GitHub repository should always contain valid and stable code. However, you may also want to back up some of the code you are currently working on, but that code is not completely stable. Maybe you want to add a new feature, you’re trying and breaking a lot of code, but you still want to keep backups to save progress!

Branching lets you process individual copies of code without affecting the Master branch. When a branch is first created, a full clone of the Master branch is created with the new name. You can then independently modify the code in this new branch, including commit files and so on. Once your new functionality is fully integrated and your code is stable, you can merge it into the Master branch!

branch

This is all you need to create and work on a branch:

Git checkout PRC /dev-wupx git checkout master ## Git branch -d branch_2 ### git branch -D branch_2 ### Viewing all current branches for the repository, including both ### local and remote branches. Great to see if you already have a ### branch for a particular feature In addition, especially on bigger ### projects ### view all current branches of the repository, including local and remote branches. Git branch -a ### View all branches, both local and remote, that have been merged into your current branch. Great for seeing where all the code comes from! Git branch -a --merged ### Git branch -r # merged git branch -r # merged $git rebase $git push Origin branchName push the branch to the remote repository source and track itCopy the code

merge

Great! Now you’ve learned how to branch and start typing code! Once you add new functionality to the branch, you need to merge it back into the Master branch so that your Master has all the latest code functionality.

The method is as follows:

## Now merge your branch into master Git merge PRC /dev-wupxCopy the code

You may have to fix any code conflicts between the branch and the master, but Git will show you how to do all of this after you type the merge command.

Fix errors and backtracking

Error…… They often happen in coding! The important thing is that we can fix them.

Don’t panic! Git gives you everything you need in case you make a mistake in the code you push, rewrite something, or just want to correct what you push.

Git reset HEAD git reset HEAD -- filename # for a specific file Filename git reset HEAD~3 -- filename git reset HEAD~3 -- filename git reset HEAD~3 -- filename git reset HEAD~3 -- filename # for a specific file git reset HEAD~5 -- filename git reset HEAD~5 -- filename # for a specific file ### Switch back to a specific commit, Git reset 0766c053 -- filename git reset 0766c053 -- filename # for a specific file ###  Your code has been reset, but Git will keep copies of the other code in case you need to use it. The --hard flag, on the other hand, tells Git to overwrite all changes in the working directory. git reset --hard 0766c053Copy the code

Useful tips and tricks for Git

We’ve done all the details! Here are some Git tips and tricks that you may find useful for improving your workflow!

search

Git grep 'project' ## Git grep -n 'project' ### git grep -c < line number > 'something' Searches the part of the string with some context (some lines are before and after the string we are looking for) git grep Git -b <number of lines> 'project' ### Git grep -a <number of lines> 'something'Copy the code

Look who wrote what

Git blame 'filename' -l git blame 'filename' -l git commit IDCopy the code

The log

This command displays all information about the submission, such as the submission ID, author, Git log -s 'project' ### Git log --author 'wupx' ### displays a summary of the commit list in the repository. Displays the commit ID and a shorter version of the commit message. Git log --since=yesterday ### Git log --grep "project" --author "wupx"Copy the code