git WorkshopLet’s have a break from feeling like we understand things
After the break we’ll check out the basics of
git, and feel stupid for not understanding them
git makes everyone feel
like thatgitgitgit defines itself as “The stupid
content tracker”
We’ll learn that it actually is “The content tracker that makes you feel stupid”
You are an amazing individual and you’re anything but stupid
git makes anyone feel
stupid
Still, when you collaborate with a lot of people
in projects with tons of files, where a syntax error can cause the whole
thing to crash, git is your friend
You’ve seen how to use git online
without having to understand it
Now let’s try (and fail) to understand it
Linux
Linux,
git, and a scuba diving softwareLinux has changed the world it runs on
everythinggit has changed the world too and is
the dominant version control system (VCS) todaygit is maintained by Junio C. Hamano
from July 2005 to this day.gitPlease write this down until you memorize it:
GitHub is not git
git is not owned by GitHub
gitYou do not need GitHub to use
git
GitHub’s app is not the only way to use
git
You only need
git <command> <arguments> to use
git
git, Then?git is a version control system (VCS)
TODO.md fileTODO:
- [X] Go to QHELP Arrifana 2023
- [X] Pay attention to the seminars
- [ ] Learn a lot
- [ ] Profit!
TODO_v2.mdTODO:
- [X] Go to QHELP Arrifana 2023
- [X] Pay attention to the seminars
+ [X] Ignore the `git` seminar that guy is just stupid
- [ ] Learn a lot (just not from the `git` seminar)
- [ ] Profit!
Since you saved your changes in a different name (e.g., “save as”)
Instead of overwriting the original file (e.g., just “save”)
You now have two versions of the same file
VCSs allow you to do that for entire projects (many files in many directories)
There are several VCSs out there (e.g.,
cvs, fossil, etc…)
git is by far the most popular, and
the focus for today
git Demogit takes snapshots a directory’s
(folder) contents so let’s make a directorygit Demogit Demogit Demogit
git config --global so you never
have to configure these settings againuser.name and the user.email with your
preferred user name and email address.git Demogit
repository (repo)git Demogit Demogit DemoWe have:
Created a directory (folder)
git initiated a repo there
We have:
Created an empty file
Added the demo_file to the
staging area
git
repo is
gitIn our demo there were no files in the repo
There is one now—demo_file
The change we’ve made is “adding the
demo_file to the repo”
We will have to get back to what a staging area is after we continue the demo
Just remember there is such a thing as a staging are
It contains the changes you’ve
git added to the repo
git Demogit Demogit DemoWe have:
created a git repo
git added a file to the staging
area
git committed the contents of the
staging are
We have seen that git status is telling us the state of
the repo
What files in the repo are untracked
What files in the repo have changed
What are the contents of the staging area
We have seen that git commit --message:
git log
Our git repo now has one
commit
No file has changed since the last commit
demo_file hasn’t been changedgit DemoI’m using command line tools to create, view, and edit the files because I want everything to be visible on the slides and automatically generated by SciOps
That last edit to the file could have been made
in RStudio, notepad,
Microsoft Word, what have you.
git Demogit DemoWhat are the contents of our staging area:
demo_fileOption (c) is correct the staging area is empty
We have changed the demo_file but
we have not git added it
git Demo# let's check our `git status` once again
git status
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: demo_file
no changes added to commit (use "git add" and/or "git commit -a")git DemoOur git repo has one commit (adding
the demo_file)
We’ve changed the demo_file and
git added it to the staging area
We have NOT git committed
it
git Demogit Demodemo_filegit Demo# let's check our git status now
git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: demo_file
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: demo_fileWe have:
One commit that creates the
demo_file
One change to the demo_file
git added to the staging area
Deleted the demo_file but we have
NOT git added that change to the staging area
What happens after we git commit?
demo_file in our
directory?
demo_file stays
deleted.demo_file with the last
change that we made.demo_file in its
original empty version?git committed?
git won’t make the commit
because the file was deletedgit committed its contents.git won’t make the commit nor
change the staging area because the file was deleteddemo_file in our
directory?
demo_file stays
deleted.git committed?
# we can use `git show` to see the contents of the commits
# If we don't give it any arguments it will show the last commit
git show
commit e600b861a7048b9c46e562920adc871a2f1ed332
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
What has happened?
diff --git a/demo_file b/demo_file
index e69de29..14f9b20 100644
--- a/demo_file
+++ b/demo_file
@@ -0,0 +1 @@
+This workshop sucksgit committed its contents.We’ve created a git repo
We’ve learned about the staging area
git repogit addgit commit
git log
We’ve learned to see our
git status
We’ve learned to see our last
git commit with git show
git Demogit add and
git commit the deletion of the demo_file by
mistakegit Demogit Demogit Demo# The `git log` now features our Upsy daisy
git log
commit cef2e205ef3d1d26bc2f97663ec6d57239adb118
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Upsy daisy
commit e600b861a7048b9c46e562920adc871a2f1ed332
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
What has happened?
commit ee9bc8107a612a8d84510cb15be0bfd2154420a0
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Make initial commitgit Demo# And we can see we messed up with
git show
commit cef2e205ef3d1d26bc2f97663ec6d57239adb118
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Upsy daisy
diff --git a/demo_file b/demo_file
deleted file mode 100644
index 14f9b20..0000000
--- a/demo_file
+++ /dev/null
@@ -1 +0,0 @@
-This workshop sucksgit DemoTo fix our mistakes we can
git revert <commit_hash> our last commit
We can also git checkout the file
that we want
git checkout <commit_hash> <file>
But we need to know the commit hashes
git Demo# the commit hash is that sequence of numbers and letters after "commit "
git log
commit cef2e205ef3d1d26bc2f97663ec6d57239adb118
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Upsy daisy
commit e600b861a7048b9c46e562920adc871a2f1ed332
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
What has happened?
commit ee9bc8107a612a8d84510cb15be0bfd2154420a0
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Make initial commitgit DemoIf we want to use git revert we
need to use the hash from the last commit, since that commit deleted the
file.
If we want to use git checkout we
need to use the hash from the second to last commit since that was the
last commit that still had the file.
git Demogit checkout the
demo_file from the second to last commitgit log
commit cef2e205ef3d1d26bc2f97663ec6d57239adb118
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Upsy daisy
commit e600b861a7048b9c46e562920adc871a2f1ed332
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
What has happened?
commit ee9bc8107a612a8d84510cb15be0bfd2154420a0
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Make initial commitgit Demo# The slides are automatically generated so I'll have to use some
# hackery to find the hash, since I can't see the log.
# Please ignore the line below.
commit_hash=$(git log | grep -B4 "What has happened" | grep "commit" | sed 's/commit //')
# Just replace the commit_hash variable with the actual hash
git checkout $commit_hash demo_fileUpdated 1 path from be292fa
git Demogit Demogit Demo# And we can see this new commit is in the log
git log
commit 8612228066af5fee70305d4d0c0475e546ae2a14
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Recover demo file
commit cef2e205ef3d1d26bc2f97663ec6d57239adb118
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Upsy daisy
commit e600b861a7048b9c46e562920adc871a2f1ed332
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
What has happened?
commit ee9bc8107a612a8d84510cb15be0bfd2154420a0
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Make initial commitgit Demo# And inspect what it did with `git show`
git show
commit 8612228066af5fee70305d4d0c0475e546ae2a14
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Recover demo file
diff --git a/demo_file b/demo_file
new file mode 100644
index 0000000..14f9b20
--- /dev/null
+++ b/demo_file
@@ -0,0 +1 @@
+This workshop sucksSo far you have worked on a single
branch—main
Branches are a very important feature of
git
However, despite code forges like GitHub and GitLab making heavy use of them they are not easy to understand before you understand the basics
If you have never tried git before,
I would try and work without branches for a while…
It is hard to explain what branches are…
Let me try…
Let’s say you want to make changes but you want to easily preserve the original versions.
For instance, you were working on a report with several graphs, all of the same type.
You could be interested in creating a version with another type of graph, while preserving the original.
Maybe you want to postpone making a decision on which type of graph you will choose, and you want to keep working on both to see what they look like.
In such cases you might create two separate versions of your report, with different file names, and work on both at the same time.
However, your report could be calling different scripts, and each script might require modifications depending on the type of graph.
Now you’d be forced to create two versions of each of those scripts as well.
git you can have
separate branches, each can hold a different version, from the other
branches, of any number of files.demo_file) can look like one thing when you’re in a branch,
and look like something completely different, when you’re in another
branch.testing.git statusOkay… So we’ve created a branch but we’re not there yet.
Let’s git checkout that
branch
Now we’re in the testing
branch
Let’s say you decide to make a new change to
demo_file, you git add demo_file
git add and
git commit your changes to the testing
branchtesting branch’s
git log will be the same as main’s
git log up until the point you made this
commit.git log
commit bbd692cf8bdb5afed482e93f4aee189d84080ed2
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Change only this branch
commit 8612228066af5fee70305d4d0c0475e546ae2a14
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Recover demo file
commit cef2e205ef3d1d26bc2f97663ec6d57239adb118
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Upsy daisy
commit e600b861a7048b9c46e562920adc871a2f1ed332
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
What has happened?
commit ee9bc8107a612a8d84510cb15be0bfd2154420a0
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Make initial commitmain and testing
have a similar commit history, but with the last git commit
to testing they have started to differ.Now imagine you start making new commits on either one.
Their git logs will start to differ
more and more from, but they will always have the same starting
point
Branches allow you to branch of a point in branch’s history, creating another, and starting a new commit history from there
Your branches will always have some common history, but after that point they will have a different history
git merge branches, merging their histories, as if they
have never branched ofdemo_file look like in
mainmain’s git log
doesn’t feature the last git commit from
testing, with the message “Only in this branch”git log
commit 8612228066af5fee70305d4d0c0475e546ae2a14
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Recover demo file
commit cef2e205ef3d1d26bc2f97663ec6d57239adb118
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Upsy daisy
commit e600b861a7048b9c46e562920adc871a2f1ed332
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
What has happened?
commit ee9bc8107a612a8d84510cb15be0bfd2154420a0
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Make initial committestingmainLet’s say we don’t want testing to
be so arrogant and think it’s special.
Let’s say we want to
git merge testing onto main, where we are, so
the demo_file can look, here, in main, like it
looks in testing
main’s
git log nowgit log
commit bbd692cf8bdb5afed482e93f4aee189d84080ed2
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Change only this branch
commit 8612228066af5fee70305d4d0c0475e546ae2a14
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Recover demo file
commit cef2e205ef3d1d26bc2f97663ec6d57239adb118
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Upsy daisy
commit e600b861a7048b9c46e562920adc871a2f1ed332
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
What has happened?
commit ee9bc8107a612a8d84510cb15be0bfd2154420a0
Author: Stupid_Lecturer <i_hate_this_lecture@boring.com>
Date: Tue Feb 6 17:12:24 2024 +0000
Make initial commitSo we’ve taken a look at the amazing power of
git branching
If this all sounds confusing its because it is…
Avoid working with branches until you have to
Unfortunately, when several people are working on the same project at the same time, you probably have to…
As it usually happens in QHELP…
So far we’ve been using git
locally
Unlike many people today think, that’s a perfectly valid use case
Still, when we get to host git
repos online a new level of collaboration is possible
Online git repos are basically a
copy of your repository in the “cloud”
You can git push your changes to
the remote repo by running git push
You can git pull any changes from
the remote with git pull
If you’re the only one working on the repo and
your work only on one machine, there’s little need to
git pull
There will be nothing there other than what you
have git pushed
If you’re working with others, though, they
might have git pushed their changes before you made
yours
When working with others, it’s always a good
idea to run git pull before you run
git push
git pulled, your git log will differ from
remotegit pull --rebase
git will try to restructure your
commits as if they happened after the ones made by your colleaguesgit pulling and
git pushinggit pull --rebase allows
git to be creative in how it rewrites the history so
everything worksgit add a file we add it to
the staging areagit commit it that
we add it to the git logWhat happens in the following situation
I change a file
I git add that file
I change the file again (before
git commiting it)
I git add the file again (before
git commiting it)
I override the changes that were in the staging area with the latest one
I don’t have a way to distinguish between the changes made in 1 and the ones made in 3.
It is the same as making all the changes at once
and git adding them later
Only if I add git commited them
would I keep both versions
Sometimes that’s what I want, sometimes that’s not
gitI suggest you git pull,
git add, git commit, and git push
very frequently
The right time to git add and
git commit was 30 minutes ago