This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

preface

Git is a version control tool that almost every programmer will use, but do you have this problem:

  • If you want to configure git information for different projects, you have to configure git config –local once for each new project
  • The feature-Shoot workflow is often used to switch between branches of multiple features or bug fixes, syC each item and Stash temporary changes
  • The Git checkout command is responsible for switching branches and restoring files
  • .

This article introduces some of the new Git features I use daily, and ends with a way to update Git on your MAC.

Differential configuration: IncludeIf (2.13)

As a heavy User of Github, I often post personal projects. There are many differences in git configuration between personal and corporate projects, for example:

  • Username email
  • How to pull code (rebase or merge)
  • .

Git config –global –local git config –local

Starting with version 2.13, we have a new option, IncludeIf, to provide different default Settings for repositories in different directories.

For example, you can create three directories under ~/projects: work, Personal, and thirdparty to store corporate, personal, and third-party projects:

Gitconfig-work,.gitconfig-personal,.gitconfig-personal,.gitconfig-personal,.gitconfig-personal, .gitconfig-thridparty:

Then we added the following configuration in ~/.gitconfig:

Omit...+ [includeIf "gitdir:~/projects/work/"]
+ path = ~/.gitconfig-work
+ [includeIf "gitdir:~/projects/personal/"]
+ path = ~/.gitconfig-personal
+ [includeIf "gitdir:~/projects/thirdparty/"]
+ path = ~/.gitconfig-thirdparty
Copy the code

All git repositories in ~/projects/work/ use the configuration in ~/.gitconfig-work. Git config –local is a higher priority.

For example, in ~/.gitconfig-work, we fill in the user information for work:

[user]
  signingkey = xxxxxxxx
  name = xxxx
  email = [email protected]
Copy the code

Checkout: switch and restore (2.23)

Git Checkout can also switch to a commit or restore a file:

Git checkout -b branchname git checkout -b branchname git checkout Checkout -- filename # discards changes to a file in the workspaceCopy the code

Git 2.23 introduced the git switch and git restore commands

# Switch branches- git checkout branchname
+ git switch branchnameCreate and switch to a new branch- git checkout -b branchname
+ git switch -c branchname # -c -- createGit checkout commitid- git checkout -- filename
+ git restore --filename # equivalent git restore -w --filenameRemove a file from the staging area+ git restored-s -- filename # -s stands for -- passage tenRemove a file from the staging area and discard the workspace changes+ git restore -S -W -- filename
Copy the code

git switchThe command focuses on switching branches,git restoreCommand to move files from staging to workspace or discard them from workspace

Git switch cannot switch to a commit ID

Multi-branch operation: WorkTree (2.5)

The prevailing Git workflow is feature-shoot, meaning that any new feature or bug fix is written in a branch.

When we are developing on a feature branch (branch name feature) and there is a high quality bug that needs to be solved, we usually do the following operations:

  1. usegit stashHide unfinished changes
  2. Create and switch to a bug-fixing branch based on the dev branch (branch name bugfix)
  3. Do the development, and when the test is complete, join the dev branch
  4. Cut back to the feature branch and usegit stash popResume development by restoring the previous changes that were not completed

Many of you will create a clone project to simplify this scenario, but there is another problem: you need to keep both repositories in sync with the remote.

Git 2.5 introduces the git worktree command, which allows branches to be checked out to a folder. For the scenario mentioned above, we need only:

  1. usegit worktree add -b bugfix .. /bugfix devCreate a bugfix branch based on dev to.. Bugfix/directory
  2. cd .. /bugfixEnter this directory for development

The bugfix directory corresponds to the Bugfix branch, which does not interfere with the feature branch. Git in the bugfix directory is a file pointing to the main repository.

Git worktree add -b

Create a branch to the specified path based on the specified branch

MAC uses BREW to update the default Git

In general, git comes with a lower version on the MAC, so you can install or update Git using BREW:

#Install git
brew install git

#To upgrade the git
brew upgrade git
Copy the code

Updating Homebrew starts with the following update: Updating Homebrew… , usually slow, we can skip this process by pressing CTRL + C.

The default git installation is not the brew installation version, so you can use the following command to connect the default Git installation to the brew installation:

brew link --overwrite git
Copy the code

You can view the current git version using git version or git –version. The complete operation is shown as follows:

About me

People love to do things that give them positive feedback. If this article is helpful to you, please click 👍. This is very important to me

I am Flywith24. Only through discussion with others can we know whether our own experience is real or not. By communicating with others on wechat, we can make progress together.

  • The Denver nuggets
  • Github
  • Wechat (official account of the same name) : Flywith24