Git
Reasons to use version control
- Have a record of what, when and why you did something
- Snapshots of different development states
- Compare different versions of files
- Easily share work with others
- Integrate changes from others (merging)
- Mark finished product versions (tags)
- Try out new ideas (branches)
- Best practice and Industry standard

Glossary
- Repository
- Stores complete history, branches, tags (and other meta-information)
- Working Copy
- Your “playground”, actual source code
- Commit / Revision
- A specific, single version/snapshot
- Branch
- A distinct line of development
Distributed Version Control
- Every user has a full copy of the repository
- Repositories can be synchronized (push/fetch)
- Off-line access


Git
- Started in 2005 by Linus Torvalds
- Used for Linux Kernel development
- Highly distributed
- Cryptographic integrity
Install & Setup
- Install - https://www.git-scm.com
- Set user information
git config --global user.name 'Norbert Winkler'
git config --global user.email 'n.winkler@htlkrems.at'Cheat Sheet
https://education.github.com/git-cheat-sheet-education.pdf
Creating Repositories
- Create a new local repository
$ cd path/to/project
$ git init
# or
$ git init new_dir
$ cd new_dirAdding files
- Adding new files
- Current state of the file is recorded
# add a new file foo.txt with contents 'bar'
$ >foo.txt echo 'bar'
git add foo.txtWriting history
- Permanently save your work
- The seven rules of a great Git commit message
$ git commit -m "Showcased how to add files to git"Updating Files
- Tell Git about new changes
- Same thing as adding new files (Git only cares about the content of files)
# add new content to foo.txt
$ >>foo.txt echo 'new content'
$ git add foo.txt
$ git commit -m 'updated file'Browsing history
$ git log
commit 87bebde3c4c24c34f8b61d3332ac18df416d28c7
Author: norwin <me@norwin.at>
Date: Tue Jun 11 04:35:35 2024 +0200
Updated file
commit f49e31ed76c07887481bea4ba4dffd606b794c45
Author: norwin <me@norwin.at>
Date: Tue Jun 11 04:32:13 2024 +0200
Showcased how to add files to gitWorking copy status
- Shows list of modified, new and staged files
- Also reminds you of important commands
$ git status
On branch main
nothing to commit (working directory clean)Inspecting changes
- Show current changes
- Show differences between commits
- Show differences between branches
$ git diff
$ git diff HEAD~ HEAD
$ git diff branch1 branch2What the fork?
- Try out new features
- In an ideal world: one branch per feature
- Branches are cheap, use them often
- Branches can be deleted
# create and switch to new branch
$ git branch newbranch
$ git checkout newbranch
# list branches (star marks active branch)
$ git branch
main
* newbranchMerging changes
- Integrate changes from a branch
- Integrate changes from others
# Checkout the branch you want the changes to merge into
$ git checkout main
# merge changes from other branch
$ git merge newbranchMerge conflicts
- Merges don’t always go well
- Git inserts conflict markers into conflicting files
git statustells you the current state- Remove conflicts and add that state of the file with
git add
$ git diff
diff --cc foo.txt
…
<<<<<<< HEAD
current content
=======
branch content
>>>>>>> newbranch
# manually resolve conflict
$ vim foo.txt
# add new state to git
$ git add foo
$ git commitStash
- Temporarily put aside changes
- Handy for urgent bugfixes
# stash changes
git stash save
# Your working copy is reset to your last commit
# You can checkout another branch and come back later
# recall changes from stash
git stash popForking existing projects
- Get a copy of the repository
fetch+mergeto update your copy
$ git clone https://github.com/git/git.git git
$ cd git
# ...
$ git fetch
$ git merge origin/branchSyncing changes
- Fetch downloads the latest changes from the remote repository
- With merge you can merge these changes with your local branch
pullis a shortcut forfetch+mergepushuploads your local changes to the remote repository
$ git pull
# make changes and commit them ...
# afterwards:
$ git pushIgnoring Files
-
You don’t want all files to be versioned
- Temporary files
- Build artifacts
- Configuration, especially Passwords !!
-
Create a
.gitignorefile in your projects’s root folder
git add .gitignore
git commit -m "added .gitignore"Sample .gitignore
## Get latest from `dotnet new gitignore`
# User-specific files
*.rsuser
*.suo
*.user
# Build results
[Dd]ebug/
[Rr]elease/
[Bb]in/
[Oo]bj/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Click-Once directory
publish/
# JetBrains Rider
*.sln.iml
.idea/