| layout | page |
|---|---|
| title | Introduction to the git command line |
PREREQUISITE: Basic understanding of the command line.
Git is an easy to share and collaborate tool that keeps our code tracked and safe. With the following examples we understand how to deal with the daily usage of the tool.
Install command line git for OS X or Windows and open your terminal. If you are on linux you should already have git installed.
Create a directory where you will be storing all your projects. You can call it code or projects.
$ git config --global user.name "Your Name"
$ git config --global user.email "name@domain"You need an ssh key so you don't have to authenticate every time you want to commit to git.
$ mkdir practising-git
$ cd practising-git
$ git init$ echo "Learning git" > index.htmlThe above command will output
<h1>Learning git</h1>and store it in index.html. Open up the file and have a look.
$ git status$ git add .
$ git status
$ git commit -m 'this is my first command-line commit!'. will add all the files in the current directory and subdirectories. You should only use it when initialising your repository, or you can specify the file name.
$ git logFirst you need to create an account to the service of your choice (GitHub, GitLab). Then, create a new project (or repository).
Copy the information about adding an existing project to the repository which should be something like the details below.
$ git remote add origin <repository-url>
$ git push -u origin masterremote git all the remote repositories you have configured. You could have the same repository stored in many resources like GitHub and GitLab or Heroku.
The structure of the command is git remote <add|remove> <name of remote> <url of remote>
git remote
Or to see more information you can use the verbose (-v) flag
git remote -v
$ git pull origin master
Username for 'https://github.com': <your username>
Password for 'https://<username>@github.com': <your password>When you are working with remote repo is important to sync your local repo before any commit, merge or push.
$ git push origin master
$ git logUpdate the index.html file and then commit and push the changes
<html>
<head>
<title>Learning Git!</title>
</head>
<body>
<h1> Learning Git </h1>
<dl>
<dt>Initialise a git repository</dt>
<dd>git init</dd>
<dt>Add files to git</dt>
<dd>git add <filename></dd>
</dl>
</body>
</html>index.html
$ git status$ git add index.html
$ git commit -m 'updated to include the commands I learned today'
$ git push origin master$ git logEdit index.html
<html>
<head>
<title>Learning Git!</title>
</head>
<body>
<h1> Learning Git </h1>
<dl>
<dt>Initialise a git repository</dt>
<dd>git init</dd>
<dt>Add files to git</dt>
<dd>git add <filename></dd>
<dt>Checking file changes</dt>
<dd>git status</dd>
</dl>
</body>
</html>$ git status
$ git diffThe -/+ indications you can see mean
- indicates lines removed from the code.
+ indicates lines added to the code.
$ git diff
diff --git a/index.html b/index.html
index 21f15d1..c2031f1 100644
--- a/index.html
+++ b/index.html
@@ -10,6 +10,8 @@
<dd>git init</dd>
<dt>Add files to git</dt>
<dd>git add <filename></dd>
+ <dt>Checking file changes</dt>
+ <dd>git status</dd>
</dl>
</body>
</html>$ git commit -m 'Added git status description'
$ git push origin masterEdit the index.html file and then check the changes.
$ echo "oh no!" > index.htmlHave a look at the file using
git diff
$ git status
On branch 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: index.html
no changes added to commit (use "git add" and/or "git commit -a")$ git checkout -- index.htmlDon't forget to verify the changes
$ git diff
$ view index.htmlRepeat the steps below to change and commit a file
$ echo "oh not again" > index.html
$ git diff
$ git add index.html
$ git commit -am 'Oops, I just deleted my list'Can you explain the commands you just used?
$ git log
commit aafbe36777e19244ba5030cbc9467244a7163b61
Author: Jane Doe <jane@codebar.io>
Date: Tue Jun 3 21:12:57 2014 +0100
Oops, I just deleted my list
commit dbb313d28de82c11535968584ce2e149b1fc74ad
Author: Jane Doe <jane@codebar.io>
Date: Tue Jun 3 21:06:09 2014 +0100
Added git status description
commit c0bb15bf9f75613930c66760b90b2ccc1af0d2d6
...
...$ git reset HEAD^
Unstaged changes after reset:
M index.htmlThe caret (^) after HEAD moves head back through commits. HEAD^ is short for HEAD^1 and in the same way you can apply HEAD^2 to go back two commit ago.
$ git logDid you notice that the last commit is no longer there?
$ git statusDo you remember how to discard the changes? Have a look earlier in the tutorial.
You can correct something you pushed accidentally by changing history. In the following example you will see how can you revert the last pushed commit.
$ echo "this change will be soon reverted" > index.html
$ git diff
$ git commit -am 'add another broken change'
$ git push origin master
$ git status
$ git logWhat does git push do?
$ git log
commit f4d8d2c2ca851c73176641109172780487da9c1d
Author: Jane Doe <jane@codebar.io>
Date: Tue Jun 3 21:17:57 2014 +0100
add another broken change
commit dbb313d28de82c11535968584ce2e149b1fc74ad
Author: Jane Doe <jane@codebar.io>
Date: Tue Jun 3 21:06:09 2014 +0100
Added git status description
commit c0bb15bf9f75613930c66760b90b2ccc1af0d2d6
...
...
...$ git revert f4d8d2c2ca851c73176641109172780487da9c1dAfter reverting the changes you have to push the code to the remote repo to apply them
git push origin masterIf you are on OS X, check the following resources
https://help.github.com/articles/set-up-git
Create the file .gitconfig in your root directory and add the following configuration
[user]
name = <Your name>
email = <Your email>[alias]
ci = commit
dc = diff --cached
Can you think of another command that you would find handy to shorten down?
[apply]
whitespace = fix
[core]
excludesfile = ~/.gitignore
To apply this you need to create a .gitignore file in your root path. There you can add either specific files or extentions that you always want excluded. This is a handy list to help you start
*.DS_Store
*~
*.log
*.zip
*.pkg
*.rar
Do you know what these files are? You normally wouldn't want to commit logs or packages.
In your aliases add this as an alias for viewing git logs
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
Try it out by running git lg
Add HISTSIZE and HISTFILESIZE to your .bashrc file. HISTSIZE is the number of commands stored in memory when you are using the terminal. HISTFILESIZE is the number of commands stored in memory when you are using the terminal
HISTSIZE=10000
HISTFILESIZE=20000After typing a couple of command in the terminal, try executing
Ctrl+R followed by the command you want to run e.g. git lg
You can see the entire history by running
history
This ends Git: Introduction to command line tutorial. Is there something you don't understand? Try and go through the provided resources with your coach. If you have any feedback, or can think of ways to improve this tutorial send us an email and let us know.