Common commands for configuration management tool GIT

      This article assumes that the reader has a certain foundation for GIT, and is only for reference when you encounter problems at work.

Introduction to Git

important terms

the term

definition

storehouse

Repository

A repository contains all version information, all branch and tag information.

Every copy of a repository in Git is complete. Repositories are where you can get your working copy.

branch

Branches

A branch means an independent code line with its own history . You can generate a new branch from existing code

, this branch is completely independent of the remaining branches. The default branch is often called master . The user can select a branch, and selecting a branch is called checkout.

mark

Tags

A marker refers to the state of a branch at a particular point in time. By tagging, you can easily switch to the state at the time of tagging, such as the code state on the testing branch on January 25 , 2009 .

submit

Commit

After submitting the code, the repository will create a new version. This version can be retrieved at a later date. Every commit includes an author and a committer, and the author and committer can be different people.

URL

URl is used to identify the location of a warehouse

Revise

Revision

Used to represent a version status of the code. Git identifies different versions by ids represented by the SHA1 hash algorithm . Each SHA1 id is a 160 -bit long , hexadecimal identification string . The latest version can be obtained by HEAD . The previous version can be obtained by "HEAD~1" , and so on.

patch

Refers to a text file that contains modifications to source code. You can send this file to someone and he can apply the patch to his local repository.

index

Git needs to associate the changes in the code with the next commit. For example, if you continue to make changes to a file and then want to commit those changes to the next commit, you must commit the file to the index with the git add file command. This way the index keeps a snapshot of all changes.

New files are always added to the index explicitly. For those files that have been submitted before, you can use the -a option in the commit command to submit to the index.

HEAD pointer

How does Git know which branch you are currently working on? It holds a special pointer called HEAD . In Git , it's a pointer to the local branch you're working on (alias for the current branch).

command list

Configuration section

Configure Username and Email

git config --global user.name "Example Surname"

git config --global user.email "[email protected]"

# Set default so that all changes are always pushed to the repository

git config --global push.default "matching"

Get Git configuration information

git config --list

Configure terminal highlighting

git config --global color.status auto

git config --global color.branch auto

ignore specific files

Git can be configured to ignore specific files or folders. These configurations are placed in the .gitignore file. This file can exist in different folders and can contain different file matching patterns. To make Git ignore the bin folder, place a .gitignore file in your home directory with the content bin .

同时Git也提供了全局的配置,core.excludesfile

使用.gitkeep来追踪空的文件夹

Git会忽略空的文件夹。如果你想版本控制包括空文件夹,根据惯例会在空文件夹下放置.gitkeep文件。其实对文件名没有特定的要求。一旦一个空文件夹下有文件后,这个文件夹就会在版本控制范围内。

新增部分

创建仓库

git init

添加文件

git add .

提交更改

git commit -m "Initial commit"

git commit -a -m "These are new changes" #将未添加到索引的文件也提交

git add . && git commit -m "message"

添加远端仓库

git remote add origin ../remote-repository.git

提交到远端

git push origin master

创建一个标记

git tag version1.6 -m 'version 1.6'

创建一个新的分支

git branch testing

git branch <branchname> [<start_point>]

 

#创建新的分支,但是不会切换到新建的分支上,如果没有指定start_point,默认#从HEAD指向的提交创建分支。

git push origin develop    #提交分支到远端

切换分支

git checkout testing

git checkout -b myfeature develop    #创建及切换分支

git checkout -b newbranch  [<start_point>]  #b代表branch的意思,newbranch 是新分支的名称,如果没有指定提交点(start_point),默认从HEAD指向的提交创建分支。

合并两个不同分支的结果

#Merge通过所谓的三路合并来完成。分别来自两个分支的最新commit和两个分支的最新公共commit

git merge testing

创建补丁

git branch mybranch

git checkout mybranch

touch test05

echo "New content for test01" >test01

git add .

git commit -a -m "First commit in the branch"

# Create a patch --> git format-patch master

git format-patch origin/master

# This created patch 0001-First-commit-in-the-branch.patch

git checkout master

git apply 0001-First-commit-in-the-branch.patch

git add .

git commit -a -m "Applied patch"

rm 0001-First-commit-in-the-branch.patch

查看部分

查看修改记录(未提交)

git log

查看提交记录

git diff

查看哪些文件做过变动(未提交)

git status

图形查看变更历史

gitk --all

显示已有的远端仓库

git remote

克隆仓库

git clone [email protected]:vogella/gitbook.git

git clone origine

拉取(Pull)更改

git pull origin master

查看文件内容

less test01.txt

列出所有的标记

Git tag

列出所有本地分支,当前所在的分支前带有*

git branch

看远端仓库的分支

git branch -a

修改部分

更正提交的信息

git commit --amend -m "More changes - now correct"

还原更改(已加入索引)

#如果在你的工作副本中,你创建了不想被提交的文件,你可以丢弃它。

git clean -n

git clean –f    # Now delete

提取老版本的代码(把commit的内容复制到index和工作副本中)

git checkout commit_id       #通过commit IDgit log命令可以查看commit _id

git revert commit_id

还原所有未加入到索引的更改

#如果你还未把更改加入到索引中,你也可以直接还原所有的更改

#即使你删除了一个未添加到索引和提交的文件,你也可以还原出这个文件

git checkout test01.txt    # checkout后如果没有commit id号,就是从index中拷贝数据到工作副本

还原HEAD所指commit

#复制HEAD所指committest01文件到index

git reset HEAD test01

#复制indextest01到工作副本中

git checkout test01

还原文件夹

如果你删除了文件夹且尚未提交,可以通过以下命令来恢复这个文件夹。译者注:即使已经提交,也可以还原

git checkout HEAD -- your_dir_to_restore

恢复到一个标记

git checkout <tag_name>

推送(push)一个分支到远端仓库

git push origin testing

git push origin testing testing

git push origin testing:testing

解决合并冲突

git mergetool

合并多个commit为一个

git rebase -i HEAD~7

Rebasing多个分支

#使用mergerebase,最后的源代码是一样的,但是使用rebase产生的commit历史更加的少,而且历史记录看上去更加的线性

git branch testing

git checkout testing

echo "This will be rebased to master" > test01

git commit -a -m "New feature in branch"

git checkout master

git rebase master

删除部分

删除文件

git commit -a -m "removed"

git add -A . && git commit -m "removed"

删除索引中文件(取消删除动作)。

如果你已经添加一个文件到索引中,但是未提交。可以通过git reset file 命令将这个文件从索引中删除

git reset incorrect.txt

删除分支

git branch -d testing

git push origin :branch-name  #把一个空分支push到server上,相当于删除该分支(冒号前面的空格不能少)。

 

删除远端仓库引用    git remote remove origin

 

管理部分

谁创建了或者是修改了这个文件

git blame filename

以上上个commit信息为起点,创建一条新的分支

git checkout -b mybranch master~1

 附录:

1、github生成非对称密钥对命令:ssh-keygen -t rsa -C  [email protected] 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326482344&siteId=291194637