Table of contents
Basics
Git workflows can be divided into these main categories:
- Store - save the changes in your repository.
- Fetch - integrate the changes from a remote repository to another repository.
- Update - store your changes in a remote repository.
If you don't have access to any git repository just create a fresh one to play around with. Create and open up a new directory:
mkdir cats cd cats
Then initialize a new git repository with:
git init
As simple as that!
Store
Let's say you made some changes to index.html page in your git repository. Typing git status will always show you the current status of your working directory:
git status
Git is aware that index.html is changed and there are modifications to save. To do that, you have to create a commit. A single commit can contain multiple changes.
Use git add to put a modified file into your commit:
git add index.html
If you want to include multiple files just repeat git add for every file you want to include in this commit.
Finally, store the selected modifications:
git commit -m "my first log message"
Commits are created along with a log message which describes the changes made in that commit.
Fetch
git pull <remote> <branch>
This is one of the concepts which git uses to distribute content. This command will incorporate the changes from a remote repository (remote) to your local copy of the remote repository. To make your repository up to date with the remote repository, use:
git pull
When <remote> isn't specified, origin is used. So the equivalent of the above command would be:
git pull origin
Specifing <branch> will pull the changes from that branch into your current branch. To pull the changes from branch awesome-feature-23 to your local branch do:
git pull origin awesome-feature-23
Common errors
If you are working on the same project with other contributors (people or cats) you may get these errors after git pull:
From /a/remote/repository
* branch master -> FETCH_HEAD
error: Untracked working tree file 'myproject/example.js' would be overwritten by merge. Aborting
Another error message caused by the same problem:
From /a/remote/repository
* branch master -> FETCH_HEAD
error: Your local changes to 'foo.txt' would be overwritten by merge. Aborting. Please, commit your changes or stash them before you can merge.
Both errors indicate that you have some unsaved changes that would be overwritten by the modifications made by others to the same file.
As a solution, commit your changes, then repeat git pull.
Sleeping less than 16 hours a day, also a common error in feline species.
Share
Like other contributors of a repository, you probably want to share your changes with others. Use git push to update the remote repository with your changes.
git push origin
Oops!
Remember to use git help <command> whenever you're trouble with command syntax or behavior.
Based on my experience, here are the two, most common error scenarios, with people new to git:
Edit log message
There is a typo in your last commit message and you want to correct it:
git commit --amend
Note that this won't work if you already pushed this commit to the remote repository.
Reset previous commit
You already created a commit and forgot to include all the files you wanted. Reset your previous commit by:
git reset HEAD~1
You'll still have the changes made in this commit, only the files become uncommited.
Repeat git add and git commit to create a new commit.
Won't work if you already pushed this commit to the remote repository.