2、Git Basics
介紹如何使用 git,讀完這章就足已完成大多數 git 的操作。



2.1 - Getting a Git Repository
You can get a Git project using two main approaches. The first takes an existing project or directory and imports it into Git. The second clones an existing Git repository from another server.

Initializing a Repository in an Existing Directory
git init
git add *.c
git add README
git commit -m 'initial project version'


Cloning an Existing Repository
git clone git://github.com/schacon/grit.git
That command does the same thing as the previous one, but the target directory is called mygrit.
git clone git://github.com/schacon/grit.git mygrit



2.2 - Recording Changes to the Repository
在 working directory 中,所有的檔案都可能處於 tracked 或 untracked 狀態,如果處於 tracked 狀態,則可以是 unmodified, modified, or staged,但如果是 untracked 狀態,則什麼都不是,它們不存在於最近的 snapshot,也不存在於 staging area。

如果是在剛 clone 完一個 repository 的情況下,則所有的檔案都是 tracked 及 unmodified 的狀態。

Checking the Status of Your Files
The main tool you use to determine which files are in which state is the command:
git status

Tracking New Files
In order to begin tracking a new file, you use the command:
git add new_file

Staging Modified Files
Let’s change a file that was already tracked, and then how too stage it:
git add modified_file

Ignoring Files
you’ll have a class of files that you don’t want Git to automatically add or even show you as being untracked.

Viewing Your Staged and Unstaged Changes
git diff
If you want to see what you’ve staged that will go into your next commit, you can use:
git diff --cached

git diff --staged

Committing Your Changes
Now that your staging area is set up the way you want it, you can commit your changes. Remember that anything that is still unstaged — any files you have created or modified that you haven’t run git add on since you edited them — won’t go into this commit:
git commit

git commit -m "commit message"

Skipping the Staging Area
Git 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:
git commit -a -m "commit message"

Removing Files
To remove a file from Git, you have to remove it from your tracked files (more accurately, remove it from your staging area) and then commit:
git rm remove_file
Another useful thing you may want to do is to keep the file in your working tree but remove it from your staging area.
git rm --cached remove_file

Moving Files
If you want to rename a file in Git, you can run something like:
git mv file_from file_to



2.3 - Viewing the Commit History
After you have created several commits, or if you have cloned a repository with an existing commit history, you’ll probably want to look back to see what has happened:
git log

One of the more helpful options is -p, which shows the diff introduced in each commit. You can also use -2, which limits the output to only the last two entries:
git log -p -2



2.4 - Undoing Things
At any stage, you may want to undo something. Here, we’ll review a few basic tools for undoing changes that you’ve made.

Changing Your Last Commit
One of the common undos takes place when you commit too early and possibly forget to add some files, or you mess up your commit message:
git commit --amend

Unstaging a Staged File
git reset HEAD staged_file

Unmodifying a Modified File
git checkout -- modified_file

Remember, anything that is committed in Git can almost always be recovered. However, anything you lose that was never committed is likely never to be seen again.



2.5 - Working with Remotes
Showing Your Remotes
git remote

git remote -v

Adding Remote Repositories
git remote add [shortname] [url]

Fetching and Pulling from Your Remotes
git fetch [remote-name]
It’s important to note that the 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. You have to merge it manually into your work when you’re ready.

If you have a branch set up to track a remote branch, you can use the git pull command to automatically fetch and then merge a remote branch into your current branch.

Pushing to Your Remotes
When you have your project at a point that you want to share, you have to push it upstream. The command for this is simple:
git push [remote-name] [branch-name]

Inspecting a Remote
If you want to see more information about a particular remote, you can use:
git remote show [remote-name]

Removing and Renaming Remotes
If you want to rename a reference, in newer versions of Git you can run
git remote rename



2.6 - Tagging

Listing Your Tags

Creating Tags

Annotated Tag

Signed Tags

Lightweight Tags

Verifying Tags

Tagging Later

Sharing Tags



2.7 - Tips and Tricks

Auto-Completion

Git Aliases



2.8 - Summary
在這一章可以學到全部的基本 git 操作,包含建立或複製 repository、修改檔案、staged 或 commit 這些修改的檔案、查詢 repository 的 history change。
arrow
arrow
    全站熱搜
    創作者介紹

    silverfoxkkk 發表在 痞客邦 留言(0) 人氣()