Gerhard Bräunlich
January 2022
Distributed file versioning
🐧 | apt-get install git / yum install git |
🪟 | gitforwindows.org |
🍏 | git-scm.com/download/mac |
git config --global user.name "Chuck Norris"
git config --global user.email "chucknorris@roundhouse.gov"
Also can be done per repository (use --local
instead of
--global
)
Clone an already published project (repository)
Initialize a new repository:
Idea: Organize changes to your project in “commits” (a new state in the history of changes)
A commit is a “logical” unit of change.
👎 Bad examples:
👍 Good example:
👎 Bad example:
Source: xkcd.com/1296
To unstage files (but keep local changes) again, use
Thumbs.db
,
.DS_Store
) or directories (e.g. __MACOSX
).vscode/
)Use .gitignore
to declare this sort of files:
*.o
*.obj
Also version .gitignore
!
.gitignore
For example private folders filled with your own 🗑
Use the “private” version of .gitignore
, which wont be
published:
.git/info/exclude
When:
… Then: git branches
Create a new branch out of the current git state:
Once you continued work and want to go back to the state of a branch:
Go back to previous branch:
Delete branch:
Rename branch:
Log in to https://gitlab.ethz.ch
Create a new project
Follow the instructions there under “Push an existing Git repository”
To also publish different branches:
Create a new local git repository.
Make an initial commit with commit message
"1st commit"
, containing one single file:
README.md
. Content:
Create a 2nd branch named “secret”.
Change the file README.md
, so it now contains:
Commit the changes (commit message:
"Add description to README.md"
)
Publish both branches (main, secret) to a private project on gitlab.ethz.ch (user space).
Dont forget to uncheck the box “create README.md”!
Give me access to the repos, you created
(@brgerhar
).
Idea: Only work on “feature” branches only, let a lead developer merge the changes into a common branch.
See also this discussion.
Create a new issue for your task (fix a bug, add a feature) if not already created by a project leader
From within the issue, create a new “merge request”. This will create a new feature branch on the remote repository
Fetch the automatically created branch
git push
https://gitlab.ethz.ch/sis/courses/git-workshop/demo-project
Clean history: Update source branch for changes in target branch
Best update by means of rebase / git commit --amend
git switch target-branch
git pull
git switch source-branch
git rebase target-branch # might need to resolve conflicts!
git push --force-with-lease # with lease in case others pushed meanwhile
others (e.g. reviewer) can update:
git switch source-branch
git pull --rebase
Visit
gitlab.ethz.ch/sis/courses/git-workshop/exercises/merge_requests
and follow the instructions there.
A commit in a feature branch changes the same line as a change commited to the main branch in the meantime.
Example
When trying to merge / rebase, git will detect the conflict and mark the affected lines in the affected files.
What to do when you messed up commit?
Add changes to the most recent commit (rewrite commit):
git add file1 file2 ...
git commit --amend --no-edit # Without `--no-edit`, you also
# can alter the commit message.
git push --force-with-lease # force push
# only use -f instead of --force-with-lease if you really
# know what you're doing
Only do this to your “self owned” unmerged feature branches, never on the common branch.
When you rewrite a commit, a force push is crucial. A normal push will be rejected and you will be instructed to do a pull first (see message below). This is not what we want to accomplish in this situation. Ignore it and do the force push.
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to '...'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Edit or drop older commits / insert commits:
git rebase -i HEAD~4 # Change 4 to the number of commits between
# the current commit and the commit you want to change
# (including the current commit and the commit you
# want to change)
or
This also allows for squashing two commits into one and deleting commits.
Hard reset to a commit or branch (checkout and delete all following commits):
Rebasing on an other branch:
Merging branches locally:
Slides:
Mik Rybinski for a careful review