>>> mkdir myrepo && cd myrepo
>>> git init
Now try ls -a . Notice anything interesting?
You can also clone existing repositories from a (usually remote) different location. Git supports this via
http(s), ssh, etc.
We're going to create a remote repository on GitLab and clone it.
Please create an account on GitLab ([Link] and create a public repository called
myrepo . Then clone it to your local machine by doing
>>> git clone [Link]
>>> cd myrepo
At this point you could set some configurations.
>>> git config [Link] "emacs -nw" # or your favourite light-weight editor
>>> git config [Link] true # makes life more fun
>>> git config --list # check that it worked!
To make settings for all repositories on your computer, add the flag --global after git config .
You should also set your user name and email like this:
>>> git config [Link] "Stefan Richter"
>>> git config [Link] "[Link]@[Link]"
These will be associated with your commits.
Monitoring 1
Your first best friend in Git is the command status :
>>> git status
It shows you the files in the repository, both tracked and untracked by Git. Use this command all the time to
know what's going on.
Committing
Commit = saved snapshot of tracked files. You can always revert to a commit! You can also compare
them, share them, …
Commits are cheap. What do I mean by that?
Committing in Git works in two steps. First modified or untracked files are "registered" for the next commit
by using add . This is called staging. The staged files are then committed with commit :
Image from [Link] (license)
Note: most other VCSs (e.g. Mercurial and SVN) don't have this two-step structure. They don't have a
staging area.
>>> git add <path/to/file> # file is now staged for commit
Note: in Mercurial and SVN, add is only used to put a previously untracked file under version control. In
Git, it has a wider meaning!
>>> git commit
Then write a commit message. We'll give you hints for what is a good message.
Good commit messages matter! Here are some good recommendations (bedtime reading for you?).
Commits are identified by a unique hexadecimal number (a hash).
Monitoring 2
Your second best friend is diff . It shows you changes (differences) between versions. Without
arguments, it shows all changes made to tracked files in the repository since the last commit.
>>> git diff
>>> git diff <path/to/file>
( git diff can also be used to show differences between arbitrary revisions. You can google it.)
Use
>>> git log
to see the commit history on your current branch. I use git log -<number> a lot to only show the
<number> last commits, e.g.
>>> git log -3
A question and a suggestion:
What happens if you track files other than flat text files?
Create a hidden file .gitignore containing file patterns you want Git to ignore. These files won't
show up in git status . E.g.
*.log
*.tmp
test_data/
my_personal_notes.txt
Branches
To check which branch you are on:
>>> git branch # see where we are!
>>> git branch -a # what's the difference?
Create a new branch:
>>> git branch dev1 # dev1 is the name of the branch
Switch to the branch using checkout :
>>> git checkout dev1
>>> git branch # see where we are!
To merge my changes into another branch (let's say, master ):
>>> git checkout master
>>> git merge dev1
Working with remote repositories: sharing
See what our remote is:
>>> git remote # what's our remote
>>> git remote -v # some more info
To update the local repository (pull changes):
>>> git pull
To update the remote repository (push changes):
>>> git push origin master
When pushing the first time, do
>>> git push -u origin master # -u tells the remote to track this branch in the future
A quick word on origin and master : these are the default names of the remote repository and the