On the Git hompage (Git documentation) you find
Before we can use Git we need to install it:
git --version
.$ git --versiongit version 2.34.1.windows.1
Before we can use Git we need to install it:
git --version
.$ git --versiongit version 2.34.1.windows.1
git config --global user.name "John Doe"git config --global user.email johndoe@example.com
There are two ways to get a Git repository:
clone
an existing repository. We start by creating a Git repository from scratch:
C:\myProject
. c:cd myProject
There are two ways to get a Git repository:
clone
an existing repository. We start by creating a Git repository from scratch:
C:\myProject
. c:cd myProject
git init
git init
creates a new subdirectory named .git
that contains all repository files.
Git dosen't keep track of all your changes automatically. You have to tell
Git when you want to save (commit
) a new status.
Before we can commit
anything we need to create some files. E.g., a HelloGit.txt
file with the content
Hello World!How are you?
git status
git status
is the first git command you will constantly use.
It gives you a short overview about what changed since your last
commit
.
C:\myProject>git statusOn branch masterInitial commitUntracked files: (use "git add <file>..." to include in what will be committed) HelloGit.txtnothing added to commit but untracked files present (use "git add" to track)
git status
git status
is the first git command you will constantly use.
It gives you a short overview about what changed since your last
commit
.
C:\myProject>git statusOn branch masterInitial commitUntracked files: (use "git add <file>..." to include in what will be committed) HelloGit.txtnothing added to commit but untracked files present (use "git add" to track)
The nice thing about git status
is that it tells you what to do next to
commit changes to the Git repository.
git add
The first step to commit changes to the repository is to use git add <files_to_add>
.
git add HelloGit.txt
git add
The first step to commit changes to the repository is to use git add <files_to_add>
.
git add HelloGit.txt
Running git status
again gives
Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: HelloGit.txt
The listed files (in our case there is only one) are ready to be committed. The files are now in the so called staging area.
git commit
With the basic command git commit
all files in the staging area are commited to the git repository. There are some things to note:
git commit
in the terminal by default opens the text editor VIM. git commit
With the basic command git commit
all files in the staging area are commited to the git repository. There are some things to note:
git commit
in the terminal by default opens the text editor VIM. git config --global core.editor notepad
The first line of what you type in the editor should have less than 50 characters. It is used as the title of the commit.
After a blank line you can give a more thorough description of what you have done.
If you don't want to give a long description than you can also use
git commit -m "A short commit message"
to commit the files in the staging area without opening an editor.
The first line of what you type in the editor should have less than 50 characters. It is used as the title of the commit.
After a blank line you can give a more thorough description of what you have done.
If you don't want to give a long description than you can also use
git commit -m "A short commit message"
to commit the files in the staging area without opening an editor.
git commit - m "initial commit"
HelloGit.txt
by replacing Hello World!
with Hello Git!
git log
git log
returns the commit history of your project. We see by whom and when a commit was made together with a short commit message. C:\MyProject>git logcommit dfc8e86ac16116ed574884d171769b6e0e35c2f9Author: jens.klenke <jens.klenke@uni-due.de>Date: Tue Apr 19 15:26:58 2022 +0200 Welcome Git with Hello Git!commit 2f17088a3831efd6870ffc8ca54c93df6b39035dAuthor: jens.klenke <jens.klenke@uni-due.deDate: Wed Apr 13 15:02:02 2022 +0200 Initial commit!
Learn more about git log
.
git diff
git diff
we can compare files before and after a commit. To
know what exactly changed in our last commit we compare it to our first commit:C:\MyProject>git diff 2f17..dfc8diff --git a/HelloGit.txt b/HelloGit.txtindex 4f6866b..71e6eca 100644--- a/HelloGit.txt+++ b/HelloGit.txt@@ -1,2 +1,2 @@-Hello World!+Hello Git!
git diff
git diff
we can compare files before and after a commit. To
know what exactly changed in our last commit we compare it to our first commit:C:\MyProject>git diff 2f17..dfc8diff --git a/HelloGit.txt b/HelloGit.txtindex 4f6866b..71e6eca 100644--- a/HelloGit.txt+++ b/HelloGit.txt@@ -1,2 +1,2 @@-Hello World!+Hello Git!
git diff
Change again something in the HelloGit.txt
file. If you now run
git diff
you see all unstaged (everything that is not added yet) changes since the last commit.
git rm
Use git rm
to remove files from git. The command does 2 things:
After using git rm
you have to commit the removal doing a standard commit.
git rm HelloGit.txtgit commit -m "remove HelloGit.txt"
git rm
Use git rm
to remove files from git. The command does 2 things:
After using git rm
you have to commit the removal doing a standard commit.
git rm HelloGit.txtgit commit -m "remove HelloGit.txt"
git rm --cached HelloGit.txt
git checkout
C:\MyProject>git log --pretty=oneline332fa3c6ea743a8d9ac34f3c5db94ed6c9e9ed20 add important filec3ed28cbdf6f7419242ad0747806d309954211f9 important change5d3eb1b76baf5921b0b8babc2ba7c3fffb82666a added newFile.txt86214741766959936c09b1ecaf213dd6d749ef60 deleted HelloGit.txt27d048c3bebda441db44b9506080b4784dcc728b initial commit
HelloGit.txt
and you want it back.
With the command git checkout <SHA><path>
files can be brought back to their state of the specified commit. git checkout 27d048 newFile.txt
git reset
git reset --hard 27d048
git reset
git reset --hard 27d048
However, all changes after that commit will be discarded.
To keep changes in the directory of your computer use
git reset 27d048
27d048
you want to keep or discard since they are now all in the modified files stage. HelloGit.txt
in your repository and add it to the staging area. HelloGit.txt
. For this, remove first HelloGit.txt
from the staging area (git status
will help you). Now make some changes and commit them. HelloGit.txt
file (i.e. the state after your initial commit). Until now our repository only lives on our local machine. To collaborate with others you want to use a remote repository which everybody has access to.
Github provides a platform where you can host your remote repository for free.
Set up a remote repository:
Login to your Github account and go to + → New repository
Create repository
git remote add origin https://github.com/alexandergerber/test.gitgit push -u origin master
git push
Every commit
changes the Git repository on your local machine. To update
the remote repository on Github you also need to push
your local commits.
Add a new file to your repository. Then add, commit and push the new file.
C:\myProject>git push
git push -u origin master
.
After that git push
is enough and origin
is used as the default upstream branch. In case you start
working with branches you need to learn more about this. Find somebody in the classroom to collaborate with.
Find somebody in the classroom to collaborate with.
Person A adds the other to his Github repository.
Find somebody in the classroom to collaborate with.
Person A adds the other to his Github repository.
The one who was added to the repository clones the repository on his local machine:
git clone
like below (change the repository url to that of your collaborator). git clone https://github.com/jens-klenke/HelloGit.git
Find somebody in the classroom to collaborate with.
Person A adds the other to his Github repository.
The one who was added to the repository clones the repository on his local machine:
git clone
like below (change the repository url to that of your collaborator). git clone https://github.com/jens-klenke/HelloGit.git
Now both can work on the same project.
git fetch
and git merge
If more than one person works on a project it will happen that you are locally behind the remote repository.
To update your local repository to the newest status:
git fetch
. git merge
. This should run smoothly if you didn't work on the project in the meantime.
git status
(inspect the result) and merges the changes into his repository. Then do the same thing the other way arround. HelloGit.txt
file looked like thisHello GitHow are you?
HelloGit.txt
file looked like thisHello GitHow are you?
Hello Git,How are you? I think I shoudld work on this file.
HelloGit.txt
file looked like thisHello GitHow are you?
Hello Git,How are you? I think I shoudld work on this file.
Change of Person B
Hello Git!How are you? I hope you don't run into too many conflicts.
Person B pushed her changes first to the remote server. There was no problem because she worked with the most recent status.
However, Person A is now behind by one commit. If she tries to push a new commit, Git will tell her that
she first needs to git pull
which is short for git fetch
and git merge
.
If Person A does this Git will try to solve the conflict automatically. However, in our case git won't be able to do so because the exactly same parts of a file were changed and you don't want a machine to decide what to keep and what to discard.
C:\Users\AlexanderGerber\myProject>git mergeAuto-merging HelloGit.txtCONFLICT (content): Merge conflict in HelloGit.txtAutomatic merge failed; fix conflicts and then commit the result.
<<<<<<< HEAD
and ending with >>>>>>> refs/...
.
<<<<<<< HEADHello Git,How are you? I think I shoudld work on this file. =======Hello Git!How are you? I hope you don't run into too many conflicts. >>>>>>> refs/remotes/origin/master
.rmd
but not the generated .html
file).gitignore
..gitignore
file .Rhistory *.html Packages/example/
There is a pretty important concept which we didn't cover at all named Branching.
It allows you to diverge form the linear development of your project to having multiple parallel versions at the same time.
If you want to dig deeper into Git this would be one of the key features you should learn about after you digested the basics (which should be enough for this course.)
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |