I quickly commented out a bit of code and forgot it 🙈
I have a lot of local versions of files, no idea which change is where 🙉
My stuff doesn't do the same as last week 🙊
Someone else changed my stuff and I have no idea where 😤
What is a repository?
âž¡ Some source code and its entire history.
âž¡ The history is stored in increments (commits)
repository as tree / graph structure
git init / config / clone / .gitignore
git init
# initialize new repository
git init
git config
# set author information
git config --global user.name "Firstname Lastname"
git config --global user.email "user@uio.no"
.gitignore
# ignore python caching files
*.pyc
__pycache__
# ignore build system clutter
build-*
# always consider files source code
!*.cpp
!*.hh
!*.py
Templates for many languages can be found here: github.com/github/gitignore
git clone
# create local copy of remote repository
git clone https://git.iws.uni-stuttgart.de/dumux-repositories/dumux.git
there is two options: https/ssh (https is easier to set up)
git clone git@git.iws.uni-stuttgart.de:dumux-repositories/dumux.git
git clone https://git.iws.uni-stuttgart.de/dumux-repositories/dumux.git
git status
git diff
git log
git status
git status
General information
On branch feature/init-twop-immiscible
Your branch is up-to-date with 'origin/feature/init-twop-immiscible'.
Staging area
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: README.md
Local changes
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: dune.module
Not tracked by git (yet) and not ignored (.gitignore
)
Untracked files:
(use "git add <file>..." to include in what will be committed)
tutorial/gridmanager.py
git diff
# show local (unstaged) changes
git diff
git diff <file/folder>
# show changes between two commits
git diff <commit> <commit>
...
diff --git a/run.py b/run.py
index d4801a8..4afa3d5 100644
--- a/run.py
+++ b/run.py
@@ -1,5 +1,2 @@
# set the maxiumum number iterations (default 20)
-if args['max_num_iterations']:
- max_num_iterations = args['max_num_iterations']
-else:
- max_num_iterations = 20
+max_num_iterations = args['max_num_iterations'] or 20
git log
# inspect history
git log
git log -5
git log --oneline
fa2c249 (HEAD -> master) Second commit
dcf9426 Initial commit
git add / commit / stash
git add
# stage changes for commit
git add <file/folder>
git add -p
git commit
# create a new commit (incremental change)
git commit
git commit <file/folder>
git commit -m "[readme] update installation instructions"
git stash
# stash away local (unstaged) changes
git stash
git stash list
git stash pop
git stash pop stash@{3}
git stash drop
git stash drop stash@{1}
git checkout / reset / revert
git checkout
# throw away local (unstaged) changes
git checkout <file/folder>
git checkout -p
git reset
# remove commits / throw away changes
git reset HEAD <file/folder>
git reset --soft HEAD~3
git reset --hard HEAD~1
git revert
# create a new commit reverting <commit>
git revert <commit>
git branch / checkout / merge / cherry-pick
git branch
git branch
git branch -r
git branch -a
git branch feature/my-awesome-new-solver
git checkout
git checkout <file>
git checkout <commit>
git checkout <branch>
git checkout -b feature/my-awesome-new-solver
git merge
git checkout master
git merge fix/assembly-parallel
git merge
Usually, you will merge in a web frontend via pull requests (GitHub) / merge requests (GitLab).git cherry-pick
git cherry-pick <commit>
git cherry-pick -x <commit>
Conflicts: demonstration
git remote / fetch / pull / push
git remote
git remote add origin https://git.iws.uni-stuttgart.de/dumux-repositories/dumux.git
git remote add somewhere <url>
git remote show
git remote show somewhere
git remote remove somewhere
git fetch
# download information from remote (without changes to local repo)
git fetch origin
git pull
git pull # merge (fast-forward if possible)
git pull --rebase # rebase
git push
git push
git push -u origin master
git push origin fix/assembly-parallel
Example https://gitlab.com
git commit --amend
git rebase
git push --force
git commit --amend
âž¡ changes the commit sha
# edit the most recent commit
git commit --amend
git commit --amend --author="Firstname Lastname <user@uio.no>"
git rebase
âž¡ changes the commit shas
git rebase
git rebase -i master
git rebase -i HEAD~2
git rebase | recommendations
git push --force
âž¡ be careful!
# replace origin/feature/new-solver by my version
git push --force origin feature/new-solver
Fixing repository after things went terribly wrong
git reflog
Finding the offending commit in a large history
git bisect good
git bisect bad
git bisect start
git-LFS (large file storage)
git lfs install
git lfs track largefile.png
git is very well documented
git --help
Good git tutorials by Atlassian (bitbucket) https://www.atlassian.com/git/tutorials
Most questions are already answered on StackOverflow https://stackoverflow.com/