This is the demo repo for a hands-on lab for working with git from the command line.
Make sure git is installed and the version is > 2.23:
$ git --version
> git version 2.35.1If not, download and install git.
- Check your name and email address:
$ git config --global user.name
$ git config --global user.emailIf this does not return the desired values, set the config using these commands:
$ git config --global user.name '<your_name>'
$ git config --global user.email '<your_email_address>'- Set the default branch to
main:
$ git config --global init.defaultBranch main- Check your default editor
Check your default editor (i.e. by running git config --global --edit). If you like the editor, then you are good. If your stuck in vim (click ESC : q ! Enter to exit), then configure the editor of your choice - for example CSCode:
$ git config --global core.editor 'code --wait'Create a local file/folder:
mkdir UnderstandingGit
cd UnderstandingGit
mkdir folder;
for d in {1..6}; do echo "Line ${d}" >> folder/file.txt; done;Powershell:
mkdir UnderstandingGit
cd UnderstandingGit
mkdir folder
for ($d=1; $d -le 6; $d++) { "Line $d" >> folder\file.txt }
- Modify the file and verify its SHA value (
git hash-object) - Initialize the git repo (
git init) and check the changes in your folder. - Add file to index (
git add). - Commit file and check in your
.gitfolder what a commit is (git cat-fileandgit ls-tree). - Add another commit and verify how the two commits are connected.
- Create a sinple tag and an annotated tag (
git tagandgit tag -a).
Important commands for this exercise:
$ git hash-object folder/file.txt
$ git init
$ git add
$ git commit
$ git ls-tree
$ git cat-file [-p | -t]
$ cat
$ git tag [-a]
A commit is a tree of blobs and trees:
graph BT;
Tree==>Commit;
Blob==>Tree;
Blob2==>Tree;
Tree2==>Tree;
Blob3==>Tree2;
Blob4==>Tree2;
Tree3==>Tree2;
Blob5==>Tree3;
The commits are connected to their parent commits (DAG):
graph RL;
96a85==>49c01;
7e536==>96a85;
1e542==>7e536;
b7e6b==>1e542;
main-.->b7e6b;
HEAD-->main;
5a053==>7e536;
55805==>5a053;
branch-.->55805;
tag-.->55805;
- Check how git generates diffs (
git diff). - Create patches for your two commits (
git format-patch). - Undo your changes (
git reset). - Apply the patches (
git applyandgit am).
Important commands for this exercise:
$ git diff
$ git format-patch HEAD~2..HEAD
$ git reset --hard HEAD~2
$ git apply
$ git amecho "Solving merge conflicts in git is easy" > Merge.txt
git add Merge.txt
git commit -m "Base commit"
git switch -c experiment
echo "Solving merge conflicts is easy in git" > Merge.txt
git commit -am "Modify Merge.txt in experiment"
git switch main
echo "Solving merge conflicts in git is very easy" > Merge.txt
git commit -am "Modify Merge.txt in main"
git switch experimentCreate a merge conflict with git merge main and resolve it.
Important commands for this exercise:
$ git config --global merge.conflictStyle diff3
$ git merge --abort
$ git diff
$ git log --merge –p <filename>
$ git show :1:<filename> (common ancestor)
$ git show :2:<filename> (HEAD)
$ git show :3:<filename> (MERGE_HEAD)
$ git add <filename>
$ git merge --continueCreate a local history:
mkdir WorkingWithYourHistory
cd WorkingWithYourHistory
git init
for d in {1..6}; do touch "file${d}.md"; git add "file${d}.md"; git commit -m "adding file ${d}"; doneLook at you history. Create a git alias so that you don't have to type the long command all the time.
$ git log --oneline --graph --decorate --all
$ git config --global alias.lol 'log --oneline --graph --decorate --all'- Add another file (
File7.md) and ammend it to te last commit. - Reset the last two commits and commit them together as a new commit (
git reset --soft). - Reset the last commit and create three commits out of it (
git reset --mixed). - Undo the last three commit (
git reset --hard). - Cherry pick the changes from the reflog.
- Do an interactive rebase. Reword commit 2 and edit commit 3.
Important commands for this exercise:
$ git commit --amend
$ git reset [--hard | --soft | --mixed]
$ git reflog
$ git cherry-pick
$ git rebase [-i]$ git clone https://github.com/wulfland/DeepDive.git DeepDive
$ cd DeepDive- Create a branch
fix-bugto fix the bug (git switch -c) - Switch to
mainand search in the history for the bug (inde.htmlinstead ofindex.html) usinggit bisect. - Modify Readme.md and commit to
main. - Switch to
fix-bugand renameinde.htmltoindex.html. - Rebase the branch onto main.
- Create a branch
slow-down. - Modify line 9 of index.html and change the background URL to images/texture.jpg.
- Modify line 78 of index.html and change the timing for the game to speed it up or slow it down.
- Move
texture.jpgto a new folderimages. - Create seperate commits for refactoring and logic changes (
git add -p). - Squash the changes into
main
Important commands for this exercise:
$ git switch [-c]
$ git bisect start
$ git bisect good <SHA>
$ git bisect bad <SHA>
$ git bisect start <GOOD> <BAD>
$ git bisect run ls index.html
$ git add -p
$ git merge [--squash | --rebase]