Difference between revisions of "Git manual"

From ATI public wiki
Jump to: navigation, search
(Getting started)
((by SublimeText.Mediawiker))
 
(22 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Documentation==
+
== What is Git? ==
Git official documentation - https://git-scm.com/doc
+
Git is a most widely used version control system for developing code. Essentially what Git allows you to do is easily store the code of your project, including the history of changes and in multiple places if needed. The value of it becomes apparent when multiple people are working on one project and the changes they make to the code take different routes. Git helps with maintaining different changes throughout versions and merging them if needed.
 +
Git repositories are also a good way of making your code public to the community or keeping a portfolio of projects.
 +
To learn more about how Git works, you can start from here: [https://git-scm.com/book/en/v2/Getting-Started-Git-Basics Git Basics]
  
GitHub cheatsheet - https://training.github.com/kit/downloads/github-git-cheat-sheet.pdf
+
== Git repository ==
 +
For course related code and projects you can use the following repository:
 +
 
 +
'''[http://gitlab.pld.ttu.ee gitlab.pld.ttu.ee] '''
 +
 
 +
To login use your INTRA Uni-ID and password. Uni-ID is the same username as the one you use for logging into lab computers. If you do not know your Uni-ID and password, you can get one from ICT-410 or by logging in with ID-card or residence permit at https://pass.ttu.ee.
 +
 
 +
In addition, there are many publicly available Git repositories, like github, bitbucket, gitlab; where you can store your code.
  
 
==Downloading Git==
 
==Downloading Git==
Line 10: Line 19:
  
 
==Getting started==
 
==Getting started==
Each student has already a repository generated beforehand with URL looking like this:
 
 
https://git.ttu.ee/ained/iag0581/username
 
 
For authentication your Uni-ID will be used. (The same username and password that you use for logging in to the lab computers).
 
 
 
Assuming that Git is successfully installed, in Windows open start menu and search for '''Git Bash''' and open it. In Linux or Mac simply open terminal.
 
Assuming that Git is successfully installed, in Windows open start menu and search for '''Git Bash''' and open it. In Linux or Mac simply open terminal.
  
There is also a short video showing how to clone your repository: [http://ati.ttu.ee/~hkinks/iag0581/git_tutorial.mp4 Video link]
+
''Note: Under Windows's Git Bash Ctrl+C and Ctrl+V do not work. If you wish to paste something into Git Bash window, use Insert.''
 +
 
 
===Cloning===
 
===Cloning===
 
Let's say that we want to clone the repository to our local directory at C:\workspace. First we have to change the current directory by typing to terminal:
 
Let's say that we want to clone the repository to our local directory at C:\workspace. First we have to change the current directory by typing to terminal:
 
<source lang="bash" collapse="false">cd /c/workspace</source>
 
<source lang="bash" collapse="false">cd /c/workspace</source>
  
For example if your Uni-ID is john.doe then for cloning your existing repository from ''https://git.ttu.ee/ained/iag0581/john.doe'', type in:
+
For example if your Uni-ID is ''john.doe'' and you have made the repository with a name ''myrepo'' then for cloning your existing repository from ''http://gitlab.pld.ttu.ee/john.doe/myrepo.git'', type in:
<source lang="bash" collapse="false">git clone https://git.ttu.ee/ained/iag0581/john.doe iag0581</source>
+
<source lang="bash" collapse="false">git clone http://gitlab.pld.ttu.ee/john.doe/myrepo.git</source>
Next you will be asked username and password. Username is your Uni-ID, which is the same as the one you use for logging into lab computers. If you do not know your Uni-ID and password, you can get one from ICT-410 or by logging in with ID-card or residence permit at https://pass.ttu.ee .
+
Next you will be asked username and password (Uni-ID). As you are typing in password, you will not see the input as asterisk (*) symbols like you might be used to. However, the password input will still work as long as it is the correct password. After that you should be seeing message like:  
As you are typing in password, you will not see the input as asterisk (*) symbols like you might be used to. However, the password input will still work as long as it is the correct password. After that you should be seeing message like:  
+
 
  warning: You appear to have clone and empty repository.
 
  warning: You appear to have clone and empty repository.
 
  Checking connectivity.. done.
 
  Checking connectivity.. done.
 
Do not be alarmed by the warning, it is normal to see that as you are first starting off with an empty repository
 
Do not be alarmed by the warning, it is normal to see that as you are first starting off with an empty repository
 
   
 
   
After you authenticate yourself succesfully, the repository is cloned into the local directory C:\workspace\iag0581 and we can change also our current directory to it by typing:
+
After you authenticate yourself succesfully, the repository is cloned into the local directory C:\workspace\myrepo and we can change also our current directory to it by typing:
<source lang="bash" collapse="false">cd iag0581</source>
+
<source lang="bash" collapse="false">cd myrepo</source>
  
 
===Staging area===
 
===Staging area===
Next we can start adding files for version control. Let's say that we already have some files in another directory (C:\workspace\lab1) that we wish to add to version control. For copying files in terminal there is a command '''cp'''.
+
Next we can start adding files for version control. Let's say that we already have some files in another directory (C:\workspace\lab1) that we wish to add to version control. Use file manager to copy the files into the repository or for copying files from the terminal there is a command '''cp'''.
<source lang="bash" collapse="false">cp C:\workspace\lab1 C:\workspace\iag0581 -rf</source>
+
 
 +
<source lang="bash" collapse="false">cp /c/workspace/lab1 /c/workspace/myrepo -rf</source>
 +
 
 +
''Note: Git bash on Windows OS uses Unix notation for directory paths. So instead of 'C:\example', you would have to use '/c/example'.''
 +
 
 
To make sure that the files copied over successfully we can see the contents of the current directory by entering command '''ls'''.
 
To make sure that the files copied over successfully we can see the contents of the current directory by entering command '''ls'''.
 
Next we have to notify git that we have files that we wish to add to version control. Git has so called staging area, which is used to temporarily hold, prepare and review changes to be made. To add files into the staging area, command '''git add <filename>''' is used. So if we wish to add the previously copied folder, type:
 
Next we have to notify git that we have files that we wish to add to version control. Git has so called staging area, which is used to temporarily hold, prepare and review changes to be made. To add files into the staging area, command '''git add <filename>''' is used. So if we wish to add the previously copied folder, type:
 
<source lang="bash" collapse="false">git add lab1</source>
 
<source lang="bash" collapse="false">git add lab1</source>
 +
Or if we simply want to add all the files, we can insert:
 +
<source lang="bash">git add *</source>
 
To see all the files in the staging area for making sure we are ready to commit the changes, there is a command '''git status''' which lists all the new and modified files to be committed.
 
To see all the files in the staging area for making sure we are ready to commit the changes, there is a command '''git status''' which lists all the new and modified files to be committed.
===Committing===
+
 
Before committing it is required to set up your name and e-mail with git. To do that there are following commands that should be inserted:
+
=== Identifying yourself ===
 +
Before continuing and committing the changes we made it is required to set up your name and e-mail with Git in order to let the repository know who is making the commits. In the class computers it should be automatically set. Othwerise, to do that there are following commands that should be inserted:
 
<source lang="bash" collapse="false">
 
<source lang="bash" collapse="false">
 
   git config --global user.email "you@example.com"
 
   git config --global user.email "you@example.com"
 
   git config --global user.name "Your Name"
 
   git config --global user.name "Your Name"
 
</source>
 
</source>
Next we can commit the changes with the command:
+
===Committing===
 +
Next we can commit the changes. Commiting essentially means we are creating a new 'version' of our code to which we can return later on if there happens to be need for restoring any of the code.
 +
To make a commit, type in the command:
 
<source lang="bash" collapse="false">git commit -am "your comment about the commit"</source>
 
<source lang="bash" collapse="false">git commit -am "your comment about the commit"</source>
 
===Pushing===
 
===Pushing===
 
Now that the changes are stored locally we may want to push the changes back to remote repository also. To do that there is the command '''git push'''
 
Now that the changes are stored locally we may want to push the changes back to remote repository also. To do that there is the command '''git push'''
 
<source lang="bash" collapse="false">git push origin master</source>
 
<source lang="bash" collapse="false">git push origin master</source>
Origin and master specify to which remote repository and branch the commit will be pushed into.
+
Origin and master specify to which remote repository and branch the commit will be pushed into. For later commits they can be omitted if default and repository '''origin''' and branch '''master''' is used.
 
Next your username and password will be asked. (The same username and password that you use for logging in to the lab computers).
 
Next your username and password will be asked. (The same username and password that you use for logging in to the lab computers).
 +
If you want to make sure that the push was successfully made, you can simply clone your repository in another folder and see if the contents have been updated.
 
===Pulling===
 
===Pulling===
 
For the next scenario, let's say that we have repositories cloned locally to both lab computer and computer at home. Now when we have made changes to repository while working from lab and we want to update the local repository from home, '''git pull''' is used. Git pull incorporates changes from a remote repository into the current local branch.
 
For the next scenario, let's say that we have repositories cloned locally to both lab computer and computer at home. Now when we have made changes to repository while working from lab and we want to update the local repository from home, '''git pull''' is used. Git pull incorporates changes from a remote repository into the current local branch.
 
<source lang="bash" collapse="false">git pull</source>
 
<source lang="bash" collapse="false">git pull</source>
  
==Other useful (but more advanced) commands==
+
==Other useful commands==
 
See graphical revision history.
 
See graphical revision history.
 
<source lang="bash" collapse="false">gitk</source>
 
<source lang="bash" collapse="false">gitk</source>
Line 66: Line 79:
 
Undo last commit. (source: http://stackoverflow.com/questions/927358/how-do-you-undo-the-last-commit)
 
Undo last commit. (source: http://stackoverflow.com/questions/927358/how-do-you-undo-the-last-commit)
 
<source lang="bash" collapse="false">git reset --soft HEAD~1</source>  
 
<source lang="bash" collapse="false">git reset --soft HEAD~1</source>  
For more advanced usage it is recommended to read documentations on the internet. Good places to start are Git official documentation - https://git-scm.com/doc and for quick reminders - https://training.github.com/kit/downloads/github-git-cheat-sheet.pdf .
 
  
 
=== Branching ===
 
=== Branching ===
Line 80: Line 92:
 
For deleting:
 
For deleting:
 
<source lang="bash" collapse="false">git branch -D <branch name></source>
 
<source lang="bash" collapse="false">git branch -D <branch name></source>
 +
 +
== Git commands to remember ==
 +
  git clone <url>
 +
  git add .
 +
  git commit -m "comment"
 +
  git push
 +
  git pull
 +
 +
==Documentation==
 +
For more advanced usage it is recommended to read documentations on the internet. Good places to start are
 +
 +
Git official documentation - https://git-scm.com/doc
 +
 +
GitHub cheatsheet - https://training.github.com/kit/downloads/github-git-cheat-sheet.pdf

Latest revision as of 12:42, 15 September 2016

What is Git?

Git is a most widely used version control system for developing code. Essentially what Git allows you to do is easily store the code of your project, including the history of changes and in multiple places if needed. The value of it becomes apparent when multiple people are working on one project and the changes they make to the code take different routes. Git helps with maintaining different changes throughout versions and merging them if needed. Git repositories are also a good way of making your code public to the community or keeping a portfolio of projects. To learn more about how Git works, you can start from here: Git Basics

Git repository

For course related code and projects you can use the following repository:

gitlab.pld.ttu.ee

To login use your INTRA Uni-ID and password. Uni-ID is the same username as the one you use for logging into lab computers. If you do not know your Uni-ID and password, you can get one from ICT-410 or by logging in with ID-card or residence permit at https://pass.ttu.ee.

In addition, there are many publicly available Git repositories, like github, bitbucket, gitlab; where you can store your code.

Downloading Git

https://git-scm.com/downloads

When installing Git on Windows operating system, installation settings can be set as default. (Use Git from Git Bash only; Checkout Windows-style, commit Unix-stile line endings; Use MinTTY)

Getting started

Assuming that Git is successfully installed, in Windows open start menu and search for Git Bash and open it. In Linux or Mac simply open terminal.

Note: Under Windows's Git Bash Ctrl+C and Ctrl+V do not work. If you wish to paste something into Git Bash window, use Insert.

Cloning

Let's say that we want to clone the repository to our local directory at C:\workspace. First we have to change the current directory by typing to terminal:

cd /c/workspace

For example if your Uni-ID is john.doe and you have made the repository with a name myrepo then for cloning your existing repository from http://gitlab.pld.ttu.ee/john.doe/myrepo.git, type in:

git clone http://gitlab.pld.ttu.ee/john.doe/myrepo.git

Next you will be asked username and password (Uni-ID). As you are typing in password, you will not see the input as asterisk (*) symbols like you might be used to. However, the password input will still work as long as it is the correct password. After that you should be seeing message like:

warning: You appear to have clone and empty repository.
Checking connectivity.. done.

Do not be alarmed by the warning, it is normal to see that as you are first starting off with an empty repository

After you authenticate yourself succesfully, the repository is cloned into the local directory C:\workspace\myrepo and we can change also our current directory to it by typing:

cd myrepo

Staging area

Next we can start adding files for version control. Let's say that we already have some files in another directory (C:\workspace\lab1) that we wish to add to version control. Use file manager to copy the files into the repository or for copying files from the terminal there is a command cp.

cp /c/workspace/lab1 /c/workspace/myrepo -rf

Note: Git bash on Windows OS uses Unix notation for directory paths. So instead of 'C:\example', you would have to use '/c/example'.

To make sure that the files copied over successfully we can see the contents of the current directory by entering command ls. Next we have to notify git that we have files that we wish to add to version control. Git has so called staging area, which is used to temporarily hold, prepare and review changes to be made. To add files into the staging area, command git add <filename> is used. So if we wish to add the previously copied folder, type:

git add lab1

Or if we simply want to add all the files, we can insert:

git add *

To see all the files in the staging area for making sure we are ready to commit the changes, there is a command git status which lists all the new and modified files to be committed.

Identifying yourself

Before continuing and committing the changes we made it is required to set up your name and e-mail with Git in order to let the repository know who is making the commits. In the class computers it should be automatically set. Othwerise, to do that there are following commands that should be inserted:

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

Committing

Next we can commit the changes. Commiting essentially means we are creating a new 'version' of our code to which we can return later on if there happens to be need for restoring any of the code. To make a commit, type in the command:

git commit -am "your comment about the commit"

Pushing

Now that the changes are stored locally we may want to push the changes back to remote repository also. To do that there is the command git push

git push origin master

Origin and master specify to which remote repository and branch the commit will be pushed into. For later commits they can be omitted if default and repository origin and branch master is used. Next your username and password will be asked. (The same username and password that you use for logging in to the lab computers). If you want to make sure that the push was successfully made, you can simply clone your repository in another folder and see if the contents have been updated.

Pulling

For the next scenario, let's say that we have repositories cloned locally to both lab computer and computer at home. Now when we have made changes to repository while working from lab and we want to update the local repository from home, git pull is used. Git pull incorporates changes from a remote repository into the current local branch.

git pull

Other useful commands

See graphical revision history.

gitk

Checkout earlier version. Hash can be found out using gitk or git log for the corresponding version (Looks something like b22756ed3s...). It is not required to supply the whole hash, first unique characters are enough.

git checkout <hash>
git checkout b22

Undo last commit. (source: http://stackoverflow.com/questions/927358/how-do-you-undo-the-last-commit)

git reset --soft HEAD~1

Branching

Using branching can make managing and organizing your repository easier. It can be used for example, if you want to try out new feature without making edits to your master branch. To create a new branch, insert

git branch <branch name>

To check out newly created branch

git checkout <branch name>

For switching back to master branch

git checkout master

To see existing branches:

git branch

For deleting:

git branch -D <branch name>

Git commands to remember

 git clone <url>
 git add .
 git commit -m "comment"
 git push
 git pull

Documentation

For more advanced usage it is recommended to read documentations on the internet. Good places to start are

Git official documentation - https://git-scm.com/doc

GitHub cheatsheet - https://training.github.com/kit/downloads/github-git-cheat-sheet.pdf