Octet

Getting Started with Git

Abstract

This webpage has a collection of notes on the software tool Git. Git is a distributed revision control and source code management (SCM) system. This page focuses on the following topics:

  • Installing Git on Windows, Mac, and Linux/Unix.
  • Configuring Git.
  • Git commands.

It is targeted towards computer programmers who are new to Git. It is also targeted towards computer programmers who are accustomed to using the graphical interface of the Windows program Git Extensions, but would like to become more familiar with the Git command line options.

Introduction

Git is a software tool. It is a distributed revision control and source code management (SCM) system that is both free and open source.[1][2] It is also:

  • Fast — benchmarking faster than the common alternative Subversion (SVN) in many categories.[2]
  • Popular — with millions of repositories.[3]
  • Secure — since it can be configured to use secure protocols, such as the https protocol or secure shell (SSH) protocol.[4]

It was developed initially by Linus Torvalds (the creator of Linux) in 2005.

History

“Git development began after many developers of the Linux kernel chose to give up access to BitKeeper, a proprietary SCM system that had previously been used to maintain the project.”[1]

  • (2002) The Linux kernel began using BitKeeper.[4]
  • (2005) The BitKeeper tool was no longer offered for free.[4]
  • (2005-04-06) Development of Git begins with about 1300 lines of code from Linus.[5]
  • (2005-07-26) Maintenance of Git was turned over to a major contributor.[6]

Since 2005 Git has steadily grown in popularity and usage.

Installation

Installation can be done on Windows, Mac, and Linux/Unix operating systems. The official documentation on installation can be found here:

Many package managers in the Linux world can install Git with a single line at the command prompt. For Windows, msysgit is preferred.

Configuration

There are three levels of configuration in Git: Global, System, and User. This video explains the basics,and there are more like it at git-scm.com/videos:

The official description of configuration in Git from the book is as follows:

Git uses a series of configuration files to determine non-default behavior that you may want. The first place Git looks for these values is in an /etc/gitconfig file, which contains values for every user on the system and all of their repositories. If you pass the option --system to git config, it reads and writes from this file specifically.

The next place Git looks is the ~/.gitconfig file, which is specific to each user. You can make Git read and write to this file by passing the --global option.

Finally, Git looks for configuration values in the config file in the Git directory (.git/config) of whatever repository you’re currently using. These values are specific to that single repository. Each level overwrites values in the previous level, so values in .git/config trump those in /etc/gitconfig, for instance. You can also set these values by manually editing the file and inserting the correct syntax, but it’s generally easier to run the git config command.

The configuration files can be editted manually as well with any text editor.

Commands

Interaction with Git can be done primarily through commands at a command line interface. There are graphical interfaces as well, such as Git Extensions for Windows, and Microsoft Visual Studio extensions, but this section focuses on the commands.


-------------------------------------------------------------------------------
	INIT, COMMIT, CONFIG, CHECKOUT
-------------------------------------------------------------------------------
These commands are used to get the environment up and running.

INIT AND COMMIT
git init // Turns your current directory into a Git repository.

git commit // Record changes made to the repository. Will prompt for a message.
git commit -m "Inline commit message" // Commit with the message specified.
git commit -am "Inline commit message" // Commit any modified files.

CONFIGURATION
git config --global color.ui auto // Colorize output in terminal.
git config --global alias.lga "log --graph --oneline --all --decorate" // Log graph all(lga) alias.[8]

git config --global core.autocrlf true  // Change how Git handles line endings.
git config --global core.autocrlf false // Good for Windows. However, can cause issues with Linux or Mac later on.
git config --global core.autocrlf true  // Good for Windows. Store with just line feeds.
git config --global core.autocrlf input // Good for Linux/Unix environments.

git config --global --list // Show all global configuration.
git config --global push.default simple // Set this to default push the current branch rather than all.

CHECKOUT
"Checkout a branch or paths to the working tree"
git checkout <branch>
git checkout -b <branch>

-------------------------------------------------------------------------------
	GIT IGNORE, RESETTING CHANGES, CLEANING OUT TEMPORARY FILES
-------------------------------------------------------------------------------
These commands help keep your repository and local directory fresh and clean.

ADDING FILES TO .GITIGNORE
git add .gitignore

RESETTING CHANGES
git reset --hard        // Removes all of the changes that were made.
git reset --hard HEAD~1 // Rollback to one commit previous.
git reset --soft HEAD~1 // Soft reset to the previous commit. Does not remove staged changes.

CLEANING OUT TEMPORARY FILES
git clean -n // This tells you what it would do if it were to perform a clean.
git clean -f // This forces the clean to happen.


-------------------------------------------------------------------------------
	LOG, STATUS, SHOW
-------------------------------------------------------------------------------
Commands that help you see what's going on.

LOG
git log // Shows the commit logs.
git log --oneline |  wc -l  // Shows how many commits. This uses the wordcount option.
git log --oneline --graph // Shows a graph on the left hand side.
git log --format=short
git log --graph --oneline
git log --graph --oneline --all --decorate
git log origin/master

git shortlog // Lists the authors and message from them, and number of commits.
git shortlog -sne // Summary, number of commits, email.

git reflog // Manage reflog information..

STATUS
git status  // Shows all pending changes.

SHOW
git show HEAD
git show HEAD~10
git show 564262

-------------------------------------------------------------------------------
	DIFFING
-------------------------------------------------------------------------------
Sometimes it is important to compare commits and other things.

DIFFING
git diff HEAD
git diff HEAD~1..HEAD   // Compare the current commit with the previous one.
git diff HEAD~1		// Compare the current commit with the previous one (shorthand).
git diff dd6819..a15ec6 // Compare two commits.
git diff --cached       // Compare repository to staging area.
git diff dev qa         // Compare two commits.

-------------------------------------------------------------------------------
	WORKING WITH REMOTE REPOSITORIES
-------------------------------------------------------------------------------

CLONE
git clone  https://github.com/jquery/jquery.git // Clone the remote repository jQuery.

LISTING AND ADDING A REMOTE REPOSITORY
git remote -v // List remote repositories. Won't list the local ones.
git remote add origin  [url to .git file]

FETCH
It is very common to need to fetch data from the remote repository.

git fetch // Will fetch any changes from a remote repository
git fetch origin

PULLING
Pulling is a Git fetch followed by a Git merge. This is also very common.

git pull
git pull origin/master

PUSHING
Pushing lets you get your changes into the remote repository.

git push
git push --tags // You must specify --tags to push any tags.

ADDING FILES TO A REPOSITORY
git add -u  // Adds all updated / modified files.
git add -A  // Adds all files including  untracked ones.


BRANCHING
Branches are good for things like separating main line development from bug fixes.

git branch  // Shows all branches in the repository.
git branch -r // Shows remote branches.
git branch feature1 //

MERGING
Merging allows you to "join two or more development histories together."
https://www.kernel.org/pub/software/scm/git/docs/git-merge.html

Merging is a common operation in Git.
If there is a conflict, it lets you know.

git merge origin/master

git merge feature1
git branch -d feature1

git merge feature2_additional
git mergetool

Note: The default merge tool is kdiff3 that will come up, but others can be used.

REBASING
git rebase master

-------------------------------------------------------------------------------
	RENAMING, DELETING
-------------------------------------------------------------------------------
RENAMING BRANCHES
git branch -m fix1 bug1234  // Rename using -m (move) command.

DELETING BRANCHES
git branch -d bug1234   // Will yell at you that the branch is not merged.
git branch -D bug1234 // Will force it.

RECOVERING DELETED COMMITS
git reflog // Step 1: Check the reflog to find which one you want to recover.
git branch bug1234 5178b // Step 2: Create a new branch.
git checkout bug1234 // Step 3: Check it out.

Note: Git keeps deleted branches around for 30 days by default. It then may
clean them out automatically with a garbage collection mechanism.

-------------------------------------------------------------------------------
	TAGS
-------------------------------------------------------------------------------
TAGS
git tag // Shows all versions that have been tagged.
git tag v1.0 // Tag a branch with a specific version.

git tag -a v1.0_with_message  // Allows you to tag with a message.
git tag -a v1.0_with_message  -m  ""  //  Message inline.
git tag -s v1.0_signed // If you are signing a tag, requires a message.
git tag -v v1.0_signed // Verifying a signed tag.

-------------------------------------------------------------------------------
	STASHING
-------------------------------------------------------------------------------
Git keeps a stash area for pending changes. You can store things in here.

git stash
git stash list
git stash apply

git stash pop // Same as apply but removes it from my stash list.
git stash drop
git stash branch feature2

-------------------------------------------------------------------------------
	CHERRY PICKING
-------------------------------------------------------------------------------
git cherry-pick 6fa324

-------------------------------------------------------------------------------
	CREATING AND DELETING REMOTE BRANCHES
-------------------------------------------------------------------------------
CREATING A REMOTE BRANCH
Creating a remote branch is as easy. You can use this command:
git push <remote-name> <branch-name>

Or more formally:
git push <remote-name> <local-branch-name>:<remote-branch-name>

When initially creating a remote branch, the remote-name is origin and the
remote-branch-name is master:

git push origin master


DELETING A REMOTE BRANCH
Deleting a remote branch works a little unexpectedly. You give the push command
along with blank for the local branch, and the name of the remote branch.

git push origin :remote_branch_name

-------------------------------------------------------------------------------
	PROTOCOLS
-------------------------------------------------------------------------------
In order to get up and running, certain ports may need to be opened up depending
on your configuration.

- Git may use http or https. These protocols operate on port 80/443 by default.
- Git may use the Git protocol. The Git Protocol operates on 9418.
- Git may use SSH. The SSH Protocol operates on port 22 (the telnet port).

-------------------------------------------------------------------------------
	A FULL EXAMPLE
-------------------------------------------------------------------------------
Say for example you wanted to checkout the dev branch. Then pull down the latest
changes. The dialogue might look like this:

// Checkout the branch.
git checkout qa
	Switched to branch 'qa'

// Check the status.
git status
	On branch qa

// Pull latest changes.
git pull origin qa
	…
	1 file changed, 2 insertions(+), 2 deletions(-)

// Run the log command to get an idea what commit you want to cherry-pick from dev to qa.
git log dev

	Commit 38000735…
	Author: John Smith
	Date: …

	     …

// Cherry pick that commit.
git cherry-pick 3800
 7 files changed, 118 insertions(=), 77 deletions(-)

// Double check your status that you are ready to push (it should auto-commit it).
git status

// Go ahead and push those changes into the remote repository.
git push origin qa

Please refer to the Git Reference manual for official documentation, and formal descriptions of these commands. For more information on learning Git and getting used to it, the Git home page has videos, the book, and reference material.

References

These websites were referenced:

[1] Git homepage.
http://git-scm.com/
[3] GitHub Blog - Those are some big numbers.
https://github.com/blog/841-those-are-some-big-numbers
[5] Trivia: When did git self-host? - MARC.
http://marc.info/?l=git&m=117254154130732
[6] Meet the new maintainer - MARC.
http://marc.info/?l=git&m=112243466603239
[7] gitref.org
http://gitref.org/
[8] Git Fundamentals - Pluralsight
pluralsight.com

Last updated 2014-01-13.