Git is a powerful system for distributed version control.

To get documentation for a Git command:

1. $ man git-[command-name]

2. $ git help [command-name]

Configuring Git for all local repositories

1. $ git config --global user.name "[name]" 

# to set the name to commit transactions

2. $ git config --global user.email "[email address]" 

# to set the email to commit transactions

Working with branches

Branches - an important part of working with Git.

1. $ git status 

# to check the status of the repository

2. $ git branch [branch-name] 

# git command to create a new branch

3. $ git checkout [branch-name] 

# switches to the specified branch and updates the working directory

4. $ git merge [branch] 

# combines the specified branch’s history into the current branch.

5. $ git branch -d [branch-name] 

# deletes the specified branch

Get information and Make changes to the branches

1. $ git log 

# lists version history for the current branch

git log --pretty=oneline 

# lists version history for the current branch in one line format

2. $ git diff [first-branch]...[second-branch] 

# shows content differences between two branches

3. $ git show [commit] 

# outputs metadata and content changes of the specified commit

4. $ git add [file] 

# snapshots the file in preparation for versioning and stages the changes

5. $ git commit -m "[descriptive message]" 

# records file snapshots in version history

Redo Git commits

1. $ git reset [commit] 

# undoes all commits after [commit] and preserve changes locally

2. $ git reset --hard [commit] 

# discards all history and changes back to the specified commit

Creating repositories

1. $ git init 

# to start out a new git repository (initial setup)

2. $ git remote add origin [url] 

# to link the local repository to an empty GitHub repository

3. $ git clone [url] 

# clone (download) a repository that already exists on GitHub, including all of the files, branches, and commits

4. $ git fetch origin <commit-hash> 

# to fetch a commit using its hash

The .gitignore file

To exclude files from being tracked with Git use a special file - .gitignore.

Create a file in project directory named .gitignore (Note: the name starts with a period) and place inside it names of files and folders you want to exclude from tracking.

Synchronizing changes

1. $ git fetch 

# downloads all history from the remote tracking branches

2. $ git merge 

# combines remote tracking branches into current local branch

3. $ git push 

# uploads all local branch commits to GitHub

4. $ git pull 

# updates your current local working branch with all new commits from the corresponding remote branch on GitHub. git pull is a combination of git fetch and git merge

Git recipes

Using git to create a git repository with a previous version of a project

version 1 (using git fetch)

1. $ git init

# create and initialize an empty git repository

2. $ git remote add origin <repository> 

# add a remote named origin for the repository at <repository>

3. $ git fetch origin <commit-hash> 

# fetch a commit using its hash

4. $ git reset --hard FETCH_HEAD 

# reset repository to that commit

version 2 (using git clone)

1. $ git clone <repository> 

# clone the git repository into the current directory

2. $ git reset --hard <commit-hash> 

# hard reset repository to specific commit

version 3 (using git clone)

1. $ git clone <repository> 

# clone the git repository into the current directory

2. $ git checkout <commit-hash> 

# checkout a commit using its hash

3. $ git reset --hard 

# hard reset repository

version 4 (restoring a previous version of a project in a new local branch)

$ git checkout -b [new-branch-name] <commit-hash> 

# create a new branch – “new-branch-name” with hash of the old project revision to restore

GitHub and Bitbucket - online Git repository service providers

  1. GitHub and Bitbucket – hosting services for software development and version control using Git.

Git online tutorials, resources and commands