This should take you one step further with Git

  • Install Git
  • The configuration file
    • Look at the config
    • Setting User Information
    • Git alias
    • . Gitignore file
  • Basic warehouse operations
    • Initialize the warehouse
    • Add files to the staging area
    • The status warehouse
    • The diff warehouse
    • Submit the updates
    • Remove the file
    • Move files
    • View Historical commits
    • Cancel the operation
    • The label
  • Branch operation
    • Branch creation
    • Branch switching
    • Branch merge
    • Delete the branch
    • Branch management
    • rebase
    • Remote branch
    • Tracking branch
  • Remote warehouse operation
    • Cloning of warehouse
    • Viewing the Remote Warehouse
    • Adding a remote repository
    • Pull push for remote warehouse
    • Remote repository removal renaming

Install Git

Linux

$ sudo yum install git

or

$ sudo apt-get install git

Mac

Run the git command in Terminal. If no installation method is displayed.

If you like the installer, click here

Look at the config

Git comes with a Git config tool to help set configuration variables that control how Git looks and behaves.

/etc/gitconfigFile: contains the common configuration of each user and their repository on the system.

$ git config --system --list


~/.gitconfig~/.config/git/configFile: Only for the current user.

$ git config --global --list


The config file in the Git directory of the repository currently in use (i.e..git/config) : for that repository.

Each level overrides the configuration of the previous level, so the.git/config configuration variable overrides the configuration variable in /etc/gitconfig.


You can also view the configuration information of a certain item.

  • Form:git config [--global|--system]

Check the final configuration property value $git config user.name

$git config –global user.name


[email protected]

Use the –global option, which will be used for any future operations on the system. If you need to use a different name and mailbox for a particular project, you can set the config file in the project so that the properties in config override the global properties and are not affected in other projects. You can modify the.git/config file in your project by removing the –global parameter

$ git config user.name "name"


Obtain help Manual

Git –help

For example, check the Config manual

$ git help config

Git alias

For example, after an alias is generated, it can be used later with a short representation

$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
Copy the code

Basic warehouse operations

Add files to the staging area

$git add. $git add. $git addCopy the code

The diff warehouse

Diff is useful if you want to know exactly what the file changed

# See what parts of a file have been updated to track but not yet staged, without adding an extra parameter $git diff # Use --staged parameters or --cached $git diff --staged for content added to staged using addCopy the code

Remove the file

If you want to delete a file in the working interval, you need to add it before committing it. Git rm allows you to delete files directly from the workspace and submit them to the staging area.

$git rm fileName $git rm log/\*. Log # Delete all files in the log directory whose names end with Delete all files ending in ~Copy the code

View Historical commits

For the most basic Git log, all updates are listed by commit time, including the submitted SHA-1 checksum, author name, email address, commit time, and commit instructions. Here are some common options.

$git log -p -x $git log -p -x $git log -p $git log --stat # --pretty: $git log --pretty=format:"%h - %an, %ar: %s" # effect :1a99c42 - su, 19 hours ago: Meaningless submissionCopy the code

aboutformatThe writing and meaning of the corresponding common placeholders

options instructions
%H The full hash string of the commit object
%h A short hash string of the submitted object
%T The full hash string of a tree object
%t A short hash string for a tree object
%P The full hash string of the parent object
%p A short hash string of the parent object
%an Name of the author
%ae Email address of the author
%ad Author revision date (can be customized with the –date= option)
%ar Author revision date, in the manner of how long ago
%cn The name of the committer
%ce Email address of the submitter
%cd Submission date
%cr Submission date, as in how long ago
%s Submit instructions

Graphs showing the merge history of branches

$git log --graph --oneline #oneline just makes the output look comfortableCopy the code

Some other git log operations

options instructions
-p Shows the differences between each update in patch format.
–stat Display file modification statistics for each update.
–shortstat Show only the last row number modification in –stat add remove statistics.
–name-only A list of modified files is displayed only after the information has been submitted.
–name-status Displays the list of new, modified, and deleted files.
–abbrev-commit Only the first few characters of SHA-1 are displayed, not all 40 characters.
–relative-date Use a short relative time display (for example, “2 weeks ago”).
–graph Displays branch merge history for ASCII graphical representation.
–pretty Display historical submission information in another format. Available options include oneline, short, Full, Fuller, and format (followed by the specified format).

Look for the occurrence and deletion of a string in the commit

$git log fileName $git log fileName $git log fileNameCopy the code

Ha ha, even if you accidentally write a hidden bug no matter how many months later, it’s really easy to figure out if the problem was caused by writing. $git log -p fileName $git log -p fileName Forget it. Be more careful after you admit it.

There are also options to limit log output

options instructions
-(n) Only the last n commits are displayed
–since, –after Only commits after the specified time are displayed.
–until, –before Displays only commits prior to the specified time.
–author Only submissions related to the specified author are displayed.
–committer Displays only the commits associated with the specified submitter.
–grep Only submissions with the specified keyword are displayed
-S Only submissions that add or remove a keyword are displayed

As a practical example, if you want to view a commit file named Su in a Git repository between November 1 and November 7, 2016, you can use the following query command:

The git log - pretty = "% h - % s", the author = Sue - since = "2016-11-01 \" - before = "2016-11-07"Copy the code

The label

Git can tag a commit in history as important. For example, every time the official version of the launch.

List the tag

$git tag -l 'v2.*' $git tag -l 'v2.*'Copy the code

Create a label

Labels are divided into two types: one is additional labels and the other is lightweight labels.

  • Additional label: Saves the label holder’s information, time and additional information. Finally more along with the label submission
  • Lightweight tagging: Just make a mark on a submission. Store the hash value in a file that holds the label

First, additional labels:

$git tag -a v1.0 -mCopy the code

Lightweight tag

$git tag v1.0Copy the code

Late tagging is to append labels to a commit that has already been committed

$git tag -a v1.1a6b4c97 $git tag -a v1.1a6b4c97 $git tag -a v1.1a6b4c97Copy the code

Shared label

By default, Git Push does not pass tags to remote servers. Git push origin [tagname]

$git push Origin v1.4 # $git push origin --tags = $git push originCopy the code

Check out the tag

You can’t actually check out a tag in Git, but you can create a branch at the tag. The following

$git checkout -b checkbranch2 v2.0Copy the code

View information about labels

You can use git show < tag name > to see the details of the corresponding tag, if a command like git show only shows the content of the last commit

$git show v1.4Copy the code

Branch creation

The creation of a branch is essentially the creation of a pointer that can be moved, and this pointer name is the new branch name

$git branch dev # creates a branch. There's no switching, You can use a command to create and switch to a new branch: $git checkout -b dev # You can use the 'git log' command to view the objects that each branch points to: $git log --oneline --decorate # If you want to see the bifurcation history in graph form, you can do this: $git log --oneline --decorate --graph --allCopy the code

Branch merge

Git merge < target branch to merge > merges the contents of the current branch with the contents of the target branch. If you try to merge the current branch with the target branch, you are essentially moving the current pointer forward. Since there is no staging to resolve in this case, Git calls this a fast-forward.

git merge dev
Copy the code

Branch management

Git branch commands can be created for more than just deleting branches. If you add no parameters, you get a list of all the branches

$git branch # * representative in front of one of the branch, the branch of the current detection is the HEAD pointer points to branch # additional -v parameter can show every branch last commit $git branch - v # - merged: look at those branches have been incorporated into the current $ Git branch - merged # generally show this list in addition to the other branch of *, can remove # - no - merged: check all contain unincorporated branch $git branch - no - mergedCopy the code

Remote branch

$git remote show origin $git branch -aCopy the code

Pull the remote branch

Suppose remote if there is a dev branch and you use fetch to fetch. At this point, a local editable copy will not be automatically generated, in other words, there will be no new dev local branch, just an unmodifiable Origin /dev pointer. You can run git merge origin/dev to merge the work of the remote dev branch into the current branch. If you want to work on your local dev branch, you can build it on top of a remote branch.

$ git checkout -b dev origin/dev
Copy the code

Create a remote branch

Git push < remote repository name > < local branch to push > if you have a new dev branch locally and you want to commit it to the remote dev branch, you can use git push < remote repository name > < local branch to push >

$ git push origin dev
Copy the code

Deleting a Remote Branch

$git push origin --delete devCopy the code

Remote warehouse operation

Viewing the Remote Warehouse

$git remote $git remote $git remote $git remote -v = $git remote -vCopy the code

Git remote show git remote show git remote show git remote show

$ git remote show origin
Copy the code

[email protected]: suzeyu1992 / GitOperateDemo git # If you want to fetch the new remote, just use the old reference name to fetch rpCopy the code

Pull push for remote warehouse

pull

Git pull is the most commonly used git pull directive. The command usually grabs data from the server and automatically tries to merge it into the current branch.

We can and can use Git fetch for local branches where the remote branch tracked is not committed. And then we can do that manually.

push

Git push [remote-name] [branch-name]

$ git push origin master
Copy the code

This command takes effect only if you have write permission to the server and no one has committed it before.

These two feeds are good for pulling or pulling. If the branch has remote server branch tracing, you can omit the warehouse name and branch name. Specifies that must be displayed if not set.