Git Basics



Your Identity

The first thing you should do when you install Git is to set your username and e-mail address. This is important because every Git commit uses this information, and it’s immutably baked into the commits you pass around:

$ git config -global user.name "Jerry Lee"
$git config --global user.email jerry.lee@example.com

Your Editor

You can configure the default text editor that will be used when Git needs you to type in a message. By default, Git uses your system’s default editor, which is generally Vi or Vim. If you want to use a different text editor, such as Emacs, you can
do the following:

$git config --global core.editor emacs

Checking Your Settings

$git config --list

Getting a Git Repository

Initializing a Repository in an Existing Directory

To track an existing project in Git, you need to go to the project’s directory and type:

$git init

Cloning an Existing Repository

Every version of every file for the history of the project is pulled down when you run git clone [url].
Git has a number of different transfer protocols you can use: git://, https:// or user@server:/path.git

Recording Changes to the Repository

Remember that each file in your working directory can be in one of two states: tracked or untracked. Tracked files are files that were in the last snapshot; they can be unmodified, modified, or staged. Untracked files are everything else—any files in your working directory that weren’t in your last snapshot and aren’t in your staging area.

Tracking New Files

In order to begin tracking a new file, you use the command git add .

Staging Modified Files

To stage modified files, you run the git add command . (it’s a multipurpose command—you use it
to begin tracking new files, to stage files, and to do other things like marking merge-conflicted
files as resolved).
If you modify a file after you run git add, you have to run git add again to stage the latest version of the file

Ignoring Files

you can create a file named .gitignore listing patterns to match them.

Viewing Your Staged and Unstaged Changes

To see what you’ve changed but not yet staged, type git diff with no other arguments.
If you want to see what you’ve staged that will go into your next commit, you can use git diff staged.

Committing Your Changes

The commit records the snapshot you set up in your staging area. Anything you didn’t stage is still sitting there modified; you can do another commit to add it to your history. Every time you perform a commit, you’re recording a snapshot of your project that you can revert to or compare to later.

Skipping the Staging Area

Providing the -a option to the git commit command makes Git automatically stage every file that is already tracked before doing the commit, letting you skip the git add part.

Removing Files

git rm

Moving Files

git mv is is equivalent to running something like this:

mv file1 file2
git rm file1
git add file2

Viewing the Commit History

By default, with no arguments, git log lists the commits made in that repository in reverse chronological order.
This option -p displays the same information but with a diff directly following each entry. You can also use -p  -2 , which limits the output to only the last two entries
The --stat option prints below each commit entry a list of modified files,
how many files were changed, and how many lines in those files were added and removed. It
also puts a summary of the information at the end.
The --pretty= option changes the log output to formats other than the default: oneline, short, full and fuller.

You can use git show to list the changes of a commit:

git show commit_number

You can use the following command to list only filename changed for a commit:

git diff-tree --no-commit-id --name-only -r commit_number

Undoing Things

Changing Your Last Commit

If you want to try that commit again, you can run git log --amend.

Unstaging a Staged File

git reset HEAD

Unmodifying a Modified File

git checkout --

Working with Remotes

Remote repositories are versions of your project that are hosted on the Internet or network somewhere.

Showing Your Remotes

To see which remote servers you have configured, you can run the git remote command. If you’ve cloned your repository, you should at least see origin—that is the default name Git gives to the server you cloned
from. You can also specify -v, which shows you the URL that Git has stored for the shortname to be expanded to: git remote -v.

Adding Remote Repositories

To add a new remote Git repository as a shortname you can reference easily, run git remote add [remote-name] [url]:
git remote add pb git://github.com/paulboone/ticgit.git

Now you can use the string pb as the Git URL.

Fetching and Pulling from Your Remotes

To get data from your remote projects, you can run:
git fetch [remote-name]
The command goes out to that remote project and pulls down all the data from that remote project that you don’t have yet. After you do this, you should have references to all the branches from that remote, which you can merge in or inspect at any time.
git fetch origin fetches any new work that has been pushed to that server since you cloned (or last fetched from) it. It’s important to note that the git fetch command pulls the data to your local repository—it doesn’t automatically merge it with any of your work or modify what you’re currently working on.
By default, the git clone command automatically sets up your local master branch to track the remote master branch on the server you cloned from. Running git pull generally fetches data from the server you originally cloned from and automatically tries to merge it into the code you’re currently working on.

Pushing to Your Remotes

When you have your project at a point that you want to share, you have to push it upstream:
git push [remote-name] [remote-branch]
This command works only if you cloned from a server to which you have write access and if nobody has pushed in the meantime.

Inspecting a Remote

If you want to see more information about a particular remote, you can use the git remote show [remote-name] command.

Removing and Renaming Remotes

You can run git remote rename [old-name] [new-name] to change a remote’s shortname.
If you want to remove a reference, you can use git remote rm [remote-name].

Tagging

Git has the ability to tag specific points in history as being important.

Listing Your Tags

This git tag command lists the tags in alphabetical order.
You can also search for tags with a particular pattern.

Creating Tags

Git uses two main types of tags: lightweight and annotated. A lightweight tag is just a pointer to a specific commit. Annotated tags, however, are stored as full objects in the Git database.

Annotated Tags

git tag -a [tag-name] -m "tag messages"

Signed Tags

You can also sign your tags with GPG,
git tag -s [tag-name] -m "tag messages"


Verifying Tags

git tag -v [tag-name]

Lightweight Tags

git tag [tag-name]

Sharing Tags

By default, the git push command doesn’t transfer tags to remote servers. You have to explicitly
push tags to a shared server after you create them.
git push origin [tag-name]
To transfer to the remote server all of your tags that aren’t already there,
git push origin --tags

Tips and Tricks

Auto-Completion

Git comes with a nice auto-completion script in the Git source code, look in the contrib/completion directory; there should be a file called git-completion.bash. Copy this file to your home directory, and add this to your
.bashrc file:
source ~/.git-completion.bash
Press the Tab key when you’re writing a Git command, and it should return a set of suggestions for you to pick from.

Git Aliases

If you don’t want to type the entire text of each of the Git commands, you can easily set up an alias for each command using git config.



留言

熱門文章