This post is a summary of a talk I was recently asked to give in front of my fellow PhD students. It basically was a git tutorial with a few extension from GitHub. I gave a live demo in which I demonstrated how I use git in my all-day work. Git really is a something worth knowing, and easy to learn (even if it sometimes doesn’t seem like it). It takes some time to pick it up, but the advantages outweigh the expenditure of time hundredfold, trust me!
GitHub is a web-based file-hosting service using git
git init git add git status git commit -m "Cookies are awesome" git push # and git pull
If you don’t know/remember what those are try typing man gittutorial
or man giteveryday
in your terminal!
To understand what git is and why you should use it, it’s best to just give it a try. So open a terminal window and let’s go!
Initialize a git repository wit
mkdir gitdemo && cd gitdemo
git init
You’ll (probably not) notice that a new directory .git
appeared!
Type ls -a
to check…
Let’s create a file to start our new project
touch index.html
Write something in it to make it a more realistic test! For example:
cat <<EOF > index.html <!DOCTYPE html> <html> <head> <title>My awesome baking recipes</title> </head> <body> <ul class="cookies"> <li>flour</li> <li>eggs</li> <li>butter</li> <li>sugar</li> <li>salt</li> <li>baking soda</li> <li>chocolate</li> </ul> </body> </html> EOF
Once we finished our work on the file, let’s check the status of our repository…
git status
Git will respond with
On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) index.html nothing added to commit but untracked files present (use "git add" to track)
It told us that it currently doesn’t track changes in our new file index.html
.
It will also propose what to do next with commands in ("")
.
So, let’s add index.html
to our git structure with
git add index.html
If multiple files were created and you’d want to add all of them at once, simply type git add .
.
Now the status says:
On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: index.html
Now the file is recognized by git. Since we’re happy with our file, let’s commit its contents with
git commit -m "Cookies are awesome"
Now the status shows
On branch master nothing to commit, working tree clean
From now on, everytime you change index.html
, git will recognize those changes and compare to the previously logged (i.e. committed) version.
To check, change the file contents of index.html
somehow and look at the difference with
sed -i '16i\ <li>water</li>' index.html
git diff index.html
To undo those changes simply type
git checkout -- index.html
Now that we have tried the recipe, eaten the cookies, and are confident that we don’t want to change it, we’re finally ready to upload our repository.
To do that we need to create a repository on GitHub.
This can be done manually (by signing in to GitHub, clicking on the “New repository” button, and following further instructions from GitHub).
However, we’re “browser-lazy”, so we’ll use GitHub’s API to do the same from the terminal.
Note: if you want to use the following two commands, sign up for an account at GitHub, replace phdenzel
with your own GitHub username and gitdemo
with your repository’s name.
curl -u phdenzel https://api.github.com/user/repos -d "{\"name\":\"gitdemo\"}"
Check on GitHub if the repo was created. If so, you might notice that it is still empty. To add a remote, an off-site copy of the same repository with which to sync up, go back to the terminal and type:
git remote add origin git@github.com:phdenzel/gitdemo.git git remote -v
To sync up with your local repository simply type
git push -u origin master
And voila! Your file was uploaded to GitHub.
You’re now able to work on the repository, expand it, etc.
After you worked on it, don’t forget to add+commit your changes with git commit -a -m "Pizza recipe"
and push the commits to GitHub with git push
.
It often happens that you pushed something accidentally and want to revert to the previous commit. This can be achieved with
git reset HEAD^ --hard git push -f
Sometimes, pulls and pushes give rise to merge conflicts, especially if more than one person works on the same file.
It happens when two lines have been changed to something else than what is expected from the remote.
Unfortunately, this isn’t automatically solved by git, so you have to fix each conflict by hand.
Git will mark the lines with <<<<<<< HEAD
, >>>>>>> BRANCH-NAME
, and divide them with =======
.
Just decide which version you like better and delete the rest, add+commit, and then push.
Another thing I find useful, is what I like to call checkpoint branching. Sometimes your project is in perfect shape, and you don’t want to mess it up by changing something. The solution is to move to a different branch and copy everything over.
Look at all current branches with
git branch -a
Now check that you are on the master
branch, branch off to a new one, and
git checkout master git branch dev
Perform your experiment on the dev
branch, where you don’t hurt the master
branch.
Then add+commit, and push the entire branch to the remote
git add .
git commit -a -m "Highly experimental"
git push -u origin dev
Once you’re finished and want to move everything to master
, merge them with
git checkout master git pull origin dev git push origin master