Git is a free software distributed under the terms of the GNU. Similar to CVS, it's a widely used source code management system, a distributed revision control system. Here are some some mostly used commands examples:

To config git environment

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

$ git config --global "Firstname Lastname"
$ git config --global This email address is being protected from spambots. You need JavaScript enabled to view it.
$ git config --global core.editor emacs

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

To colone an existing repo into your local directotry

It the command same as cvs checkout, it gets every version of every file in the history of the project

## source_url_or_ssh can be something like:

this source remote is named origin:
$ git clone [source_url_or_ssh]

Note: using git protocol, the path can be configured in protocol configuration, thus, you don't have to specify the path in git protocol. It's really depends on you site git configuration.

Also, you do not need to add the '.git' extension to the name of the repository by either method.

Checkout the files from remote

$ git checkout -- [FILENAME]

Show the remote server names and their urls

$ git remote -v

Pull any new changes from remote project

merge new changes into your current branch

$ git pull [remote-name] 

Does not merge into your current branch

$ git fetch [remote-name]

Check local/remote status

$ git status

See what's been changed in local files but not staged

$ git diff

See what's different between staged and the last commited

$ git diff --staged

To get an existing project into git

cd into the project directory

makes a .git subdir

$ git init

adds everything in the project dir to git


$ git add .

Otherwise do

$ git add /PATH/TO/FILE

commit with message

Omitting -m 'text' will open a vim session for your message

$ git commit -m 'some message'

actually push to a remote server (the one named origin)

$ git push origin master

prepare it for commit

To track/untrack a new file, or to stage/unstage a modified file

git track/stage FILENAME
$ git add [FILENAME]

## unstage FILENAME

$ git reset HEAD [FILENAME]

Remember to stage files before committing, otherwise you will only commit the changes in your most recent add/stage (or, use the -a flag when committing to stage tracked files automatically)
## see what files are and aren't staged for commit

To commit

will open a vim session for you to type your message

$ git commit

commit with a message and avoid vim

$ git commit -m 'some message'

skip the staging area (will automatically stage every tracked file before commit)

$ git commit -a -m 'some message'

## the second commit replaces your first, in case you forgot a file in your first commit

$ git commit --amend

To undo local changes:

discard changes in working dir and pull a fresh version of FILENAME (overwrites local ver.)

$ git checkout -- [FILENAME]

discard all local changes

$ git reset --hard HEAD


stage a tracked file for removal

Commit this and the file will be untracked and gone

$ git rm FILE

renames a file

Do this otherwise git might not track your renamed file

$ git mv FILE1 FILE2

List existing branches

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

$ git branch

Merges one or more branches into your current branch

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

git merge

Resets your index and working directory

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

$ git reset --hard HEAD
$ git reset

Temporarily saves changes

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

git stash

To restore

git stash apply

Tags a specific commit

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

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

Shows all the remote versions

Shows all the remote versions of your repository.

git remote
git remote origin
git remote -v

Show a listing of commits

$git log

Shows information about a git object

git show commit

Shows a tree object

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

git ls-tree

View the type of an object

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

git cat-file

Search content in git

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

$git grep "" -- *.p* 

Comments powered by CComment