Transferred from: http://www.cnblogs.com/elfsundae/archive/2011/07/17/2099698.html
References:
Http://stackoverflow.com/questions/315911/git-for-beginners-the-definitive-practical-guide
http://www.kernel.org/pub/software/scm/git/docs/
http://progit.org/book/
Git install, configure user name mailbox, SSH server build
Http://www.cnblogs.com/elfsundae/archive/2011/07/06/2099182.html
Create/list/remove a new Project/repository
$ git init
A hidden directory named ". Git" will be created in the current directory.
$ git init Project1
Equivalent to $ mkdir project1 && cd Project1 && git init
$ git status
Check if the current directory contains a git repo
$ ls. git
View git directory
$ RM-RF. git/
To remove everything about git
Configure git to ignore files
The. gitignore file can define files to be ignored. See http://www.kernel.org/pub/software/scm/git/docs/gitignore.html for detailed rules.
Filter folder:/build/
Filter files of some kind: *.tmp
Filter a file:/build/products/test.app
The opening means no filtering:!*.C,!/dir/subdir/
Wildcard characters are supported: *. [OA] Filters all files in the repo with. O or. A as extensions
There are three ways to apply filtering:
- Apply filtering to all users of the repo:
Place the. gitignore file in the working directory with the directory, edit the. Gitignore after the completion of the submission
git Add. Gitignore
- Filter only on your own repo backup:
Add/Edit the $git_dir/info/exclude of your working directory, for example your working copy directory is
~/src/project1, the path is
~/src/project1/.git/info/exclude
- System Global Filtering
Create a ignore file with a random name, such as my ~/.gitglobalignore, and then configure GIT:
$ core.excludesfile = ~/.gitglobalignore
Examples of. gitignore files:
. Ds_store
# # # Build Directory
imochaapp/build/
imochasdk/build/
# # # Testing Projects Directory
/testing/
Getting the latest Code
# Your working directory
$ git fetch <remote> <branch> # fetches the code but does not merge
# it into your working directory
$ git pull--tag <remote> <branch> # Same as above and fetch tags as well
$ git fetch--tag <remote> <branch> # get the idea
Checking out Code (clone)
$ git clone [email protected]/dir/to/repo [Target DirName]
Commit changes
When you have modified the file, you need to commit the changes (commit).
$ git commit source/main.c
The previous sentence will be submitted in the./source/directory under the Main.c file.
$ git commit-a
The-a flag indicates that all modified files are committed, but no new additions are submitted. The newly added file needs to be added to the GIT index using $ git-add.
"Submit" only changes your local repo, if you want to commit changes to the server, you need to use push:
$ git push <remote> <branch>
View current status
$ git status can see what's currently working with that branch, what's going to be submitted, reminding you what to forget, etc...
Undo/revert/reset a Commit
If you do not want the current changes to take effect, return to the previous commit, you can run the following command:
# Revert to a previous commit by hash:
$ git-reset--hard
You can use the head^ shortcut to specify the last commit hash:
# Revert to previous commit:
$ git-reset--hard head^
File comparison
Compare command is $ git diff
# to compare 2 revisions of a file:
$ git diff <commit1> <commit2> <file_name>
# to compare current staged file against the repository:
$ git diff--staged <file_name>
#to Compare current unstaged file against the repository:
$ git diff <file_name>
How does the history of revisions to a file?
$ git log--filename
Git Branch (branch)
Git default branch called Master
# Create a new branch
$ git Branch <branch-name>
# to-see a list of all branches in the Cureent repoitory
$ git Branch
# If you want-to-switch to another branch your can use
$ git checkout <branch-name>
# to create a new branch and switch to it in one step
$ git checkout-b <branch-name>
# to delete a branch:
$ git branch-d <branch-name>
# to create a branch with the changes from the current branch,do:
$ git stash
$ git Stash Branch <branch-name>
How does the merge branches?
If you want to merge a branch (e.g. "Master" to "release"), make sure your current branch are the target branch you ' d like t o Merge into (use $git branch or $git status to see your current branch).
Then use
$ git Merge Master
(where master is the name of the branch you want-to-merge with the current branch).
If There is any conflicts, you can use
$ git diff
To see pending conflicts you have to resolve.
Trace Remote Branch
Suppose you have clone a remote repo with a ' some_branch ' branch. The following command will track this branch locally:
# List Remote Branches
Git branch-r
# Start Tracking One remote branch
Git branch--track some_branch origin/some_branch
# Change to the branch locally
git checkout Some_branch
# make changes and commit them locally
....
# Push your changes to the remote repository:
git push
To create a remote branch
# Create a new branch locally
Git branch Name_of_branch
git checkout Name_of_branch
# edit/add/remove Files
# Commit your changes locally
git add fileName
Git commit-m Message
# Push changes and new branch to remote repository:
Git push Origin Name_of_branch:name_of_branch
Delete Remote Branch
git push [远程名] :[分支名]
$ git push origin:mybranchname
Common git actions-ignore files and common commands "go"