Version control with Git
This post outlines the essential commands for placing your code under version control with Git.
I assume you have installed Git, if not see Pro Git for information on how. I recommend the Git Reference as a good summary of the Git commands.
Initial setup
New repository setup
Change directories to the root directory of your project and initialise a new repository:
Now you can see that there is .git directory in your project.
Git uses a .gitignore file to control which files to ignore when adding to the repository. In my example I am using a Rails application and that file was automatically generated for me when I created the Rails application. Edit the .gitignore file to your liking:
Adding your project files
Change directories to the root directory of your project and add all files recursively as follows:
The files are added to a staging area. Commit the changes as follows:
The -m flag adds a message for the commit.
Check status
You may check the status of the files in your working directory and staging area as follows:
Branching and merging
I typically following a coding lifecycle of creating a branch, editing, commiting and merging the branch with the master.
Branch
Create a new branch and switch to it:
Commit
Merge
Now tidy up by deleteing the branch:
Push to remote repository
Lets push our local repository up to GitHub as an example. Create an account on GitHub if you do not already have an account. Next setup your SSH keys, see Set Up Git on how to. Create a repository on GitHub.
Now push up as follows:
I assume you have installed Git, if not see Pro Git for information on how. I recommend the Git Reference as a good summary of the Git commands.
Initial setup
After installing Git setup your environment:
git config --global user.name "Your Name" git config --global user.email your.email@foobar.com
New repository setup
Change directories to the root directory of your project and initialise a new repository:
git init
Now you can see that there is .git directory in your project.
Git uses a .gitignore file to control which files to ignore when adding to the repository. In my example I am using a Rails application and that file was automatically generated for me when I created the Rails application. Edit the .gitignore file to your liking:
# Ignore bundler config /.bundle # Ignore SQLite database. /db/*.sqlite3 # Ignore logfiles and tempfiles. /log/*.log /tmp # Ignore generated transient files and any OS specific files. In my case for OS X. doc/ *.swp *~ .project .DS_Store
Adding your project files
Change directories to the root directory of your project and add all files recursively as follows:
git add .
The files are added to a staging area. Commit the changes as follows:
git commit -m "Initial"
The -m flag adds a message for the commit.
Check status
You may check the status of the files in your working directory and staging area as follows:
git status
Branching and merging
I typically following a coding lifecycle of creating a branch, editing, commiting and merging the branch with the master.
Branch
Create a new branch and switch to it:
git checkout -b branch-name
Commit
git commit -a -m "commit-message"
Merge
git checkout master git merge branch-name
Now tidy up by deleteing the branch:
git branch -d branch-name
Push to remote repository
Lets push our local repository up to GitHub as an example. Create an account on GitHub if you do not already have an account. Next setup your SSH keys, see Set Up Git on how to. Create a repository on GitHub.
Now push up as follows:
git remote add origin git@github.com:<username>/<repository name>.git git push origin master
Comments