Git is a distributed version control system for tracking changes in computer files and coordinating work on those files among multiple people.
Git has three main states that your files can reside in: committed, modified, and staged:
- Committed means that the data is safely stored in your local database.
- Modified means that you have changed the file but have not committed it to your database yet.
- Staged means that you have marked a modified file in its current version to go into your next commit snapshot.
Figure 1. Working tree, staging area, and Git directory.Installing Git
- Installing on Windows
The most official build is available for download on the Git website. Just go to http://git-scm.com/download/win and the download will start automatically. - Installing on Linux (Ubuntu/Debain)
If you’re on a Debian-based distribution, such as Ubuntu, tryapt
:1
$ apt-get install git
- Installing on Windows
Git configure tooling
Git comes with a tool called git config that lets you get and set configuration variables that control all aspects of how Git looks and operates. These variables can be stored in three different places:
/etc/gitconfig
file: Contains values applied to every user on the system and all their repositories. If you pass the option--system
to git config, it reads and writes from this file specifically. (Because this is a system configuration file, you would need administrative or superuser privilege to make changes to it.)~/.gitconfig
or~/.config/git/config
file: Values specific personally to you, the user. You can make Git read and write to this file specifically by passing the –global option.- config file in the Git directory (that is,
.git/config
) of whatever repository you’re currently using: Specific to that single repository.
The first thing you should do when you install Git is to set your user name and email address. This is important because every Git commit uses this information, and it’s immutably baked into the commits you start creating:1
2$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
If you want to override this with a different name or email address for specific projects, you can run the command without the --global
option when you’re in that project.
Git commands
Creating or cloning a Git repository
If you have a project directory that is currently not under version control and you want to start controlling it with Git, you first need to go to that project’s directory and type:1
$ git init
This creates a new subdirectory named .git
that contains all of your necessary repository files — a Git repository skeleton. At this point, nothing in your project is tracked yet. If you want to start version-controlling existing files (as opposed to an empty directory), you should probably begin tracking those files and do an initial commit. You can accomplish that with a few git add
commands.
If you want to get a copy of an existing Git repository — for example, a project you’d like to contribute to — the command you need is git clone
like this:1
$ git clone https://github.com/libgit2/libgit2
Make Changes
If you have made changes in your profiles,you can list all new or modified files by typing:1
$ git status
While the git status output is pretty comprehensive, it’s also quite wordy. Git also has a short status flag so you can see your changes in a more compact way. If you run git status -s
or git status --short
In order to begin tracking a new file, you use the command:1
$ git add
To see what you’ve changed but not yet staged, type:1
$ git diff
That command compares what is in your working directory with what is in your staging area. The result tells you the changes you’ve made that you haven’t yet staged.
If you want to see what you’ve staged that will go into your next commit, you can use git diff --staged
. This command compares your staged changes to your last commit.And you can use git diff --cached
to see what you’ve staged so far.
Group Changes
To List out all local branches in the current repository, you can run:1
$ git branch
If you want to create a new branch, you can use command git branch [branch-name]
(command git branch -d [branch-name]
for deleting), then use the command git checkout [branch-name]
to switch to the specified branch and update the working directory.
Certainly,To create a new branch and switch to it at the same time, you can run:1
$ git checkout -b [branch-name]
Review History
To view the commit history of the current branch, you should use the command:1
$ git log
Certainly, common options to $ git log
helps you to output more freely like sql server. Options are listed in Table.1
Option | Description |
---|---|
–pretty=oneline | Print each commit on a single line |
–pretty=format: | Outputyour own log output format |
–patch | Show the patch introduced with each commit. |
–stat | Show statistics for files modified in each commit. |