Git For Beginners

Git is a bit different than other well known version control systems, but its popularity is growing up each day. If someone needs only the basics and doesn't want to get into great detail regarding what Git is doing in the background, here is the quick tutorial and some useful tips.

Installing Git

On a Debian-based distribution like Ubuntu, try using apt-get:

$ apt-get install git

On Red Hat based distributions, such as Fedora:

$ yum install git

On Windows, simply download the installer and run it:

http://git-scm.com/download/win

On Mac OS X, grab the installer here:

http://git-scm.com/download/mac

Getting a Git Repository

To create a new git repository, create a new directory, open it and perform:

$ git init

This command creates a new hidden subdirectory named .git that will contain all repository files (it is empty right after the initialization).

To clone an existing repository (to checkout a repository):

$ git clone path/to/repository

or if you use a remote server:

$ git clone username@host:/path/to/repository

For example, to clone django (using git protocol):

$ git clone git@github.com:django/django.git

Tracking files and commit changes

To check the status of your files use:

$ git status
# On branch master
nothing to commit (working directory clean)

To add a file to the list of files that will be committed to the repository:

$ git add [file]

or to add all files:

$ git add *

To actually commit changes to the repository, perform:

$ git commit -m 'This is commit message.' [file]

The -m option sets the message that will be saved with the commit. If you don’t use it, an editor will be started to enter the message.

To remove a file from Git, remove it from your tracked files and then commit.

$ git rm [file]

To see what is changed but not yet staged:

$ git diff

To see what is staged, but not yet commited:

$ git diff --cached

Checking the Commit History can be done using git log:

$ git log

Ignoring files can be done by adding a file .gitignore which lists patterns that git will ignore. Here is the example of .gitignore file:

$ nano .gitignore
*.log
*~

The first line tells Git to ignore all files ending with .log. The second line tells Git to ignore all files that end with a tilde (~), which is very userful as many text editors use tilde to mark temporary files.

Undoing changes

If you forgot to add a file before a commit or you messed up the commit message:

$ git commit -m 'initial commit with errors'
$ git add [forgotten_file]
$ git commit --amend

Or another way:

$ git reset --soft HEAD^

To unstage an added file:

$ git reset HEAD [file]

To discard changes on a file (use this with caution as you are going to loose all uncommited changes on the file):

$ git checkout -- [file]

For undoing last 3 commits permanently, run:

$ git reset --hard HEAD~3

Undo a merge or pull:

$ git pull
Auto-merging test
CONFLICT (content): Merge conflict in test
Automatic merge failed; fix conflicts and then commit the result.
$ git reset --hard
$ git pull . origin/[branch-name]
$ git reset --hard ORIG_HEAD

Branches

After a git repository is initialized, one default branch named master is created. To list all the branches in the repository:

$ git branch
* master
branch_1
branch_2

The asterisk is placed before the name of the current branch.

To create a new branch:

$ git branch [branch-name]

To switch to a new branch:

$ git checkout [branch-name]

To create a new branch and switch to it in one command, run:

$ git checkout -b [branch-name]

To delete a branch, switch to another branch and run:

$ git branch -d [branch-name]

To see all differences between the master branch and the branch named issue_1:

$ git diff master..issue_1

To merge branches, switch to the branch you wish to merge into and then run the git merge command:

$ git checkout master
$ git merge issue_1

Occasionally, you will do different changes on the same part of the same file on two branches that you’re merging together. Then you will get conflicts which you need to resolve manually by editing the files.

Auto-merging file.html
CONFLICT (content): Merge conflict in file.html
Automatic merge failed; fix conflicts and then commit the result.

To see which files are unmerged after a merge conflict, run git status:

$ git status

After the cleaning, you need to add all unmerged files to the index with git add command and commit.

Using git rebase command can rewrite the history of a repository: all the changes that were committed on one branch can be replayed on another one.

$ git checkout [branch-name]
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: added staged command

To find out which branch contains a change:

$ git branch --contains 30f1283

For getting the one specific commit, cherry-pick can be used:

$ git cherry-pick 5e2e1a4

where ID of the commit can be found with git log.

Remote repositories

To create new remote repository, first create it on GitHub and after that run:

$ git remote add [remote-repository-name] [url]

$ git remote add origin git@github.com:my-username/my-project.git
$ git push origin master

To list the names of remote repositories:

$ git remote
origin

To list all remote-tracking branches:

$ git branch -r

To publish the changes to the existing remote repository:

$ git push [remote-repository-name] [branch-name]

To retrieve new updates from the remote repository and merge it into your local one:

$ git pull [remote-repository-name] [branch-name]

To delete the remote branch:

$ git push [remote-repository-name] :[branch-name]

To remove all branches from the local repository that have been deleted in a remote repository:

$ git remote prune [remote-repository-name]

To set up a local branch to track remote branch:

$ git branch -u [remote-repository-name]/[remote-branch-name] [local-branch-name]

It directs git pull without arguments to pull from the upstream when the new branch is checked out.

To create a new branch with the remote branch as the starting point:

$ git checkout -b [local-branch-name] [remote-repository-name]/[remote-branch-name]

Tagging

To list all tags, run:

$ git tag -l

To create a tag:

$ git tag [tag-name]

To push the created tag to the remote repository:

$ git push [remote-repository-name] [tag-name]

Stashing

To save your local modifications, put them away and revert the working directory to match the HEAD commit, use git stash. This can be useful when you modify files in the working directory and you need to fix a bug on a different branch, so you need to checkout to another branch, but don't want to commit changes on the current one. First add files that you want to stash to the git index and than use the git stash command:

$ git add [file-to-stash]
$ git stash

Now you can work on another branch and when ready, come back to the branch where you did stashing. Get back your working directory where it was before stashing with:

$ git stash pop


Home Page

Articles

:: Git For Beginners Logrotate