天天看點

Git - Tutorial官方【轉】

Version 5.8

Copyright © 2009-2015 vogella GmbH

10.08.2015

<a></a>

Git Tutorial

This tutorial explains the usage of the distributed version control system Git via the command line. The examples were done on Linux (Ubuntu), but should also work on other operating systems like Microsoft Windows.

Table of Contents

<dl></dl>

<dd><dl></dl></dd>

Git - Tutorial官方【轉】

A version control system (VCS) allows you to track the history of a collection of files. It supports creating different versions of this collection. Each version captures a snapshot of the files at a certain point in time and the VCS allows you to switch between these versions. These versions are stored in a specific place, typically called a repository.

You may, for example, revert the collection of files to a state from 2 days ago. Or you may switch between versions of your files for experimental features.

The process of creating different versions (snapshots) in the repository is depicted in the following graphic. Please note that this picture fits primarily to Git. Other version control systems like Concurrent Versions System (CVS) don't create snapshots but store file deltas.

Git - Tutorial官方【轉】

VCS are typically used to track changes in source code for a programming language or other text files, like HTML code or configuration files. But a typical version control system can put any type of file under version control, e.g., you may use a VCS to track the different versions of your company logo.

A localized version control system keeps local copies of the files. This approach can be as simple as creating a manual copy of the relevant files. A centralized version control system provides a server software component which stores and manages the different versions of the files and let developer copy (checkout) a certain version onto their individual computer.

Both approaches have the drawback that they have only one single point of failure, e.g., in localized version control systems the individual computer and in a centralized version control systems the server machine. Both system makes it also harder to work in parallel on different features.

In a distributed version control system each user has a complete local copy of a repository on his individual computer. The user can copy an existing repository. This copying process is typically called cloning and the resulting repository can be referred to as a clone.

Every clone contains the full history of the collection of files and a cloned repository has the same functionality as the original repository.

Every repository can exchange versions of the files with other repositories by transporting these changes. This is typically done via a repository running on a server which is, unlike the local machine of a developer, always online. Typically there is a central server for keeping a repository but each cloned repository is a full copy of this repository. The decision which of the copies is considered to be the central server repository is pure convention and not tied to the capabilities of the distributed version control system itself.

Git - Tutorial官方【轉】

Git is currently the most popular implementation of a distributed version control system.

Git originates from the Linux kernel development and was founded in 2005 by Linus Torvalds. Nowadays it is used by many popular open source projects, e.g., the Android or the Eclipse developer teams, as well as many commercial organizations.

The core of Git was originally written in the programming language C, but Git has also been re-implemented in other languages, e.g., Java, Ruby and Python.

The original tooling for Git is based on the command line, i.e., the Git development team provides only tooling for the command line. Most of the following examples are based on the Git command line tooling which offers all capabilities of Git.

The double hyphens (--) in Git separates out any references or other options from a path (usually file names). For example HEAD has a special meaning in Git. Using double hyphens llows you to distinguish between looking at a file called HEAD from a Git commit reference called HEAD.

In case Git can determine the correct parameters and options automatically the double hyphens can be avoided.

The process of copying an existing Git repository is called cloning. After cloning a repository the user has the complete repository with its history on his local machine. Of course, Git also supports the creation of new repositories.

If you want to delete a Git repository, you can simply delete the folder which contains the repository.

If you clone a Git repository, by default, Git assumes that you want to work in this repository as a user. Git also supports the creation of repositories targeting the usage on a server.

bare repositories are supposed to be used on a server for sharing changes coming from different developers. Such repositories do not allow the user to modify locally files and to create new versions for the repository based on these modifications.

non-bare repositories target the user. They allow you to create new changes through modification of files and to create new versions in the repository. This is the default type which is created if you do not specify any parameter during the clone operation.

A local non-bare Git repository is typically called local repository.

A local repository provides at least one collection of files which originate from a certain version of the repository. This collection of files is called the working tree. It corresponds to a checkout of one version of the repository with potential changes done by the user.

The user can change the files in the working tree by modifying existing files and by creating and removing files. Afterwards he can add these changes to the repository.

Once the user has his local repository, he can perform modify files in his working tree and perform version control operations. For example he can create new versions for the files in his Git repository, revert the files to another version stored in the repository, etc.

Git allows the user to synchronize the local repository with other (remote) repositories.

Users with sufficient authorization can send new version in their local repository to to remote repositories via the push operation. They can also integrate changes from other repositories into their local repository via the fetch and pull operation.

Git supports branching which means that you can work on different versions of your collection of files. A branch separates these different versions and allows the user to switch between these versions to work on them.

For example, if you want to develop a new feature, you can create a branch and make the changes in this branch without affecting the state of your files in another branch.

Branches in Git are local to the repository. A branch created in a local repository, which was cloned from another repository, does not need to have a counterpart in the remote repository. Local branches can be compared with other local branches and with remote-tracking branches. A remote-tracking branch proxies the state of a branch in another remote repository.

Git supports the combination of changes from different branches. This allows the developer, for example, to work independently on a branch called production for bugfixes and another branch called feature_123 for implementing a new feature. The developer can use Git commands to combine the changes at a later point in time.

For example, the Linux kernel community used to share code corrections (patches) via mailing lists to combine changes coming from different developers. Git is a system which allows developers to automate such a process.

add selected changes to the something called the staging area and

afterwards you commit the changes stored in the staging area to the repository

This process is depicted in the following graphic.

Git - Tutorial官方【轉】

You need to mark changes in the working tree to be relevant for Git. This process is called staging or to add changes to the staging area.

You add changes in the working tree to the staging area with the <code>git add</code> command. This command stores a snapshot of the specified files in the staging area.

The <code>git add</code> command allows you to incrementally modify files, stage them, modify and stage them again until you are satisfied with your changes.

Older versions of Git used the term index instead of staging area. Staging area is nowadays the preferred term by the Git community. Both terms mean the same thing.

After adding the selected files to the staging area, you can commit these files to add them permanently to the Git repository. Committing creates a new persistent snapshot (called commit or commit object) of the staging area in the Git repository. A commit object, like all objects in Git, is immutable.

The staging area keeps track of the snapshots of the files until the staged changes are committed.

For committing the staged changes you use the <code>git commit</code> command.

Conceptually a commit object (short:commit) represents a version of all files tracked in the repository at the time the commit was created. Commits know their parent(s) and this way capture the version history of the repository.

This commit object is addressable via a hash (SHA-1 checksum). This hash is calculated based on the content of the files, the content of the directories, the complete history of up to the new commit, the committer, the commit message, and several other factors.

This means that Git is safe, you cannot manipulate a file or the commit message in the Git repository without Git noticing that corresponding hash does not fit anymore to the content.

The commit object points to the individual files in this commit via a tree object. The files are stored in the Git repository as blob objects and might be packed by Git for better performance and more compact storage. Blobs are addressed via their SHA-1 hash.

Packing involves storing changes as deltas, compression and storage of many objects in a single pack file. Pack files are accompanied by one or multiple index files which speedup access to individual objects stored in these packs.

A commit object is depicted in the following picture.

Git - Tutorial官方【轉】

The above picture is simplified. Tree objects point to other tree objects and file blobs. Objects which didn't change between commits are reused by multiple commits.

A Git commit object is identified by its hash (SHA-1 checksum). SHA-1 produces a 160-bit (20-byte) hash value. A SHA-1 hash value is typically rendered as a hexadecimal number, 40 digits long.

In a typical Git repository you need fewer characters to uniquely identify a commit object. As a minimum you need 4 characters and in a typical Git repository 5 or 6 are sufficient. This short form is called the abbreviated commit hash or abbreviated hash. Sometimes it is also called the shortened SHA-1 or abbreviated SHA-1.

Several commands, e.g., the <code>git log</code> command can be instructed to use the shortened SHA-1 for their output.

The following table provides a summary of important Git terminology.

Table 1. Important Git terminology

Term

Definition

Branch

A branch is a named pointer to a commit. Selecting a branch in Git terminology is called to checkout a branch. If you are working in a certain branch, the creation of a new commit advances this pointer to the newly created commit.

Each commit knows their parents (predecessors). Successors are retrieved by traversing the commit graph starting from branches or other refs, symbolic references (for example: HEAD) or explicit commit objects. This way a branch defines its own line of descendants in the overall version graph formed by all commits in the repository.

You can create a new branch from an existing one and change the code independently from other branches. One of the branches is the default (typically named master). The default branch is the one for which a local branch is automatically created when cloning the repository.

Commit

When you commit your changes into a repository this creates a new commit objectin the Git repository. This commit object uniquely identifies a new revision of the content of the repository.

This revision can be retrieved later, for example, if you want to see the source code of an older version. Each commit object contains the author and the committer, thus making it possible to identify who did the change. The author and committer might be different people. The author did the change and the committer applied the change to the Git repository. This is common for contributions to open source projects.

HEAD

HEAD is a symbolic reference most often pointing to the currently checked out branch.

Sometimes the HEAD points directly to a commit object, this is called detached HEAD mode. In that state creation of a commit will not move any branch.

If you switch branches, the HEAD pointer points to the branch pointer which in turn points to a commit. If you checkout a specific commit, the HEAD points to this commit directly.

Index

Index is an alternative term for the staging area.

Repository

A repository contains the history, the different versions over time and all different branches and tags. In Git each copy of the repository is a complete repository. If the repository is not a bare repository, it allows you to checkout revisions into your working tree and to capture changes by creating new commits. Bare repositories are only changed by transporting changes from other repositories.

This book uses the term repository to talk about a non-bare repository. If it talks about a bare repository, this is explicitly mentioned.

Revision

Represents a version of the source code. Git implements revisions as commit objects (or short commits). These are identified by an SHA-1 hash.

Staging area

The staging area is the place to store changes in the working tree before the commit. The staging area contains a snapshot of the changes in the working tree (changed or new files) relevant to create the next commit and stores their mode (file type, executable bit).

Tag

A tag points to a commit which uniquely identifies a version of the Git repository. With a tag, you can have a named point to which you can always revert to. You can revert to any point in a Git repository, but tags make it easier. The benefit of tags is to mark the repository for a specific reason, e.g., with a release.

Branches and tags are named pointers, the difference is that branches move when a new commit is created while tags always point to the same commit. Tags can have a timestamp and a message associated with them.

URL

A URL in Git determines the location of the repository. Git distinguishes betweenfetchurl for getting new data from other repositories and pushurl for pushing data to another repository.

Working tree

The working tree contains the set of working files for the repository. You can modify the content and commit the changes as new commits to the repository.

A file in the working tree of a Git repository can have different states. These states are the following:

untracked: the file is not tracked by the Git repository. This means that the file never staged nor committed.

tracked: committed and not staged

staged: staged to be included in the next commit

dirty / modified: the file has changed but the change is not staged

Each commit has zero or more direct predecessor commits. The first commit has zero parents, merge commits have two or more parents, most commits have one parent.

Git - Tutorial官方【轉】

In Git you typically need to address certain commits. For example you want to tell Git to show you all changes which were done in the last three commits. Or you want to see the differences introduced between two different branches.

Git allows addressing commits via commit reference for this purpose.

A commit reference can be a simple reference (simple ref), in this case it points directly to a commit. This is the case for a commit hash or a tag. A commit reference can also be symbolic reference (symbolic ref, symref). In this case it points to another reference (either simple or symbolic). For example HEAD is a symbolic ref for a branch, if it points to a branch. HEAD points to the branch pointer and the branch pointer points to a commit.

A branch points to a specific commit. You can use the branch name as reference to the corresponding commit. You can also use HEAD to reference the corresponding commit.

The Git terminology is parent for ^ and ancestor for ~.

[reference]~1 describes the first predecessor of the commit object accessed via [reference]. [reference]~2 is the first predecessor of the first predecessor of the [reference] commit. [reference]~3 is the first predecessor of the first predecessor of the first predecessor of the [reference] commit, etc.

[reference]~ is an abbreviation for [reference]~1.

For example, you can use the HEAD~1 or HEAD~ reference to access the first parent of the commit to which the HEAD pointer currently points.

[reference]^1 also describes the first predecessor of the commit object accessed via [reference].

For example HEAD^^^ is the same as HEAD~~~ and is the same as HEAD~3.

The difference is that [reference]^2 describes the second parent of a commit. A merge commit typically has two predecessors. HEAD^3 means ‘the third parent of a merge’ and in most cases this won’t exist (merges are generally between two commits, though more is possible).

Git - Tutorial官方【轉】

[reference]^ is an abbreviation for [reference]^1.

You can also specify ranges of commits. This is useful for certain Git commands, for example, for seeing the changes between a series of commits.

The double dot operator allows you to select all commits which are reachable from a commit c2 but not from commit c1. The syntax for this is "c1..c2". A commit A is reachable from another commit B if A is a direct or indirect parent of B.

Think of c1..c2 as all commits as of c1 (not including c1) until commit c2.

For example, you can ask Git to show all commits which happened between HEAD and HEAD~4.

This also works for branches. To list all commits which are in the "master" branch but not in the "testing" branch, use the following command.

You can also list all commits which are in the "testing" but not in the "master" branch.

The triple dot operator allows you to select all commits which are reachable either from commit c1 or commit c2 but not from both of them.

This is useful to show all commits in two branches which have not yet been combined.

On Ubuntu and similar systems you can install the Git command line tool via the following command:

On Fedora, Red Hat and similar systems you can install the Git command line tool via the following command:

To install Git on other Linux distributions please check the documentation of your distribution. The following listing contains the commands for the most popular ones.

Git is also installed by default with the Apple Developer Tools on Mac OS X.

The <code>git config</code> command allows you to configure your Git settings. These settings can be system wide, user or repository specific.

A more specific setting overwrites values in the previous level, i.e., a setting the repository overrides the user setting and a user setting overrides a system wide setting.

You can provide a system wide configuration for your Git settings. A system wide configuration is not very common, most settings are user specific or repository specific as described in the next chapters.

On a Unix based system Git uses the <code>/etc/gitconfig</code> file for this system-wide configuration. To set this up, ensure you have sufficient rights, i.e. root rights, in your OS and use the <code>--system</code> option for the <code>git config</code> command.

Git allows you to store user settings in the <code>.gitconfig</code> file located in the user home directory. This is also called the global Git configuration.

For example Git stores the committer and author of a change in each commit. This and additional information can be stored in the Git user settings.

In each Git repository you can also configure the settings for this repository. User configuration is done if you include the <code>--global</code> option in the <code>git config</code> command.

You can also store repository specific settings in the <code>.git/config</code> file of a repository. Use the <code>--local</code> or use no flag at all. If neither the <code>--system</code> not the <code>--global</code> parameter is used, the setting is specific for the current Git repository.

You have to configure at least your user and email address to be able to commit to a Git repository because this information is stored in each commit.

Configure your user and email for Git via the following command.

If your are using Git in a version below 2.0 you should also execute the following command.

This configures Git so that the <code>git push</code> command pushes only the active branch (in case it is connected to a remote branch, i.e., configured as remote-tracking branches) to your Git remote repository. As of Git version 2.0 this is the default and therefore it is good practice to configure this behavior.

If you pull in changes from a remote repository, Git by default creates merge commits if you pull in divergent changes. This may not be desired and you can avoid this via the following setting.

This setting depends on the individual workflow. Some teams prefer to create merge commits, but the author of this book likes to avoid them.

The following commands enables color highlighting for Git in the console.

By default Git uses the system default editor which is taken from the VISUAL or EDITOR environment variables if set. You can configure a different one via the following setting.

File conflicts might occur in Git during an operation which combines different versions of the same files. In this case the user can directly edit the file to resolve the conflict.

Git allows also to configure a merge tool for solving these conflicts. You have to use third party visual merge tools like tortoisemerge, p4merge, kdiff3 etc. A Google search for these tools help you to install them on your platform. Keep in mind that such tools are not required, you can always edit the files directly in a text editor.

Once you have installed them you can set your selected tool as default merge tool with the following command.

To query your Git settings, execute the following command:

If you want to query the global settings you can use the following command.

Git can be configured to ignore certain files and directories for repository operations. This is configured via one or several <code>.gitignore</code> files. Typically, this file is located at the root of your Git repository but it can also be located in sub-directories. In the second case the defined rules are only valid for the sub-directory and below.

For example, the following <code>.gitignore</code> file tells Git to ignore the <code>bin</code> and <code>target</code> directories and all files ending with a ~.

You can create the <code>.gitignore</code> file in the root directory of the working tree to make it specific for the Git repository.

The <code>.gitignore</code> file tells Git to ignore the specified files in Git commands. You can still add ignored files to the staging area of the Git repository by using the <code>--force</code> parameter, i.e. with the <code>git add --force [paths]</code> command. 

This is useful if you want to add, for example, auto-generated binaries, but you need to have a fine control about the version which is added and want to exclude them from the normal workflow.

It is good practice to commit the local <code>.gitignore</code> file into the Git repository so that everyone who clones this repository has it.

You can also setup a global <code>.gitignore</code> file valid for all Git repositories via the <code>core.excludesfile</code> setting. The setup of this setting is demonstrated in the following code snippet.

The global <code>.gitignore</code> file is only locally available.

You can also create local per-repository rules by editing the <code>.git/info/exclude</code> file in your repository. These rules are not committed with the repository so they are not shared with others.

This allows you to exclude, for example, locally generated files.

Git ignores empty directories, i.e., it does not put them under version control.

If you want to track an empty directory in your Git repository, it is a good practice to put a file called <code>.gitignore</code> in the directory. As the directory now contains a file, Git includes it into its version control mechanism.

The file could be called anything. Others sources recommend to call the file <code>.gitkeep</code>. One problem with this approach is that <code>.gitkeep</code> is unlikely to be ignored by build systems, resulting in the <code>.gitkeep</code> file being copied to the output repository.

In this chapter you create a local Git repository. The comments (marked with #) before the commands explain the specific actions.

Open a command shell for the operations.

The following commands create an empty directory which is used later in this exercise to contain the working tree and the Git repository.

Every Git repository is stored in the <code>.git</code> folder of the directory in which the Git repository has been created. This directory contains the complete history of the repository. The<code>.git/config</code> file contains the configuration for the repository.

The following command creates a Git repository in the current directory.

All files inside the repository folder excluding the <code>.git</code> folder are the working tree for a Git repository.

In this chapter you create several files and place them under version control.

Use the following commands to create several new files.

The <code>git status</code> command shows the working tree status, i.e. which files have changed, which are staged and which are not part of the staging area. It also shows which files have conflicts and gives an indication what the user can do with these changes, e.g., add them to the staging area or remove them, etc.

Run it via the following command.

The output looks similar to the following listing.

Before committing changes to a Git repository you need to mark the changes that should be committed. This is done by adding the new and changed files to the staging area. This creates a snapshot of the affected files.

Afterwards run the <code>git status</code> command again to see the current status.

In case you change one of the staged files before committing, you need to add it again to the staging area to commit the new changes. This is because Git creates a snapshot of these staged files. All new changes must again be staged.

Validate that the new changes are not yet staged.

Add the new changes to the staging area.

Use the <code>git status</code> command again to see that all changes are staged.

After adding the files to the Git staging area, you can commit them to the Git repository. This creates a new commit object with the staged changes in the Git repository and the HEAD reference points to the new commit. The <code>-m</code> parameter allows you to specify the commit message. If you leave this parameter out, your default editor is started and you can enter the message in the editor.

You see an output similar to the following.

Your directory contains the Git repository as well as the Git working tree for your files. This directory structure is depicted in the following screenshot.

Git - Tutorial官方【轉】

If you delete a file you use the <code>git add .</code> command to add the deletion of a file to the staging area. This is supported as of Git version 2.0.

Alternatively you can use the <code>git rm</code> command to delete the file from your working tree and record the deletion of the file in the staging area.

Use the <code>git checkout</code> command to reset a tracked file (a file that was once staged or committed) to its latest staged or commit state. The command removes the changes of the file in the working tree. This command cannot be applied to files which are not yet staged or committed.

If you use <code>git status</code> command to see that there are no changes left in the working directory.

Use this command carefully. The <code>git checkout</code> command deletes the unstaged and uncommitted changes of tracked files in the working tree and it is not possible to restore this deletion via Git.

The <code>git commit --amend</code> command makes it possible to replace the last commit. This allows you to change the last commit including the commit message.

Assume the last commit message was incorrect as it contained a typo. The following command corrects this via the <code>--amend</code> parameter.

You should use the <code>git --amend</code> command only for commits which have not been pushed to a public branch of another Git repository. The <code>git --amend</code> command creates a new commit ID and people may have based their work already on the existing commit. In this case they would need to migrate their work based on the new commit.

Git allows you to define pattern for files which should not be tracked by the Git repository. Create the following <code>.gitignore</code> file in the root of your Git directory to ignore the specified directory and file.

The above command creates the file via the command line. A more common approach is to use your favorite text editor to create the file. This editor must save the file as plain text, e.g., gedit under Ubuntu or Notepad under Windows.

The resulting file looks like the following listing.

Files that are tracked by Git are not automatically removed if you add them to a <code>.gitignore</code> file. Git never ignores files which are already tracked, so changes in the <code>.gitignore</code>file only affect new files. If you want to ignore files which are already tracked you need to explicitly remove them.

The following command demonstrates how to remove the <code>.metadata</code> directory and the <code>doNotTrackFile.txt</code> file from being tracked. This is example code, as you did not commit the corresponding files in your example, the command will not work in your Git repository.

It is good practice to commit the <code>.gitignore</code> file into the Git repository. Use the following commands for this.

Remotes are URLs in a Git repository to other remote repositories that are hosted on the Internet, locally or on the network.

Such remotes can be used to synchronize the changes of several Git repositories. A local Git repository can be connected to multiple remote repositories and you can synchronize your local repository with them via Git operations.

Think of remotes as shorter bookmarks for repositories. You can always connect to a remote repository if you know its URL and if you have access to it. Without remotes the user would have to type the URL for each and every command which communicates with another repository.

It is possible that users connect their individual repositories directly, but a typically Git workflow involves one or more remote repositories which are used to synchronize the individual repository. Typically the remote repository which is used for synchronization is located on a server which is always available.

Git - Tutorial官方【轉】

A remote repository can also be hosted in the local file system.

A remote repository on a server typically does not require a working tree. A Git repository without a working tree is called a bare repository. You can create such a repository with the <code>--bare</code> option. The command to create a new empty bare remote repository is displayed below.

By convention the name of a bare repository should end with the <code>.git</code> extension.

To create a bare Git repository in the Internet you would, for example, connect to your server via the SSH protocol or you use some Git hosting platform, e.g., GitHub.com.

Converting a normal Git repository to a bare repository is not directly support by Git.

You can convert it manually by moving the content of the <code>.git</code> folder into the root of the repository and by removing all others files from the working tree. Afterwards you need to update the Git repository configuration with the <code>git config core.bare true</code> command.

As this is officially not supported, you should prefer cloning a repository with the <code>--bare</code> option.

The <code>git clone</code> command copies an existing Git repository. This copy is a working Git repository with the complete history of the cloned repository. It can be used completely isolated from the original repository.

If you clone a repository, Git implicitly creates a remote named origin by default. The origin remote links back to the cloned repository.

If you create a Git repository from scratch with the <code>git init</code> command, the origin remote is not created automatically.

In this section you create a bare Git repository. In order to simplify the following examples, the Git repository is hosted locally in the filesystem and not on a server in the Internet.

Execute the following commands to create a bare repository based on your existing Git repository.

You add as many remotes to your repository as desired. For this you use the <code>git remote add</code> command.

You created a new Git repository from scratch earlier. Use the following command to add a remote to your new bare repository using the origin name.

You can synchronize your local Git repository with remote repositories. These commands are covered in detail in later sections but the following command demonstrates how you can send changes to your remote repository.

To see the existing definitions of the remote repositories, use the following command.

To see the details of the remotes, e.g., the URL use the following command.

The <code>git push</code> command allows you to send data to other repositories. By default it sends data from your current branch to the same branch of the remote repository.

The <code>git pull</code> command allows you to get the latest changes from another repository for the current branch.

Clone a repository and checkout a working tree in a new directory via the following commands.

Make some changes in your local repository and push them from your first repository to the remote repository via the following commands.

To test the <code>git pull</code> in your example Git repositories, switch to your second repository, pull in the recent changes from the remote repository, make some changes, push them to your remote repository via the following commands.

You can pull in the changes in your first example repository with the following commands.

Git supports several transport protocols to connect to other Git repositories; the native protocol for Git is also called <code>git</code>.

The following command clones an existing repository using the Git protocol. The Git protocol uses the port 9148 which might be blocked by firewalls.

If you have SSH access to a Git repository, you can also use the <code>ssh</code> protocol. The name preceding @ is the user name used for the SSH connection.

Alternatively you could clone the same repository via the <code>http</code> protocol.

As discussed earlier cloning repository creates a remote called <code>origin</code> pointing to the remote repository which you cloned from. You can push changes to this repository via<code>git push</code> as Git uses <code>origin</code> as default. Of course, pushing to a remote repository requires write access to this repository.

You can add more remotes via the <code>git remote add [name] [URL_to_Git_repo] </code>command. For example, if you cloned the repository from above via the Git protocol, you could add a new remote with the name github_http for the http protocol via the following command.

To rename an existing remote repository use the <code>git remote rename</code> command. This is demonstrated by the following listing.

It is possible to use the HTTP protocol to clone Git repositories. This is especially helpful if your firewall blocks everything except HTTP or HTTPS.

For secured SSL encrypted communication you should use the SSH or HTTPS protocol in order to guarantee security.

Git also provides support for HTTP access via a proxy server. The following Git command could, for example, clone a repository via HTTP and a proxy. You can either set the proxy variable in general for all applications or set it only for Git.

The following listing configures the proxy via environment variables.

The following listing configures the proxy via Git config settings.

Git allows you to create branches, i.e. named pointers to commits. You can work on different branches independently from each other. The default branch is most often calledmaster.

A branch pointer in Git is 41 bytes large, 40 bytes of characters and an additional new line character. Therefore, the creating of branches in Git is very fast and cheap in terms of resource consumption. Git encourages the usage of branches on a regular basis.

If you decide to work on a branch, you checkout this branch. This means that Git populates the working tree with the version of the files from the commit to which the branch points and moves the HEAD pointer to the new branch.

The <code>git branch</code> command lists all local branches. The currently active branch is marked with <code>*</code>.

The <code>-v</code> option lists more information about the branches.

In order to list branches in a remote repository use the <code>git branch -r</code> command as demonstrated in the following example.

You can create a new branch via the <code>git branch [newname]</code> command. This command allows to specify the starting point (commit id, tag, remote or local branch). If not specified the commit to which the HEAD reference points is used to create the branch.

To start working in a branch you have to checkout the branch. If you checkout a branch, the HEAD pointer moves to the last commit in this branch and the files in the working tree are set to the state of this commit.

The following commands demonstrate how you switch to the branch called testing, perform some changes in this branch and switch back to the branch called master.

To create a branch and to switch to it at the same time you can use the <code>git checkout</code> command with the <code>-b</code> parameter.

Renaming a branch can be done with the following command.

To delete a branch which is not needed anymore, you can use the following command. You may get an error message that there are uncommited changes if you did the previous examples step by step. Use force delete (uppercase <code>-D</code>) to delete it anyway.

You can push the changes in the current active branch to a remote repository by specifying the target branch. This creates the target branch in the remote repository if it does not yet exist.

To see the difference between two branches you can use the following command.

Git has the option to tag a commit in the repository history so that you find it easier at a later point in time. Most commonly, this is used to tag a certain version which has been released.

If you tag a commit, you create an annotated or lightweight tag.

Git supports two different types of tags, lightweight and annotated tags.

Tags are frequently used to tag the state of a release of the Git repository. In this case they are typically called release tags.

Convention is that release tags are labeled based on the [major].[minor].[patch] naming scheme, for example "1.0.0". Several projects also use the "v" prefix.

The idea is that the patch version is incremented if (only) backwards compatible bug fixes are introduced, the minor version is incremented if new, backwards compatible functionality is introduced to the public API and the major version is incremented if any backwards incompatible changes are introduced to the public API.

You can use the <code>-l</code> parameter in the <code>git tag</code> command to search for a pattern in the tag.

To create a lightweight tag don't use the <code>-m</code>, <code>-a</code> or <code>-s</code> option.

The term build describes the conversion of your source code into another state, e.g., converting Java sources to an executable <code>JAR</code> file. Lightweight tags in Git are often used to identify the input for a build. Frequently this does not require additional information other than a build identifier or the timestamp.

You can create a new annotated tag via the <code>git tag -a</code> command. An annotated tag can also be created using the <code>-m</code> parameter, which is used to specify the description of the tag. The following command tags the current active HEAD.

You can also create tags for a certain commit id.

If you want to use the code associated with the tag, use:

By default the <code>git push</code> command does not transfer tags to remote repositories. You explicitly have to push the tag with the following command.

You can delete tags with the <code>-d</code> parameter. This deletes the tag from your local repository. By default Git does not push tag deletions to a remote repository, you have to trigger that explicitly.

The following commands demonstrate how to push a tag deletion.

The <code>git status</code> command shows the status of the working tree, i.e., which files have changed, which are staged and which are not part of the staging area. It also shows which files have merge conflicts and gives an indication what the user can do with these changes, e.g., add them to the staging area or remove them, etc.

The following commands create some changes in your Git repository.

The <code>git status</code> command shows the current status of your repository and suggests possible actions which the user can perform.

The output of the command looks like the following listing.

The <code>git diff</code> command allows seeing the changes in the working tree compared to the last commit.

In order to test this, make some changes to a file and check what the <code>git diff</code> command shows to you. Afterwards commit the changes to the repository.

To see which changes you have staged, i.e., you are going to commit with the next commit, use the following command.

The <code>git log</code> command shows the history of your repository in the current branch, i.e., the list of commits.

The <code>--oneline</code> parameter fits the output of the <code>git log</code> command in one line.

If you use the <code>--abbrev-commit</code> parameter, the <code>git log</code> command uses shorter versions of the SHA-1 identifier for a commit object but keeps the SHA-1 unique. This parameter uses 7 characters by default, but you can specify other numbers, e.g., <code>--abbrev-commit --abbrev=4</code>.

The <code>graph</code> parameter draws a text-based graphical representation of the branches and the merge history of the Git repository.

To see changes in a file you can use the <code>-p</code> option in the <code>git log</code> command.

You can use the <code>--pretty </code>parameter to configure the output.

This command creates the output.

Git - Tutorial官方【轉】

You can filter the output of the <code>git log</code> command to commits whose commit message, , or reflog entry, respectively, matches the specified regular expression pattern with the<code>--grep=&lt;pattern&gt;</code> and <code>--grep-reflog=&lt;pattern&gt;</code> option.

For example the following command instructs the log command to list all commits which contain the word "workspace" in their commit message.

There is also the <code>--invert-grep=&lt;pattern&gt;</code> option. When this option is used, git log lists the commits that don't match the specified pattern.

You can use the <code>--author=&lt;pattern&gt;</code> or <code>--committer=&lt;pattern&gt;</code> to filter the log output by author or committer. You do not need to use the full name, if a substring matches, the commit is included in the log output.

The following command lists all commits with an author name containing the word "lvogel".

To see the changes introduced by a commit use the following command.

To see the differences introduced between two commits you use the <code>git diff</code> command specifying the commits. For example, the following command shows the differences introduced in the last commit.

To see the files which have been changed in a commit use the <code>git diff-tree</code> command. The <code>name-only </code>tells the command to show only the names of the files.

The <code>git blame</code> command allows you to see which commit and author modified a file on a per line base.

That is very useful to identify the person or the commit which introduced a change.

The following code snippet demonstrates the usage of the <code>git blame</code> command.

The <code>git blame</code> command can also ignore whitespace changes with the <code>-w</code> parameter.

In some cases simply using <code>git blame</code> is not sufficient in order to see all details of certain changes. Therefore you can for exmaple navigate to the filelocation in the target git repository and use the <code>gitk [filename]</code> command to see all commits of a file in a clear UI.

In this screenshot we can see all commits of the <code>ShowViewHandler.java</code> by using the <code>gitk ShowViewHandler.java</code> command:

Git - Tutorial官方【轉】

On linux you can easily install gitk by using the <code>sudo apt-get install gitk</code> command in a terminal.

The <code>git shortlog</code> command summarizes the <code>git log</code> output, it groups all commits by author and includes the first line of the commit message.

The <code>-s</code> option suppresses the commit message and provides a commit count. The <code>-n</code> option sorts the output based on the number of commits by author.

This command also allows you to see the commits done by a certain author or committer.

Git provides the <code>git stash</code> command which allows you to record the current state of the working directory and the staging area and to revert to the last committed revision.

This allows you to pull in the latest changes or to develop an urgent fix. Afterwards you can restore the stashed changes, which will reapply the changes to the current version of the source code.

In general using the stash command should be the exception in using Git. Typically, you would create new branches for new features and switch between branches. You can also commit frequently in your local Git repository and use interactive rebase to combine these commits later before pushing them to another Git repository.

Even if you prefer not to use branches, you can avoid using the <code>git stash</code> command. In this case you commit the changes you want to put aside and amend the commit with the next commit. If you use the approach of creating a commit, you typically put a marker in the commit message to mark it as a draft, e.g., "[DRAFT] implement feature x".

The following commands will save a stash and reapply them after some changes.

It is also possible to keep a list of stashes.

You can also create a branch for your stash if you want to continue to work on the stashed changes in a branch. This can be done with the following command.

If you have untracked files in your working tree which you want to remove, you can use the <code>git clean</code> command.

Be careful with this command. All untracked files are removed if you run this command. You will not be able to restore them, as they are not part of your Git repository.

The following commands demonstrate the usage of the <code>git clean</code> command.

If you have a tracked file in Git, you can always recreate the file content based on the staging area or based on a previous commit. You can also remove staged changes from the staging area to avoid that these changes are included in the next commit. This chapter explain you how you can do this.

You can use the <code>git reset [paths]</code> command to remove staged changes from the staging area. This means that <code>git reset [paths]</code> is the opposite of <code>git add [paths]</code>. It avoids that the changes are included in the next commit. The changes are still available in the working tree, e.g., you will not lose your changes and can stage and commit them at a later point.

In the following example you create a new file and change an existing file. Both changes are staged.

The output of <code>git status</code> command should look similar to the following.

Remove the changes from the staging area with the following command.

Use the <code>git status</code> command to see the result.

Be careful with the following command. It allows you to override the changes in files in your working tree. You will not be able to restore these changes.

Changes in the working tree which are not staged can be undone with <code>git checkout</code> command. This command resets the file in the working tree to the latest staged version. If there are no staged changes, the latest committed version is used for the restore operation.

For example, you can restore the content of a directory called <code>data</code> with the following command.

If you want to undo a staged but uncommitted change, you use the <code>git checkout [commit-pointer] [paths]</code> command. This version of the command resets the working tree and the staged area.

The following demonstrates the usage of this to restore a delete directory.

The additional commit pointer parameter instructs the <code>git checkout</code> command to reset the working tree and to also remove the staged changes.

When you have added the changes of a file to the staging area, you can also revert the changes in the staging area base on the last commit.

Sometimes you want to change the commmit your branch pointer is pointing to. The <code>git reset</code> command allows you to manually set the current HEAD pointer (and its associated branch) to a specified commit. This is for example useful to undo a particular change or to build up a different commit history.

Git - Tutorial官方【轉】

All commits which were originally pointed to by the HEAD pointer and the commit pointed to by HEAD after the reset, are reseted, e.g., not directly visible anymore from the current HEAD and branch pointer.

Via parameters you can decide what you happen to the changes in the working tree and changes which were included in the commits between the original commit and the commit now referred to by the HEAD pointer. As a reminder, the working tree contains the files and the staging area contains the changes which are marked to be included in the next commit. Depending on the specified parameters the <code>git reset</code> command performs the following:

If you specify the <code>--soft</code> parameter, the <code>git reset</code> command moves the HEAD pointer. Changes in the working tree will be left unchanged and all changes which were commited included in commits which are reseted are staged.

If you specify the <code>--mixed</code> parameter (the default), the <code>git reset</code> command moves the HEAD pointer and resets the staging area to the new HEAD. Any file change between the original commit and the one you reset to shows up as modifications (or untracked files) in your working tree. Use this option to remove commits but keep all the work you have done. You can do additional changes, stage changes and commit again. This way you can build up a different commit history.

If you specify the <code>--hard</code> parameter, the <code>git reset</code> command moves the HEAD pointer and resets the staging area and the working tree to the new HEAD. This effectively removes the changes you have done between the original commit and the one you reset to.

Via parameters you can define if the staging area and the working tree is updated. These parameters are listed in the following table.

Table 2. git reset options

Reset

Branch pointer

soft

Yes

No

mixed (default)

hard

The <code>git reset</code> command does not remove untracked files. Use the <code>git clean</code> command for this.

If you specify a path via the <code>git reset [path]</code> command , Git does not move the HEAD pointer. It updates the staging area or also the working tree depending on your specified option.

If you reset the branch pointer of a branch to a certain commit, the <code>git log</code> commands does not show the commits which exist after this branch pointer. For example assume you have two commits A-&gt; B, where B is the commit after A. You if you reset your branch pointer to A, the <code>git log</code> command does not include B anymore.

The <code>git reset --hard</code> command makes the working tree exactly match HEAD.

If you have tracked files with modifications, you lose these changes with the above command.

As a soft reset does not remove your change to your files and index, you can use the <code>git reset --soft</code> command to squash several commits into one commit.

As the staging area is not changed with a soft reset, you keep it in the desired state for your new commit. This means that all the file changes from the commits which were reseted are still part of the staging area.

The <code>git show</code> command allows to see and retrieve files from branches, commits and tags. It allows seeing the status of these files in the selected branch, commit or tag without checking them out into your working tree.

By default, this command addresses a file from the root of the repository, not the current directory. If you want the current directory then you have to use the ./ specifier. For example to address the <code>pom.xml</code> file the current directory use: <code>./pom.xml</code>

The following commands demonstrate that. You can also make a copy of the file.

You can checkout a file from the commit. To find the commit which deleted the file you can use the <code>git log</code> or the <code>git ref-list</code> command as demonstrated by the following command.

The <code>git log</code> command allows you to determine which commit deleted a file. You can use the <code>--</code> option in <code>git log</code> to see the commit history for a file, even if you have deleted the file.

You can revert commits via the <code>git revert</code> command. This command reverts the changes of a commit.

Such commits are useful to document that a change was withdrawn.

The following command demonstrates the usage of the <code>git revert</code> command.

You can check out older revisions of your file system via the git checkout command followed by the commit ID. This command will reset your complete working tree to the status described by this commit.

The commit ID is shown if you enter the <code>git log</code> command.

The following command shows the log.

The following listing shows an example output of a Git log command.

To checkout a specific commit you can use the following command.

If you checkout a commit or a tag, you are in the so-called detached HEAD mode. If you commit changes in this mode, you have no branch which points to this commit. After you checkout a branch you cannot see the commit you did in detached head mode in the <code>git log</code> command.

To find such commits you can use the <code>git reflog</code> command.

Reflog is a mechanism to record the movements of the HEAD and the branches references.

The Git reflog command gives a history of the complete changes of the HEAD reference.

The <code>git reflog</code> command also list commits which you have removed.

There are multiple reflogs: one per branch and one for HEAD. For branches use the <code>git reflog [branch]</code> command and for HEAD use the <code>git reflog</code>or the <code>git reflog HEAD</code> command.

The following example shows how you can use git reflog to reset the current local branch to a commit which isn't reachable from the current branch anymore.

Your local Git repository contains references to the state of the branches on the remote repositories to which it is connected. These local references are called remote-tracking branches.

You can see your remote-tracking branches with the following command.

It is also safe to delete a remote branch in your local Git repository. You can use the following command for that.

The next time you run the <code>git fetch</code> command the remote branch is recreated.

To delete the branch in a remote repository use the following command.

Alternatively you can also use the following command.

For example if you want to delete the branch called testbranch in the remote repository called origin you can use the following command.

Note you can also specify the remote repository's URL. So the following command also works.

Branches can track another branch. This is called to have an upstream branch and such branches can be referred to as tracking branches.

Tracking branches allow you to use the <code>git pull</code> and <code>git push</code> command directly without specifying the branch and repository.

If you clone a Git repository, your local master branch is created as a tracking branch for the master branch of the origin repository (short: origin/master) by Git.

You create new tracking branches by specifying the remote branch during the creation of a branch. The following example demonstrates that.

Instead of using the <code>git checkout</code> command you can also use the <code>git branch</code> command.

The <code>--no-track</code> allows you to specify that you do not want to track a branch. You can explicitly add a tracking branch with the <code>git branch -u</code> command later.

To see the tracking branches for a remote repository (short: remote) you can use the following command.

An example output of this might look as follows.

The <code>git fetch</code> command updates your remote-tracking branches, i.e., it updates the local copy of branches stored in a remote repository. The following command updates the remote-tracking branches from the repository called origin.

The fetch command only updates the remote-tracking branches and none of the local branches and it does not change the working tree of the Git repository. Therefore, you can run the <code>git fetch</code> command at any point in time.

After reviewing the changes in the remote tracking branch you can merge the changes into your local branches or rebase your local branches onto the remote-tracking branch.

Alternatively you can also use the <code>git cherry-pick commit_id</code> command to take over only selected commits.

The <code>git fetch</code> command updates only the remote-tracking branches for one remote repository. In case you want to update the remote-tracking branches of all your remote repositories you can use the following command.

The following code shows a few options how you can compare your branches.

The above commands show the changes introduced in HEAD compared to origin. If you want to see the changes in origin compared to HEAD, you can switch the arguments or use the <code>-R</code> parameter.

You can rebase your current local branch onto a remote-tracking branch. The following commands demonstrate that.

The <code>git pull</code> command performs a <code>git fetch</code> and <code>git merge</code> (or <code>git rebase</code> based on your Git settings). The <code>git fetch</code> does not perform any operations on your local branches. You can always run the fetch command and review the incoming changes.

Git allows you to combine the changes which were created on two different branches. One way to achieve this is merging, which is described in this chapter. Other ways are using rebase or cherry-pick.

If the commits which are merged are direct successors of the HEAD pointer of the current branch, Git simplifies things by performing a so-called fast forward merge. This fast forward merge simply moves the HEAD pointer of the current branch to the tip of the branch which is being merged. You can also merge based on a tag or a commit.

This process is depicted in the following diagram. The first picture assumes that master is checked out and that you want to merge the changes of the branch labeled "branch 1" into your "master" branch. Each commit points to its predecessor (parent).

Git - Tutorial官方【轉】

After the fast-forward merge the HEAD points to the master branch pointing to "Commit 3". The "branch 1" branch points to the same commit.

Git - Tutorial官方【轉】

If commits are merged which are not direct predecessors of the current branch, Git performs a so-called three-way-merge between the latest commits of the two branches, based on the most recent common predecessor of both.

Git - Tutorial官方【轉】

As a result a so-called merge commit is created on the current branch which combines the respective changes from the two branches being merged. This commit points to both of its predecessors.

Git - Tutorial官方【轉】

If multiple common predecessors exist, Git uses recursion to create a virtual common predecessor. For this Git creates a merged tree of the common ancestors and uses that as the reference for the 3-way merge. This is called the recursive merge strategy and is the default merge strategy.

The Git command line tooling also supports the octopus merge strategy for merges of multiple references. With this operation it can merge multiple branches at once.

The <code>ours</code> strategy merges a branch without looking at the changes introduced in this branch. This keeps the history of the merged branch but ignores the changes introduced in this branch.

You typically use the ours merge strategy to document in the Git repository that you have integrated a branch and decided to ignore all changes from this branch.

The <code>git merge</code> command performs a merge. You can merge changes from one branch to the current active one via the following command.

The <code>-s</code> parameter allows you to specify other merge strategies. This is demonstrated with the following command.

For example, you can specify the ours strategy in which the result of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. This is demonstrated with the following command.

Be careful if you use the ours merge strategy, it ignores everything from the branch which is merged.

The usage of the octopus merge strategy is triggered if you specify more than one reference to merge.

The recursive merge strategy (default) allows you to specify flags with the <code>-X</code> parameter. For example you can specify here the <code>ours</code> option. This option forces conflicting changes to be auto-resolved by favoring the local version. Changes from the other branch that do not conflict with our local version are reflected to the merge result. For a binary file, the entire contents are taken from the local version.

The <code>ours</code> option for the recursive merge strategy should not be confused with the ours merge strategy.

A similar option to <code>ours</code> is the <code>theirs</code> option. This option prefers the version from the branch which is merged.

Both options are demonstrated in the following example code.

Another useful option is the <code>ignore-space-change</code> parameter which ignores whitespace changes.

If you prefer to have merge commits even for situations in which Git could perform a fast-forward merge you can use the <code>git merge --no-ff</code> command.

The <code>--no-ff</code> parameter can make sense if you want to record in the history at which time you merged from a maintenance branch to the master branch.

When pulling from a remote repository, prefer doing a rebase to a merge. This will help to keep the history easier to read. A merge commit can be helpful to document that functionality was developed in parallel.

You can use Git to rebase one branch on another one. As described, the <code>merge</code> command combines the changes of two branches. If you rebase a branch called A onto another, the <code>git</code> command takes the changes introduced by the commits of branch A and applies them based on the HEAD of the other branch. This way the changes in the other branch are also available in branch A.

The process is displayed in the following picture. We want to rebase the branch called <code>branch_1</code> onto master.

Git - Tutorial官方【轉】

Running the rebase command creates a new commit with the changes of the branch on top of the master branch.

Git - Tutorial官方【轉】

Performing a rebase does not create a merge commit. The final result for the source code is the same as with merge but the commit history is cleaner; the history appears to be linear.

Rebase can be used to forward-port a feature branch in the local Git repository onto the changes of the master branch. This ensures that your feature is close to the tip of the upstream branch until it is finally published.

If you rewrite more than one commit by rebasing, you may have to solve conflicts per commit. In this case the merge operations might be simpler to be performed because you only have to solve merge conflicts once.

Also, if your policy requires that all commits result in correct software you have to test all the rewritten commits since they are "rewritten" by the rebase algorithm. Since merge/rebase/cherry-pick are purely text-based and do not understand the semantics of these texts they can end up with logically incorrect results. Hence, it might be more efficient to merge a long feature branch into upstream instead of rebasing it since you only have to review and test the merge commit.

You should avoid using the Git rebase operation for changes which have been published in other Git repositories. The Git rebase operation creates new commit objects, this may confuse other developers using the existing commit objects.

Assume that a user has a local feature branch and wants to push it to a branch on the remote repository. However, the branch has evolved and therefore pushing is not possible. Now it is good practice to fetch the latest state of the branch from the remote repository. Afterwards you rebase the local feature branch onto the remote tracking branch. This avoids an unnecessary merge commit. This rebasing of a local feature branch is also useful to incorporate the latest changes from remote into the local development, even if the user does not want to push right away.

Rebasing and amending commits is safe as long as you do not push any of the changes involved in the rebase. For example, when you cloned a repository and worked in this local repository. Rebasing is a great way to keep the history clean before contributing back your modifications.

In case you want to rewrite history for changes you have shared with others you need to use the <code>-f</code> parameter in your <code>git push</code> command and subsequently your colleagues have to use fetch -f to fetch the rewritten commits.

The following demonstrates how to perform a rebase operation.

The following commands create several commits which will be used for the interactive rebase.

We want to combine the last seven commits. You can do this interactively via the following command.

This command opens your editor of choice and lets you configure the rebase operation by defining which commits to pick, squash or fixup.

The following listing shows an example of the selection, we pick the last commit, squash 5 commits and fix the sixth commit. The listing uses the long format of the commands (for example <code>fixup</code> instead of the short form <code>f</code>) for better readability.

The <code>git cherry-pick</code> command allows you to select the patch which was introduced with an individual commit and apply this patch on another branch. The patch is captured as a new commit on the other branch.

This way you can select individual changes from one branch and transfer them to another branch.

The new commit does not point back to its original commit so do not use cherry-pick blindly since you may end up with several copies of the same change. Most often cherry-pick is either used locally (to emulate an interactive rebase) or to port individual bug fixes done on a development branch into maintenance branches.

In the following example you create a new branch and commit two changes.

You can check the commit history, for example, with the <code>git log --oneline </code>command.

The following command selects the first commit based on the commit ID and applies its changes to the master branch. This creates a new commit on the master branch.

The <code>cherry-pick</code> command can be used to change the order of commits. <code>git cherry-pick</code> also accepts commit ranges for example in the following command.

If things go wrong or you change your mind, you can always reset to the previous state using the following command.

A conflict during a merge operation occurs if two commits from different branches have modified the same content and Git cannot automatically determine how both changes should be combined when merging these branches.

This happens for example if the same line in a file has been replaced by two different commits.

If a conflict occurs, Git marks the conflict in the file and the programmer has to resolve the conflict manually.

After resolving it, he adds the file to the staging area and commits the change. These steps are required to finish the merge operation.

Sometimes if a conflict occurs the developer does not want to solve the conflict. He decides that he wants to keep the original version or the new version of the file.

For this, there is the <code>--theirs</code> and the <code>--ours</code> options on the <code>git checkout</code> command. The first option keeps the version of the file that you merged in, and the second option keeps the version before the merge operation was started.

In the following example you create a conflict during a merge operation.

The following steps create a merge conflict. It assumes that repo1 and repo2 have the same origin repository defined.

As this push would not result in a non-fast-format merge, you receive an error message similar to the following listing.

To solve this, you need to integrate the remote changes into your local repository. In the following listing the <code>git fetch</code> command gets the changes from the remote repository. The <code>git merge</code> command tries to integrate it into your local repository.

This creates the conflict and a message similar to the following.

The text above the ======= signs is the conflicting change from your current branch and the text below is the conflicting change from the branch that you are merging in.

To solve the merge conflict you edit the file manually. The following listing shows a possible result.

Afterwards add the affected file to the staging area and commit the result. This creates the merge commit. You can also push the integrated changes now to the remote repository.

Instead of using the <code>-m</code> option in the above example you can also use the <code>git commit</code> command without this option. In this case the command opens your default editor with the default commit message about the merged conflicts. It is good practice to use this message.

Alternatively, you could use the <code>git mergetool</code> command. <code>git mergetool</code> starts a configurable merge tool that displays the changes in a split screen. Some operating systems may come with a suitable merge tool already installed or configured for Git.

During a rebase operaton, several commits are applied onto a certain commit. If you rebase a branch onto another branch, this commit is the last common ancestor of the two branches.

For each commit which is applied it is possible that a conflict occurs.

If a conflict occurs during a rebase operation, the rebase operation stops and the developer needs to resolve the conflict. After he has solved the conflicts, the developer instructs Git to continue with the rebase operation.

A conflict during a rebase operation is solved similarly to the way a conflict during a merge operation is solved. The developer edits the conflicts and adds the files to the Git index. Afterwards he continues the rebase operation with the following command.

To see the files which have a rebase conflict use the following command.

You can also skip the commit which creates the conflict.

You can also abort a rebase operation with the following command.

If a file is in conflict you can instruct Git to take the version from the new commit of the version of commit onto which the new changes are applied. This is sometimes easier than to solve all conflicts manually. For this you can use the <code>git checkout</code> with the <code>--theirs</code> or <code>--ours</code> flag. During the conflict <code>--ours</code> points to the file in the commit onto which the new commit is placed, i.g., using this skips the new changes for this file.

Therefore to ignore the changes in a commit for a file use the following command.

To take the version of the new commit use the following command.

An alias in Git allows you to create a short form of one or several existing Git commands. For example, you can define an alias which is a short form of your own favorite commands or you can combine several commands with an alias.

The following defines an alias to see the staged changes with the new <code>git staged</code> command.

Or you can define an alias for a detailed <code>git log</code> command. The following command defines the <code>git ll </code>alias.

You can also run external commands. In this case you start the alias definition with a <code>!</code> character. For example, the following defines the <code>git ac</code> command which combines<code>git add . -A</code> and <code>git commit</code> commands.

In the past msysGit for Windows had problems with an alias beginning with <code>!</code>, but it has been reported that this now works with msysGit, too .

Git allows you to include other Git repositories into a Git repository. This is useful in case you want to include a certain library in another repository or in case you want to aggregate certain Git repositories.

Git calls these included Git repositories submodules. Git allows you to commit, pull and push to these repositories independently.

You add a submodule to a Git repository via the <code>git submodule add</code> command. The <code>git submodule init</code> command creates the local configuration file for the submodules if this configuration does not exist.

To pull in changes into a Git repository including the changes in submodules, you can use the <code>--recurse-submodules</code> parameter in the <code>git pull command</code>.

Use the <code>git submodule update </code>command to set the submodules to the commit specified by the main repository.

The fact that submodules track commits and not branches frequently leads to confusion. That is why Git 1.8.2 added the option to also track branches. Read the following sections to learn more about this.

Since its 1.8.2 release the Git system allows tracking a branch in a submodule. To track branches you specify the branch with the <code>-b</code> parameter during the <code>submodule add</code>command.

This allows you use to use <code>--remote</code> parameter in the <code>git submodule update</code> command.

Without any additional parameter, submodules are tracked by commit, i.e., the main Git repository remembers a certain commit of the submodule.

The <code>git submodule update</code> command sets the Git repository of the submodule to that particular commit. The submodule repository tracks its own content which is nested into the main repository. This main repository refers to a commit of the nested submodule repository.

This means that if you pull in new changes into the submodules, you need to create a new commit in your main repository in order to track the updates of the nested submodules.

If you update your submodule and want to use this update in your main repository, you need to commit this change in your main repository. The <code>git submodule update</code>command sets the submodule to the commit referred to in the main repository.

The following example shows how to update a submodule to its latest commit in its master branch.

Another developer can get the update by pulling in the changes and running the submodules update command.

The <code>git bisect</code> command allows you to run a binary search through the commit history to identify the commit which introduced an issue. You specify a range of commits and a script that the <code>bisect</code> command uses to identify whether a commit is good or bad.

This script must return 0 if the condition is fulfilled and non-zero if the condition is not fulfilled.

Create a new Git repository, create the <code>text1.txt</code> file and commit it to the repository. Do a few more changes, remove the file and again do a few more changes.

We use a simple shell script which checks the existence of a file. Ensure that this file is executable.

Afterwards use the <code>git bisect</code> command to find the bad commit. First you use the <code>git bisect start</code> command to define a commit known to be bad (showing the problem) and a commit known to be good (not showing the problem).

Afterwards run the bisect command using the shell script.

The above commands serve as an example. The existence of a file can be easier verified with the <code>git bisect</code> command: <code>git bisect run test -f test1.txt</code>

The <code>git filter-branch</code> command allows you to rewrite the Git commit history for selected branches and to apply custom filters on each revision. This creates different hashes for all modified commits. This implies that you get new IDs for all commits based on any rewritten commit.

The command allows you to filter for several values, e.g., the author, the message, etc. For details please see the following link:

<a href="http://www.kernel.org/pub/software/scm/git/docs/git-filter-branch.html" target="_top">git-filter-branch(1) Manual Page</a>

Using the <code>filter-branch</code> command is dangerous as it changes the Git repository. It changes the commit IDs and reacting on such a change requires explicit action from the developer, e.g., trying to rebase the stale local branch onto the corresponding rewritten remote-tracking branch.

A practical case for using <code>git filter-branch</code> is where you have added a file which contains a password or a huge binary file to the Git repository, and you want to remove this file from the history. To completely remove the file you need to run the <code>filter-branch</code> command on all branches.

The following listing shows an example on how to replace the email address from one author of all the commits via the <code>git filter-branch</code> command.

A patch is a text file that contains changes to the source code. A patch created with the <code>git format-patch</code> command includes meta-information about the commit (committer, date, commit message, etc) and also contains the changes introduced in binary data in the commit, for example, an image.

This file can be sent to someone else and this developer can use this file to apply the changes to his local repository. The metadata is preserved.

Alternatively you could create a diff file with the <code>git diff</code> command, but this diff file does not contain the metadata information.

The following example creates a branch, changes several files and creates a commit recording these changes.

The next example creates a patch for these changes.

To apply this patch to your master branch in a different clone of the repository, switch to it and use the <code>git apply</code> command.

Afterwards you can commit the changes introduced by the patches and delete the patch file.

Use the <code>git am</code> command to apply and commit the changes in a single step. To apply and commit all patch files in the directory use, for example, the<code>git am *.patch</code> command. You specify the order in which the patches are applied by specifying them on the command line.

You can specify the commit ID and the number of patches which should be created. For example, to create a patch for selected commits based on the HEAD pointer you can use the following commands.

Git provides commit hooks, e.g., programs which can be executed at a pre-defined point during the work with the repository. For example, you can ensure that the commit message has a certain format or trigger an action after a push to the server.

These programs are usually scripts and can be written in any language, e.g., as shell scripts or in Perl, Python etc. You can also implement a hook, for example, in C and use the resulting executables. Git calls the scripts based on a naming convention.

Git provides hooks for the client and for the server side. On the server side you can use the <code>pre-receive</code> and <code>post-receive</code> script to check the input or to trigger actions after the commit. The usage of a server commit hook requires that you have access to the server. Hosting providers like GitHub or Bitbucket do not offer this access.

If you create a new Git repository, Git creates example scripts in the <code>.git/hooks</code> directory. The example scripts end with <code>.sample</code>. To activate them make them executable and remove the <code>.sample</code> from the filename.

Not all Git server implementations support server side commit hooks. For example Gerrit (a Git server which also provides the ability to do code review) does not support hooks in this form. Also Github and Bitbucket do not support server hooks at the time of this writing.

Local hooks in the local repository can be removed by the developer.

Every time a developer presses return on the keyboard an invisible character called a line ending is inserted. Unfortunately, different operating systems handle line endings differently.

Linux and Mac use different line endings than Windows. Windows uses a carriage-return and a linefeed character (CRLF), while Linux and Mac only uses a linefeed character (LF). This becomes a problem if developers use different operating system to commit changes to a Git repository.

To avoid commits because of line ending differences in your Git repository you should configure all clients to write the same line ending to the Git repository.

On Windows systems you can tell Git to convert line endings during a checkout to CRLF and to convert them back to LF during commit. Use the following setting for this.

On Linux and Mac you can tell Git to convert CRLF to LF with the following setting.

You can also configure the line ending handling per repository by adding a special <code>.gitattributes</code> file to the root folder of your Git repository. If this file is committed to the repository, it overrides the <code>core.autocrlf</code> setting of the individual developer.

In this file you can configure Git to auto detect the line endings.

To convert Subversion projects to Git you can use a RubyGem called svn2git which relies on <code>git svn</code> internally and handles most of the trouble.

To install it (on Ubuntu) simply type:

Let's say you have a repository called <code>http://svn.example.com/repo</code> with the default layout (trunk, branches, tags) and already prepared a local git repository where you want to put everything, then navigate to your git directory and use the following commands:

The parameter <code>--verbose</code> adds detailed output to the commandline so you can see what is going on including potential errors. The second <code>svn2git --rebase</code> command aligns your new git repository with the svn import. You are now ready to push to the web and get forked! If your svn layout deviates from the standard or other problems occur, seek<code>svn2git --help</code> for documentation on additional parameters.

The usage of symlinks requires that the operating system used by the developers supports them.

Git as version control system can handle symlinks.

If the symlink points to a file, then Git stores the path information it is symlinking to, and the file type. This is similar to a symlink to a directory; Git does not store the contents under the symlinked directory.

This tutorial is part of a series about the Git version control system. See the other tutorials for more information.

<a href="http://www.vogella.com/tutorials/Git/article.html" target="_top">Introduction to Git</a>

<a href="http://www.vogella.com/tutorials/GitHub/article.html" target="_top">Using Github</a>

<a href="http://www.vogella.com/tutorials/GitHosting/article.html" target="_top">Hosting Git repositories at Bitbucket or on your own server</a>

<a href="http://www.vogella.com/tutorials/GitWorkflows/article.html" target="_top">Typical workflows with Git</a>

<a href="http://www.vogella.com/tutorials/EclipseGit/article.html" target="_top">EGit - Teamprovider for Eclipse</a>

【新浪微網誌】 張昺華--sky

【twitter】 @sky2030_

【facebook】 張昺華 zhangbinghua

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利.

繼續閱讀