introduce

Git is a version control system, a system that records changes to the contents of one or more files for future reference to specific revisions. It can take your file or entire project back to a point in time, compare the details of file changes, and know exactly who changed the file. Common code management repositories such as Gitee and Github.

The installation

Installation on a MAC:

  • usehomebrewRun directly at the command linebrew install git;
  • Install git using Xcode Command Line Tools and run git Command in Terminalgit --versionIf you have not installed command-line developer tools, you will be prompted to install them.

The git installation package can be downloaded directly from the official git website, which may be very slow.

The initial configuration

// git git This topic describes two methods: $git help <verb> // ===> Obtain the config command using git help config. $git <verb> --h // ===> Obtain the add command using git add --h // The first step is to configure the name and email (corresponding to your gitee or Github account), $git config --global user.name "John Doe" $git config --global user.email [email protected] $ git config --list user.name=John Doe [email protected] color.status=auto color.branch=auto color.interactive=auto color.diff=auto ... $git config user.name John DoeCopy the code

process

A nice diagram (git articles are all over the place and I’m also a steal) clearly explains how Git works:

Remote storage

  • The remote repository where our code is stored. The local repository changes the code and pushes it to the remote repository.
  • Git Clone someone else’s project to local repository
  • The fetch pulls the remote repository to update the local repository

Repository (local Repository)

  • Use commit to store changes to local code to a local repository

Index (Temporary Storage Area)

  • Temporary storage place for files submitted with add, which can be revoked and stored locally

Woekspace (Workspace)

  • We develop locally, and when we make changes, it’s the workspace

Creating a Git repository

If you want to play Git, you must have a git repository first.

  1. Turn local directories that have not yet been versioned into Git repositories;

    • Run the command line tool in the current directory pathgit initWhen initialized, a.git file is generated
    • You can then run git commands to track your file changes
    • Then associate with the remote branch and commit directly
  2. Clone an existing repository from the web.

    • Git clone // is the location of the remote repository
    • Secure Shell (SSH) addresses need to be pre-configured with SSH keys.

The base is held

Daily operation is just that:

  1. Initialize git repository (git init)
  2. Local development, modification;
  3. Add this change to staging (git add)
  4. Commit changes to a local repository (git commit)
  5. Git pull code for pulling remote repositories
  6. Push to remote repository (push to remote repository)

Then take 🌰 to do some hands-on work, along with details of the daily use of each command

Git init (initialize a git local repository)

Open the command line tool and go to your working directory, and someone says hey I didn’t, just play WDNMD.

No direct new (suggested to practice the file first)

As shown in figure I first is to build an empty folder git – test, and then enter the folder to perform git init, execution of the command will tell you have created an empty git local repository, then generate a hidden. Git folder, direct command line to check on my side, of course, if your folders have content is the same operation. Git status command to check the status of your workspace:

Create a test.txt file and typegit statusGit will tell you that nothing has been added to index yet, but there is one file that has not been traced that we just created, so you need to add the file trace.

Git add add changes to staging

Now that we have completed the creation task, add to the staging area directly by executing the command. Git add works like this:

$git add testFile $git add testFileCopy the code

Git add test. TXT: git add test.txt:

Now you can see that I have put test.txt in the staging area. Git rm –cached is used to undo git add.

Git commit -a -m ‘message’

After adding the file, it naturally pushes to the warehouse:

// If you want to make a good comment, you need to make a brief note of what has been changed in this version every time you submit it. As a good habit, you will not sharpen your knife every day. $git commit -mCopy the code

Git commit -m ‘commit test ‘

The result is clear: a file has been modified and pushed to the master branch of the local repository. The git state is now clean and there is nothing to commit in the workspace. The local operation is done and the changes need to be pushed to the remote repository.

Git push (push to remote repository) uses HTTPS

Here take Gitee to do the test, register the account (registered email and account need to be configured to the local, see the initial configuration) after nodding the + sign next to the image to create the warehouse:

Once created, you have this thing:

The reademe file is the description file of the project. If you add something to the remote repository, the local repository will need to pull the synchronization first. It is better not to have a local readme and a remote readme collide at first. I haven’t captured the full image, but there are some basic Git tutorials for you to see. Git remote add origin git remote add origin git remote -v git remote add origin

The first push is executed:

$git push -u origin master = $git push --set-upstream origin master Now you can directly git pull and git push without specifying a branchCopy the code

The first push requires input of user name and password, namely account registration, and the result is shown in the figure below:

After push successful go to webpage remote warehouse refresh can see your push. Using HTTPS, you don’t want to add credentials every time you pull or push, store accounts and passwords like I did next time. Configuration method:

// Execute this command and then pull or push, $git config --global credential.helper osxkeychain // MAC $git config --global credential.helper store //  winCopy the code

The second method is to configure the SSH key

Since my computer has been configured with a key, it will be difficult to deal with the company’s project if the original key is overwritten by the regenerated key. Therefore, I will paste the document address of Gitee’s official website. The operation is very simple and you will be smart enough to see it.

Note: I’m not being lazy

branch

The above operations are based on the single master branch, but enterprise projects are multi-person collaboration, will be divided into master(production branch), test branch and development branch, most of the enterprise approach is similar. So skilled branch operation is necessary, no nonsense straight start.

Start with the shuttle command (all of the following examples can be found here) :

$git branch -r $git branch -r $git branch [branchname] $git branch [branchname] Commit $git branch [branch] [commit] $git branch --track [branch] [remote-branch] $git checkout [branch-name] $git checkout - $git branch --set-upstream =origin/[branchname] [branchname] # Select a COMMIT, $git cherry-pick [commit] $git branch -d [branch-name] # $git push origin --delete [branch-name] $ git branch -dr [remote/branch]Copy the code

Again, use the previous example 🌰 :

Git branch -b: git branch -b: git branch -b: git branch -b: git branch -b Git branch newbranch git checkout newbranch

Note: If you change the current branch workspace or staging area while switching branches, the change will be carried over to your new branch.

Now that we have a local branch dev that migrated from master and is in the branch, we can happily play with our own stuff on the branch and push it directly to the remote repository:

Git push origin dev commits the changes to the local repository. Git push origin dev pushes the changes to the remote repository. After establishing a tracing relationship between a new branch and a remote branch, you can use Git push and git pull to find its corresponding branch. For example, git branch — set-upgrade-to =origin/[branchname] [branchname] or git push -u origin [branchname]. Another is in the case of remote warehouse already has several branches, when we put the project clone down the local is only one main branch by default, if you want to on a remote branch under operation, need to create in the local branch association on the corresponding branch and then to push, otherwise if you create a branch name does not repeat, Push will create a new branch.

Undo modify

After committing a commit, you can undo with git reset –soft HEAD^ or –soft to –hard if you want to undo with add.

// HEAD^ indicates that the previous version was the last commit, which can also be written as HEAD~1. Commit = HEAD~2 // --soft will keep the current workspace changes.Copy the code

Merging branches

Git merge [branchName] git merge [branchName]

Once the merge is successful, just push it to the remote end. If there is a conflict during merge, resolve the conflict before committing the commit.

The last

A few little things:

  1. If you want to exclude some files from git management, you can configure the.gitignore file, but only ignore the files that have not been traced. It is recommended to configure it at the beginning of the project.
# Editor directories and files.idea.vscode *.suo #! Indicates not to ignore, for example, the specified file targetFile/*! TargetFile /readme.md # Some symbols # [] to match a single character in parentheses => targetFile[0-9]/ or targetFile[a-z] #? Represents a placeholder => text1? .txtCopy the code
  1. Pull before each push
  2. A wave of handy vscode operations: