Difference between revisions of "Git Tutorials/Creating and Changing Repositories"

From HPC Wiki
Git Tutorials/Creating and Changing Repositories
Jump to navigation Jump to search
Line 3: Line 3:
 
{{Syllabus Git Tutorials}}
 
{{Syllabus Git Tutorials}}
  
Most of the users are only interested in having some kind of revision control for their own code. Therefore, we will start with basics, which will allow you to put your code under revision control in a few simple steps and you will also learn how to easily add changes the code (this will be done locally on your harddrive; utilizing GitHub follows later). At first you will need to create a repository for your code. To fully grasp what a repository is, you can read the corresponding Wikipedia page, but for now it is sufficient to think of it as a box into which you will put everything that you want to have under revision control. Additionally, the box contains a list, which tracks everything put into the box, everything taken out of it and any changes made to the objects inside.
+
Most of the users are only interested in having some kind of revision control for their own code. Therefore, we will start with basics, which will allow you to put your code under revision control in a few simple steps and you will also learn how to easily add changes the code (this will be done locally on your hard drive; utilizing GitHub follows later). At first you will need to create a repository for your code. To fully grasp what a repository is, you can read the corresponding Wikipedia page, but for now it is sufficient to think of it as a box into which you will put everything that you want to have under revision control. Additionally, the box contains a list, which tracks everything put into the box, everything taken out of it and any changes made to the objects inside.
  
 
This tutorial will consist of the following phases (we will stick with the box analogy for now):
 
This tutorial will consist of the following phases (we will stick with the box analogy for now):
Line 12: Line 12:
  
  
=== Creating a new repository ===
+
== Creating a new Repository ==
  
 
Before we start, please make sure that you fulfill the following conditions:
 
Before we start, please make sure that you fulfill the following conditions:
Line 23: Line 23:
 
Once those conditions are met, go into ''MyFolder'' and initialize the repository by entering
 
Once those conditions are met, go into ''MyFolder'' and initialize the repository by entering
  
<syntaxhighlight lines>
+
<syntaxhighlight lang="text" line>
 
user@HPC.NRW:~$ cd MyFolder
 
user@HPC.NRW:~$ cd MyFolder
 
user@HPC.NRW:~MyFolder/$
 
user@HPC.NRW:~MyFolder/$
Line 29: Line 29:
 
Initialized empty Git repository in /home/MyFolder/.git/
 
Initialized empty Git repository in /home/MyFolder/.git/
 
</syntaxhighlight>
 
</syntaxhighlight>
Your repository has been initialized (see line 4 above). Your box has been created, but currently it is still empty. You need to '''note''' with which content you want to fill the box/repository by using the <syntaxhighlight inline>git add</syntaxhighlight> command. You can simply '''note''' to add the whole directory with
+
Your repository has been initialized (line 4). Your box has been created, but currently it is still empty. You need to '''note''' with which content you want to fill the box/repository by using the <syntaxhighlight inline>git add</syntaxhighlight> command. You can simply '''note''' to add the whole directory with
  
 
<syntaxhighlight>
 
<syntaxhighlight>
Line 37: Line 37:
 
If you want to '''note''' the addition of one or more particular files you can do this through
 
If you want to '''note''' the addition of one or more particular files you can do this through
  
<syntaxhighlight line>
+
<syntaxhighlight>
 
user@HPC.NRW:~MyFolder/$ git add someFiles* andMore
 
user@HPC.NRW:~MyFolder/$ git add someFiles* andMore
 
</syntaxhighlight>
 
</syntaxhighlight>
 
which in this case would '''note''' to add the file ''andMore'' and any files beginning with ''someFiles'' into the repository (e.g., ''someFiles2'' or ''someFilesWithMoreTextAndNumb3rsAtTheEnd'').
 
which in this case would '''note''' to add the file ''andMore'' and any files beginning with ''someFiles'' into the repository (e.g., ''someFiles2'' or ''someFilesWithMoreTextAndNumb3rsAtTheEnd'').
  
Once you are sure what goes in, it is time to '''put''' it in the box and '''apply the changes'''. The <syntaxhighlight inline>git commit</syntaxhighlight> command is used to '''put''' something in and '''apply the changes''' something. This command has to be accompanied by a message, which describes the changes you have made to the repository. The fastest way to do this is by adding the message directly to the commit through the parameter <syntaxhighlight inline>-m</syntaxhighlight>:
+
Once you are sure what goes in, it is time to '''put''' it in the box and '''apply the changes'''. The <syntaxhighlight inline>git commit</syntaxhighlight> command is used to '''put''' something in and '''apply the changes''' something. This command has to be accompanied by a message, which describes the changes you have made to the repository. The fastest way to do this is by adding the message directly to the commit through the option <syntaxhighlight inline>-m</syntaxhighlight>:
  
 
<syntaxhighlight>
 
<syntaxhighlight>
Line 49: Line 49:
 
This command will be followed by information regarding your commit like the amount of files and changes it contained. At this point, it will also inform you that you have commited to your master branch. Branches will be covered later, so you do not need to worry about them.
 
This command will be followed by information regarding your commit like the amount of files and changes it contained. At this point, it will also inform you that you have commited to your master branch. Branches will be covered later, so you do not need to worry about them.
  
=== Modifying your repository ===
+
== Modifying your Repository ==
  
 
You actually already know everything you need to apply further changes to your repository/box. Let's assume you want to add a new file ''newFile'' to the repository and that you have also made some adjustments to the file ''andMore'', which is already inside the box. As the box is already '''created''', you only need to '''note''' what you want to add and change:
 
You actually already know everything you need to apply further changes to your repository/box. Let's assume you want to add a new file ''newFile'' to the repository and that you have also made some adjustments to the file ''andMore'', which is already inside the box. As the box is already '''created''', you only need to '''note''' what you want to add and change:
 +
 +
<syntaxhighlight>
 +
user@HPC.NRW:~MyFolder/$ git add andMore newFile
 +
</syntaxhighlight>
 +
 +
Of course you can also simply use <syntaxhighlight inline>git add .</syntaxhighlight> to '''note''' all the changes that have been made so far. Afterwards, '''put''' everything in the box and '''apply the changes''' through {{Avoid wrap|<syntaxhighlight inline>git commit -m "My commit message"</syntaxhighlight>}}. If you have not added any new files, but only made changes to existing ones, then you can type
 +
 +
<syntaxhighlight>
 +
user@HPC.NRW:~MyFolder/$ git commit -a -m "My commit message with no new files added"
 +
</syntaxhighlight>
 +
This will automatically perform the <syntaxhighlight inline>git add</syntaxhighlight> command on files which have changed since your last commit (again: No new files will be added this way!).
 +
 +
== Deciding what should never go into the Repository ==
 +
 +
Usually you want to keep your repository tidy without unnecessary file bloat. For example, this can happen when you compile your repository's code and keep the binaries inside. Entering <syntaxhighlight inline>git commit -a</syntaxhighlight> or <syntaxhighlight inline>git add .</syntaxhighlight> would result in binaries being added to the repository, which is usually not desirable as they lead to larger repository size.
 +
The easiest way to control what should not go in your repository is the <syntaxhighlight inline>.gitignore</syntaxhighlight> file. Simply create the file in your repository and note inside, what you do not want to have revision control for. If you do not want track any <syntaxhighlight inline>.txt</syntaxhighlight> files and the <syntaxhighlight inline>logo.jpg</syntaxhighlight> file in particular you could do the following:
  
 
<syntaxhighlight line>
 
<syntaxhighlight line>
user@HPC.NRW:~MyFolder/$ git add andMore newFile
+
user@HPC.NRW:~MyFolder/$ touch .gitignore
 +
user@HPC.NRW:~MyFolder/$ echo -e "*.txt\nlogo.jpg" > .gitignore
 +
</syntaxhighlight>
 +
 
 +
Naturally, you can add anything, but below you find a <syntaxhighlight inline>.gitignore</syntaxhighlight> example which contains a good list of file types that can usually be ignored.
 +
 
 +
<div style="border:2px solid #bcc0c4; background-color:#bcc0c4;" class="mw-collapsible mw-collapsed" data-expandtext="&#9660;" data-collapsetext="&#x25B2;">
 +
<span class="mw-customtoggle-toggle" style="float:right;"></span>
 +
<span style="text-align:center; padding:2px 5px; display:block;">Example of a comprehensive <syntaxhighlight inline>.gitignore</syntaxhighlight> file</span>
 +
<div id="mw-customtoggle-toggle" class="mw-collapsible-content" style="background-color:#f2f2f2; padding:2px 5px; ">
 +
<syntaxhighlight lines>
 +
# SLURM output #
 +
################
 +
*.out
 +
*.err
 +
 
 +
# Compiled source #
 +
###################
 +
*.com
 +
*.class
 +
*.dll
 +
*.exe
 +
*.o
 +
*.so
 +
 
 +
# Packages #
 +
############
 +
*.7z
 +
*.dmg
 +
*.gz
 +
*.iso
 +
*.jar
 +
*.rar
 +
*.tar
 +
*.zip
 +
 
 +
# Logs and databases #
 +
######################
 +
*.log
 +
*.sql
 +
*.sqlite
 +
 
 +
# OS generated files #
 +
######################
 +
.DS_Store
 +
.DS_Store?
 +
._*
 +
.Spotlight-V100
 +
.Trashes
 +
ehthumbs.db
 +
Thumbs.db
 +
</syntaxhighlight>
 +
</div>
 +
</div>
 +
 
 +
== Going back to a previous Version ==
 +
 
 +
One advantage of revision control is the possibility of easily going back to an older version of your repository. As with every <syntaxhighlight inline>git add</syntaxhighlight> you have '''noted''' the changes made to the repository's contents, git can use these notes to switch back to a previous state/version. In order to see all existing versions use the <syntaxhighlight inline>git log</syntaxhighlight> command:
 +
 
 +
<syntaxhighlight lang ="text" line>
 +
user@HPC.NRW:~MyFolder/$ git log
 +
commit aab2d012c5a5965d14c440a6727191c19625e6e3 (HEAD -> master)
 +
Author: user <tutorials@hpc.nrw>
 +
Date:  Thu Jul 5 14:15:09 2000 +0200
 +
 
 +
    add .gitignore file
 +
 
 +
commit 30763897a2fc05b13f3ecb3197a9243b4a7941d8
 +
Author: user <tutorials@hpc.nrw>
 +
Date:  Wed Jul 1 03:43:57 2000 +0200
 +
 
 +
    Create repository/box and fill it with some initial files
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Of course you can also simply use <syntaxhighlight inline>git add .</syntaxhighlight> to '''note''' all the changes that have been made so far. Afterwards, '''put''' everything in the box and '''apply the changes''' through <syntaxhighlight inline>git commit -m "My commit message"</syntaxhighlight>. If you have not added any new files, but only made changes to existing ones, then you can type
+
This yields information about the authors who made changes to the repository, dates, related commit messages (lines 6 and 12) and commit names/references (lines 2 and 8, the string after <syntaxhighlight inline>commit </syntaxhighlight>). You can use {{Avoid wrap|<syntaxhighlight inline>git show <commit reference></syntaxhighlight>}} for detailed information on a particular commit. The long reference is actually not required and the shortened version you get with <syntaxhighlight inline>git log --oneline</syntaxhighlight> can be used instead. Remeber to inspect the different options you can pass to <syntaxhighlight inline>git log</syntaxhighlight> for extended or filtered output.
  
 +
Once you know which version you want go back to, use the {{Avoid wrap| <syntaxhighlight inline>git checkout <commit reference> .</syntaxhighlight>}} command (with <syntaxhighlight inline>.</syntaxhighlight> at the end):
 
<syntaxhighlight line>
 
<syntaxhighlight line>
user@HPC.NRW:~MyFolder/$ git commit -a -m "My commit message with no new files added"
+
user@HPC.NRW:~MyFolder/$ git checkout 3076389 .
 +
Updated 1 path from 83ca202
 
</syntaxhighlight>
 
</syntaxhighlight>
This will automatically perform the <syntaxhighlight inline>git add</syntaxhighlight> command on the files which have changed since your last commit (again: No new files will be added this way!).
+
 
 +
Please keep in mind that you should attach an appropriate message to your next <syntaxhighlight inline>commit</syntaxhighlight> to keep everybody aware of version revert due to a <syntaxhighlight inline>checkout</syntaxhighlight>. Generally, such reverts should not be done without caution, even more so when cooperating on a project. It is preferable to use branches in such cases, a [[Git_Tutorials/Branching|topic touched later upon]].
 +
 
 +
 
 +
== Using a remote Repository ==
 +
 
 +
 
 +
== Glossary ==
 +
 
 +
 
 +
== Useful Links ==

Revision as of 17:00, 28 July 2022

Tutorial
Title: Git Tutorials
Provider: HPC.NRW

Contact: tutorials@hpc.nrw
Type: Online
Topic Area: Revision control
License: CC-BY-SA
Syllabus

1. Basic Git overview
2. Creating and Changing Repositories
3. Branching


Most of the users are only interested in having some kind of revision control for their own code. Therefore, we will start with basics, which will allow you to put your code under revision control in a few simple steps and you will also learn how to easily add changes the code (this will be done locally on your hard drive; utilizing GitHub follows later). At first you will need to create a repository for your code. To fully grasp what a repository is, you can read the corresponding Wikipedia page, but for now it is sufficient to think of it as a box into which you will put everything that you want to have under revision control. Additionally, the box contains a list, which tracks everything put into the box, everything taken out of it and any changes made to the objects inside.

This tutorial will consist of the following phases (we will stick with the box analogy for now):

  1. Initializing your repository: You create the box
  2. Adding files to the repository: You note on the list what goes in the box and what changes to apply
  3. Commiting your changes to the repository: You put the objects and apply the changes to the box as has been noted on the list


Creating a new Repository

Before we start, please make sure that you fulfill the following conditions:

  • You have some kind of Linux distribution installed
  • You have installed Git
  • You have a folder (we will call it MyFolder), that you want to put under revision control
  • You have set your identity and email through the two commands
    user@HPC.NRW:~$ git config --global user.name "Your Name Comes Here"
    user@HPC.NRW:~$ git config --global user.email you@yourdomain.example.com

Once those conditions are met, go into MyFolder and initialize the repository by entering

1user@HPC.NRW:~$ cd MyFolder
2user@HPC.NRW:~MyFolder/$
3user@HPC.NRW:~MyFolder/$ git init
4Initialized empty Git repository in /home/MyFolder/.git/

Your repository has been initialized (line 4). Your box has been created, but currently it is still empty. You need to note with which content you want to fill the box/repository by using the git add command. You can simply note to add the whole directory with

user@HPC.NRW:~MyFolder/$ git add .

If you want to note the addition of one or more particular files you can do this through

user@HPC.NRW:~MyFolder/$ git add someFiles* andMore

which in this case would note to add the file andMore and any files beginning with someFiles into the repository (e.g., someFiles2 or someFilesWithMoreTextAndNumb3rsAtTheEnd).

Once you are sure what goes in, it is time to put it in the box and apply the changes. The git commit command is used to put something in and apply the changes something. This command has to be accompanied by a message, which describes the changes you have made to the repository. The fastest way to do this is by adding the message directly to the commit through the option -m:

user@HPC.NRW:~MyFolder/$ git commit -m "Repository/Box has been created and filled with some initial files"

This command will be followed by information regarding your commit like the amount of files and changes it contained. At this point, it will also inform you that you have commited to your master branch. Branches will be covered later, so you do not need to worry about them.

Modifying your Repository

You actually already know everything you need to apply further changes to your repository/box. Let's assume you want to add a new file newFile to the repository and that you have also made some adjustments to the file andMore, which is already inside the box. As the box is already created, you only need to note what you want to add and change:

user@HPC.NRW:~MyFolder/$ git add andMore newFile

Of course you can also simply use git add . to note all the changes that have been made so far. Afterwards, put everything in the box and apply the changes through git commit -m "My commit message". If you have not added any new files, but only made changes to existing ones, then you can type

user@HPC.NRW:~MyFolder/$ git commit -a -m "My commit message with no new files added"

This will automatically perform the git add command on files which have changed since your last commit (again: No new files will be added this way!).

Deciding what should never go into the Repository

Usually you want to keep your repository tidy without unnecessary file bloat. For example, this can happen when you compile your repository's code and keep the binaries inside. Entering git commit -a or git add . would result in binaries being added to the repository, which is usually not desirable as they lead to larger repository size. The easiest way to control what should not go in your repository is the .gitignore file. Simply create the file in your repository and note inside, what you do not want to have revision control for. If you do not want track any .txt files and the logo.jpg file in particular you could do the following:

user@HPC.NRW:~MyFolder/$ touch .gitignore
user@HPC.NRW:~MyFolder/$ echo -e "*.txt\nlogo.jpg" > .gitignore

Naturally, you can add anything, but below you find a .gitignore example which contains a good list of file types that can usually be ignored.

Example of a comprehensive .gitignore file

# SLURM output #
################
*.out
*.err

# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so

# Packages #
############
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

Going back to a previous Version

One advantage of revision control is the possibility of easily going back to an older version of your repository. As with every git add you have noted the changes made to the repository's contents, git can use these notes to switch back to a previous state/version. In order to see all existing versions use the git log command:

 1user@HPC.NRW:~MyFolder/$ git log
 2commit aab2d012c5a5965d14c440a6727191c19625e6e3 (HEAD -> master)
 3Author: user <tutorials@hpc.nrw>
 4Date:   Thu Jul 5 14:15:09 2000 +0200
 5
 6    add .gitignore file
 7
 8commit 30763897a2fc05b13f3ecb3197a9243b4a7941d8
 9Author: user <tutorials@hpc.nrw>
10Date:   Wed Jul 1 03:43:57 2000 +0200
11
12    Create repository/box and fill it with some initial files

This yields information about the authors who made changes to the repository, dates, related commit messages (lines 6 and 12) and commit names/references (lines 2 and 8, the string after commit). You can use git show <commit reference> for detailed information on a particular commit. The long reference is actually not required and the shortened version you get with git log --oneline can be used instead. Remeber to inspect the different options you can pass to git log for extended or filtered output.

Once you know which version you want go back to, use the git checkout <commit reference> . command (with . at the end):

user@HPC.NRW:~MyFolder/$ git checkout 3076389 .
Updated 1 path from 83ca202

Please keep in mind that you should attach an appropriate message to your next commit to keep everybody aware of version revert due to a checkout. Generally, such reverts should not be done without caution, even more so when cooperating on a project. It is preferable to use branches in such cases, a topic touched later upon.


Using a remote Repository

Glossary

Useful Links