My Notes for git

The Git Notes

There are tons of walk through and tutorials for learning and understanding what is Git. These notes provide just the basic commands. Sometimes all that is needed the a quick refresh of the commands. As I find the best tutorials I'll put them in the Links section.

What is git?

Git is a version control system. Most users think of it as just for source code for developers. But it can just as easily be used for a written document.

A non-source code example would be a corporate SOP (Standard Operating Procedure) manual. Each department could create a branch to work in their section and as time goes along merge it into the master document. Each change would be tracked as to who made it and exactly what changes were made.

One of the most difficult ideas to understand the different areas of the repository. The question comes up to when a commit is done where does it go. There are four parts.

  • Working -> the files that are being worked on.
  • Staging -> the space changes go to say, "these changes are ready"
  • Local Repository -> This is the local version is final version.
  • Online Repository -> Optional, This is the repository stored on a git service like Bitbucket or Github.

Wei Wang has a nice Github page that explains how the commands move through the areas visually.

Installing Git

Most computers do not come with git preloaded. If you are using an online repository, I would recommend visiting their site and download whatever package they recommend. While any git application should work with any git service, the instructions and examples will most likely be using whatever application they recommend.

MySysGit.github.io
Download and install git from github for a windows machine.
Git on SourceForge for Mac
For Macs an installer is located on sourceforge.
git-scm
Not only multiple operating systems, also documentation and other resources.
apt-get install git-core
APT (Advanced Packaging Tool) is loaded on most Debian distribution of linux. [Ubuntu]
yum install git-core
Yum (yellowdog updater, modified) is another common linux installer. [Fedora, CentOS, Scientific Linux]

git created files

So where does Git store all this magic information? There will be a hidden directory in the repository that holds the information that git is building about the project. Optionally Git allows a little configuration within files.

.gitignore
This is a text file that contains a list of files and directories that git should ignore. Part of this list should be files containing automatically created information, files containing credentials or vendor packages would not need or maybe not wanted to be put inside a repository.

The Global Settings

Setting up the global account information is needed only once. These settings will allow git to know what information that should be

git config --global user.name "Alice"
Set up the user.name, "Alice" will be replaced by your username.
git config --global user.email "alive@example.com"
Set up the user email used for the git repository
git config --global core.editor "vim"
Optionally, the default editor can be changed to any editor.
git config --list
Lists out the Config settings. Use this to confirm the settings have been changed.

Create a local repository from scratch

If you already have a project on your pc that you want to use as the Initial commit for a git repository the steps are as follows. This can also be down in an empty folder to start a project from scratch.

git init
Initialize the git repository for that project. This needs to be done in the uppermost directory of the project. Each project must be in it's own directory.

Download an existing repository from an online source

There are a huge selection of projects available to start working on. It could be your own, a friend or co-worker or an open source project you want to work on.

git clone [url]
The url is available from the repository's web site. If it is private a user name and password will be required.

Adding Changes to the staging area

As you make changes to the project, the changes need to be added to the staging area. From the staging area the changes are then committed to the local repository.

git add --all
Add all files and directory's into the staging area. A common option here is to use a . instead of --all.
git add index.html
Add just index.html to the staging area.

Rolling back changes

Sometimes as you work, you may find the need to go back to a point earlier in the development.

git reset HEAD index.html
Takes the file index.html back out of the staging area.
git checkout .
This will bring the repository back to the point of the last commit
git checkout index.html
Roll back just index.html to the last committed version
git reset [GUID for the commit]
Take current files and reset back to the commit marked by that GUID. This is a rewrite of history. It will look like the current files were actually created at an earlier commit. This can lead to problems if someone was already at a commit being deleted.
git reset --hard [GUID for the commit]
Undo all commits back to the specified commit. As with a soft reset, this can cause issues for if the reset commits were being used somewhere else.

Branches

Creating a branch is similar to have a different time-line. The project is split so it can be worked on separately and at a later time, the two branches can be merged together. This is useful if a feature is being developed and the master branch still needs to be available for bug fixes.

git checkout -b feature/mailers
The -b tells git that a new branch needs to be started on.
git branch
List branches. A * will be marked next to the current branch.
git checkout master
Switch to the master branch.
git merge feature/mailers
From the master branch, this will bring the branch together. Any files that were edited in both branches will fail as a conflict. These will need to be edited by hand. Git will combine the conflicted files together. Each branches version will be marked with a start and stop. Once the files have been manually combined, they need to be recommitted.

Having git ignore files

While building projects, there might be files that shouldn't be considered part of the repository. This might included files used during the compling of the application. Visit git Hub'ssite for more information.

Create the .gitignore file
This text file should be in the root folder of the project.
List files to be ignored
List any files and file types that are to be ignored by git. Wildcards (*,?) are allowed.

Committing changes to the local repository

As you make changes to the project, the changes need to be added to the staging area. From the staging area the changes are then committed to the local repository.

git commit -m "Initial commit"
Commit all the files that were added, and use the message "Initial commit" and the message.
git commit -am "Initial commit"
With the 'a' attribute, modified files are sent directly to the local repository, without the need for a 'add' step. This should only be used if only edits in the files were made, files that were added or deleted may not transfer properly.

Linking the local to the online repository

For each local repository that will be connected to the online repository, these steps will need to be taken inside the local repositories folder. These only needs to be done once.

git remote add origin git@bitbucket.org:alice/project0.git
Add a remote for your repository. alice is your username, project0.git is is repository name on the website. Origin is a nickname so later the address doesn't need to be used.

Getting the SSH keys

First of all you'll need to create an account with a Repository service. There are quite a few repositories out there. I have chosen to use Bitbucket as my main account. github is probably the largest and most popular.


For security create a SSH key pair. This pair contains a public and private key. The private will stay on your computer, never to be shared. The public is given out to others. Anyone with the public key will be able to encrypt data that only the private key will be able to unlock. The advantage is that your PC and the repository will be able to move files with the need for passwords. This makes working with git aware application easy since they won't need to know your password. This isn't required, just makes the connection easier for third party software to be Git aware.

ssh-keygen
This will create a SSH key pair
ls ~/.ssh/
This is where the file is_rsa.pub is kept.
geany ~/.ssh/id_rsa.pub
Using your favorite editor, open the text file to copy the key.

Edit your account at the repository and enter the key.

Pushing changes to the online repository

After you've made changes to the files, updated the online repository is just a few commands away. Start these commands from the root directory of the project.

git add -all
Add all changed files to the local staging area.
git commit -m "a description of the changes you've made"
Commit the files added, with a description of the changes.
git push -u origin master
Push the commit files to the online repo.

Change the online repositories nickname

git remote rename origin sf
Rename the repository named origin to sf.

Pulling from an online repository

Sometimes the online repository has been modified and the local is now behind. Very quickly the local can be brought back upto date.

git remote -v
Lists the remote repositories that are associated to the local.
git fetch origin
git pull https://alice@bitbucket.org/Alice/CoolProject.git

Checking up on a repository

While working on a repository you may need to check the history or the status

git log
Displays a log of the commits made in the current repository
git log --author="Wendy"
Displays a log of the commits made in the current repository made by Wendy.
git log --pretty=oneline
Displays a log of the commits and puts each commit on a single line
git log --pretty=oneline -2
Displays a log of the commits and puts each commit on a single line but only show the 2 newest commits.
git status
Displays the status of the current git repository. Including information about the files in the folder.

Viewing Differences

While working on a repository you may need to check the differences between files.

git diff
Displays a log of the differences between the working copy and the repository.
git diff --stage
Displays a log of the differences between the staging version and the repository.

Delete a file

While working on a repository you may need to delete a file.

git rm third.txt
Removes the file third.txt from the working copy and the repository. Still needs to commit the change.

Rename a file

While working on a repository you may need to rename a file. If you just rename the file, git will think a new file appeared and a file was deleted. To make sure that git is aware a couple of other steps should be done. These extra steps are optional. The only issue is that git is unaware of the change of the filename and if someone needed to trace the version of the file back further then the rename, git would not be able to help. I would suggest playing with this.

Rename the file
Renames the file inside the project. If a git status was performed it would show the delete and add to the files.
git add home.txt
Adds the home.txt to the staging area
git rm first.txt
Removes the file first.txt from the repository. At this point a git status would show now git knows a rename was performed.
git mv first.txt second.txt
Since both files are in the same directory, this in effect renames the file.

Common Bash Commands

There are many other commands you'll need.

clear
Clears the bash window.
pwd
Prints the current working directory
cd ~
change directory to home directory
cd ..
change directory to one directory up
ls
list the files in the current directory
ls -la
List the [l]ong way, [a]ll information.
mkdir newFolder
Makes a folder with the name newFolder.
rm oldFile
Removes a file
rm -r oldFolder
Removes a folder
touch newFile
Creates an empty file.