现在的位置: 首页 > 综合 > 正文

GIT Tutorial

2019年10月01日 ⁄ 综合 ⁄ 共 7301字 ⁄ 字号 评论关闭

一,How to install GIT on Linux, Windows or Mac

On Linux you can compile the system from source or use commands like the following ones:

$apt-get git-core

or

$yum install git-core

It depends on your distribution.

On Windows the installation is even simpler. You should download the exe file, run it and follow the on-screen instructions.
Then you will run GIT through the Windows command line.

The easiest way to install GIT on Mac OS is to use the MacPorts software ( http://www.macports.org)
and to run the following command:

$sudo port install git-core


二,GIT Repository Management


In this section you will find a full scenario on how to use GIT to create a project, add files, commit modifications and upload them
in the remote repository.

After the GIT installation you should set your configuration. Use the following commands:

1. username: First you need to tell git your name, so that it can properly label the commits you make.

$git config --global user.name "Your Name Here"
# Sets the default name for git to use when you commit

2. Git also saves your email address into the commits you make. We use the email address to associate your commits with your GitHub account.

$git config --global user.email "your_email@youremail.com"
# Sets the default email for git to use when you commit

Good to know: You don't have to use your real email address. Git is actually very relaxed about the email setting. While it requires it to be set, it doesn't force you to use a valid address. Many users use
a more generic "user@server" format. This allows you to identify who made the commit, and from where, without exposing your email address to anyone that has access to the repo.


3. Password caching
The last option we need to set will tell git that you don't want to type your username and password every time you talk to a remote server. To do this, you need install the osxkeychain credential helper and tell git to use it.
If you installed git using homebrew, you should already have the osxkeychain helper. You can verify this by trying to run it:

$ git credential-osxkeychain 
# Usage: git credential-osxkeychain <get|store|erase>

If you do not have the helper, you candownload it and copy it to
/usr/local/bin

To tell git to use osxkeychain, simply set the global git config:

$git config --global credential.helper osxkeychain
# Set git to use the osxkeychain credential helper

The next time you clone an HTTPS URL that requires a password you will be prompted for your username and password, and to grant access
to the OSX keychain. After you've done this, the username and password are stored in your keychain and you won't be required to type them in to git again.


Good to know: The credential helper only works when you clone an HTTPS repo URL. If you use the SSH repo URL instead, SSH keys are used for authentication. While we do not recommend it, if you wish to use this method, check out this guide for help generating
and using an SSH key. 


三,Major GIT commands with examples

Here
you will find a list with the major commands, their short descriptions and exemplary usage. For a detailed description of all the GIT commands please visit 

http://git-scm.com/docs/
 或者 http://www.kernel.org/pub/software/scm/git/docs/

Major
GIT commands:

1. git
config

Sets
configuration values for your user name, email, gpg key, preferred diff algorithm, file formats and more.

example:

 git config --global user.name "My Name"
 git config --global user.email "user@domain.com"

then you can execute to see you configuration:

$cat ~/.gitconfig

2. git init

Initializes
a git repository – creates the initial ‘.git’ directory in a new or in an existing project.

example:

$cd /home/user/my_new_git_folder/
$git init

3. git clone

Makes
a Git repository copy from a remote source. Also adds the original location as a remote so you can fetch from it again and push to it if you have permissions.

example:

$git clone git@github.com:user/test.git

4. git add

Adds
files changes in your working directory to your index.

example:

$git add .

5. git rm

Removes
files from your index and your working directory so they will not be tracked.

example:

$git rm filename

6. git commit

Takes
all of the changes written in the index, creates a new commit object pointing to it and sets the branch to point to that new commit.

example:

$git commit -m ‘committing added changes’
$git commit -a -m ‘committing all changes, equals to git add and git commit’

7. git status

Shows
you the status of files in the index versus the working directory. It will list out files that are untracked (only in your working directory), modified (tracked but not yet updated in your index), and staged (added to your index and ready for committing).

example:

$git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    README
nothing added to commit but untracked files present (use "git add" to track)

8. git branch

Lists
existing branches, including remote branches if ‘-a’ is provided. Creates a new branch if a branch name is provided.

example:

$git branch -a
* master
  remotes/origin/master

9. git checkout

Checks
out a different branch – switches branches by updating the index, working tree, and HEAD to reflect the chosen branch.

example:

$git checkout newbranch

10. git merge

Merges
one or more branches into your current branch and automatically creates a new commit if there are no conflicts.

example:

$git merge newbranchversion

11. git reset

Resets
your index and working directory to the state of your last commit.

example:

$git reset --hard HEAD

12. git stash

Temporarily
saves changes that you don’t want to commit immediately. You can apply the changes later.

$git stash
Saved working directory and index state "WIP on master: 84f241e first commit"
HEAD is now at 84f241e first commit
(To restore them type "git stash apply")

13. git tag

Tags
a specific commit with a simple, human readable handle that never moves.

example:

$git tag -a v1.0 -m 'this is version 1.0 tag'

14. git fetch

Fetches
all the objects from the remote repository that are not present in the local one.

example:

$git fetch origin

15. git pull

Fetches
the files from the remote repository and merges it with your local one. This command is equal to the git fetch and the git merge sequence.

$git pull origin

16. git push

Pushes
all the modified local objects to the remote repository and advances its branches.

example:

$git push origin master

17. git remote

Shows
all the remote versions of your repository.

example:

$git remote
origin

18. git log

Shows
a listing of commits on a branch including the corresponding details.

example:

$git log
commit 84f241e8a0d768fb37ff7ad40e294b61a99a0abe
Author: User <user@domain.com>
Date:   Mon May 3 09:24:05 2010 +0300

    first commit

19. git show

Shows
information about a git object.

example:

$git show
commit 84f241e8a0d768fb37ff7ad40e294b61a99a0abe
Author: User <user@domain.com>
Date:   Mon May 3 09:24:05 2010 +0300

    first commit

diff --git a/README b/README
new file mode 100644
index 0000000..e69de29

20. git ls-tree

Shows
a tree object, including the mode and the name of each item and the SHA-1 value of the blob or the tree that it points to.

example:

$git ls-tree master^{tree}
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    README

21. git cat-file

Used
to view the type of an object through the SHA-1 value.

example:

$git cat-file -t e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
blob

22. git grep

Lets
you search through your trees of content for words and phrases.

example:

$git grep "www.siteground.com" -- *.php

23. git diff

Generates
patch files or statistics of differences between paths or files in your git repository, or your index or your working directory.

example:

$git diff

24. gitk

Graphical
Tcl/Tk based interface to a local Git repository.

example:

$gitk

25. git instaweb

Runs
a web server with an interface into your local repository and automatically directs a web browser to it.

example:

$git instaweb --httpd=webrick
$git instaweb --stop

26. git archive

Creates
a tar or zip file including the contents of a single tree from your repository.

example:

$git archive --format=zip master^ README >file.zip

27. git gc

Garbage
collector for your repository. Optimizes your repository. Should be run occasionally.

$git gc
Counting objects: 7, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), done.
Total 7 (delta 1), reused 0 (delta 0)

28. git fsck

Does
an integrity check of the Git file system, identifying corrupted objects.

example:

$git fsck

29. git prune

Removes
objects that are no longer pointed to by any object in any reachable branch.

example:

$git prune                                                                                                                            
















抱歉!评论已关闭.