Git is the most popular version control system. But it can be quite confusing, so I'd advise you to read the other parts of this series to understand the core concepts, which allows you to use the more advanced features.
This article is just a quick and dirty guide that gets you started quickly.
This is a general basic workflow for using Git, each part will be explained under its own heading:
- Get a repository
- Make changes to the source code
- Add the changes to the stage (
- Commit changes from the stage (
The stage acts like a shopping cart. With a shopping cart, you add multiple products and check them all out in one transaction. The stage allows you to add changes and then commit them all in one go.
If you have the project source code on multiple machines (e.g. on your local machine as well as a production server), it's good to create a central remote repository everyone will work from. You'd then:
- Push the changes you've made to the remote respository (
- Pull the changes made by others from the remote repository (
Getting a Repository
If you want to create a new repository, run
git init in your project's root directory.
$ git init
If you have an existing repository, use
$ git clone /path/to/repository
$ git clone username@host:/path/to/repository
$ git clone email@example.com:d4nyll/test.git # Using SSH $ git clone https://firstname.lastname@example.org/d4nyll/test.git # Using HTTPS
Now you have a repository, you need to store your files in there so it can be version-controlled. Run
git status to see which files have changed since your last commit.
$ git status
You'll get something like this:
On branch master Your branch is up-to-date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: css/screen.css modified: sass/components/_content.scss modified: sass/components/_footer.scss modified: sass/components/_topbar.scss modified: sass/lib/responsive-nav/_responsive-nav.scss no changes added to commit (use "git add" and/or "git commit -a")
This shows that you have made changes to 5 files since your last commit; or if it's a new repository, everything will be untracked.
You can now do
git add <filename> to add the modified/untracked files to the stage.
git add css/screen.css
Do this for all the files you want to be staged. Alternatively, you can run
git add -A which adds all the modified/untracked files in one go.
Usually you'd stage changes that are relevant to each other - so changes relating to content updates should be in a separate commit to ones for adding a new responsive menu.
git status again to check that you have staged everything you want to. Once happy, run
$ git commit -m "Your commit message"
-m flag allows you to write a message for the commit, which helps others using the repository to quickly understand what changes you've made. If it's your first commit, people usually just write "Initial commit" as the message.
Now if you run
git log, you can see all the previous commits, the author and the time the commit was made.
$ git log
If you have a remote repository from running
git clone, you can push the changes you've made to the remote repository by running
$ git push
For your first commit, use
git push -u origin --allinstead
If you started a new repository but now wants to push to a remote repository, create the remote repository first, get the repository's address, and run:
$ git remote add origin <address>
$ git remote add origin email@example.com:d4nyll/test.git
And now run:
$ git push -u origin --all
Now you've made the changes locally and pushed to the remote repository. If you're working on different machines and want to start from the changes you last made, running
git pull will pull those changes down.
$ git pull
If it helps, you can think of
git pull the same way as
git clone - both are getting the latest version from the remote repository; but you'd only use
git clone on the first run.
Here's an example putting everything together. We're working on a local environment but want to push changes to production.
Initial Set Up
$ git init $ git add -A $ git commit -m "What I changed" $ git remote add origin firstname.lastname@example.org:d4nyll/test.git $ git push -u origin --all
$ git clone email@example.com:d4nyll/test.git
$ git add -A $ git commit -m "What I changed" $ git push
$ git pull
If you get stuck, try
$ git help
If you get stuck on a particular command, run
$ git help <command>