Git Basics
Create a Git repository and make your first commit
Description: Create a folder named my-notes, make it a Git repository, create a file notes.txt with some text, and save it in a first commit with the message “Add notes”.
Hint:
- Use
git initinside the folder. - A file must be staged with
git addbefore the commit.
Solution
$ mkdir my-notes
$ cd my-notes
$ git init
Initialized empty Git repository in /home/robin/my-notes/.git/
$ echo "My first note" > notes.txt
$ git add notes.txt
$ git commit -m "Add notes"
[main (root-commit) 6927e03] Add notes
1 file changed, 1 insertion(+)
create mode 100644 notes.txt
$ git log –oneline
6927e03 (HEAD -> main) Add notes
How it works
git initcreates the hidden.gitfolder, which makes the folder a repository.git addstages the file, andgit commit -msaves it with the message.git log --onelineshows the new commit.
Read Git Init and Git Commit.
Check the status of files in a Git repository
Description: In a repository, create two files a.txt and b.txt, stage only a.txt, and show the status in the short format.
Hint:
- Use
git status -sfor the short format.
Solution
$ mkdir status-demo
$ cd status-demo
$ git init -q
$ echo "a" > a.txt
$ echo "b" > b.txt
$ git add a.txt
$ git status -s
A a.txt
?? b.txt
How it works
Ain the first column meansa.txtis staged (added).??meansb.txtis untracked.
Read Git Status.
Stage only selected files and commit them separately
Description: You changed index.html, about.html and style.css. Make two commits: one with the two html files and one with only the css file.
Hint:
- Stage the html files first with a pattern, commit, then stage the css file.
Solution
$ git add *.html
$ git commit -m "Update html pages"
[main 3f16559] Update html pages
2 files changed, 2 insertions(+), 2 deletions(-)
$ git add style.css
$ git commit -m "Update style"
[main 3ce526e] Update style
1 file changed, 1 insertion(+), 1 deletion(-)
$ git log –oneline
3ce526e (HEAD -> main) Update style
3f16559 Update html pages
333d1be Start
How it works
- The staging area lets us choose which files go in a commit.
- Two small commits are easier to review than one big commit.
Read Git Add.
Show the commit history in one line per commit
Description: Show the history of a repository with one line per commit, and then show only the last 2 commits.
Hint:
- Use
--onelineand the option-n(or-2).
Solution
$ git log –oneline
a9b08d4 (HEAD -> main) Update file again
f95aab8 Update file
ad07eb3 Add file
$ git log –oneline -2
a9b08d4 (HEAD -> main) Update file again
f95aab8 Update file
How it works
--onelineshows the short hash and the title of each commit.-2limits the output to two commits.
Read Git Log.
See the changes of a file before staging and before commit
Description: Change a line in a tracked file. Show the change before staging, then stage the file and show the staged change.
Hint:
- Plain
git diffshows unstaged changes, and--stagedshows staged changes.
Solution
$ printf "Tea\nMilk\n" > drinks.txt
$ git diff
diff –git a/drinks.txt b/drinks.txt
index 345af57..4227ee2 100644
— a/drinks.txt
+++ b/drinks.txt
@@ -1,2 +1,2 @@
Tea
-Coffee
+Milk
$ git add drinks.txt
$ git diff
$ git diff –staged
diff –git a/drinks.txt b/drinks.txt
index 345af57..4227ee2 100644
— a/drinks.txt
+++ b/drinks.txt
@@ -1,2 +1,2 @@
Tea
-Coffee
+Milk
How it works
- Lines with
-are removed and lines with+are added. - After
git add, plaingit diffis empty, because there is no unstaged change.
Read Git Diff.
Ignore all .log files with .gitignore
Description: Create debug.log, app.js and a folder build with a file inside. Make Git ignore all .log files and the build folder.
Hint:
- Write the patterns
*.logandbuild/in a file named.gitignore.
Solution
$ mkdir ignore-demo
$ cd ignore-demo
$ git init -q
$ echo "log" > debug.log
$ echo "code" > app.js
$ mkdir build
$ echo "out" > build/out.js
$ printf "*.log\nbuild/\n" > .gitignore
$ git status -s
?? .gitignore
?? app.js
$ git check-ignore -v debug.log
.gitignore:1:*.log debug.log
How it works
- Only
.gitignoreandapp.jsare shown. The log file and the build folder are hidden from Git. git check-ignore -vtells which rule matched.
Read Git Ignore.
Delete a file with git rm
Description: Remove the file old.txt from the project and commit the change.
Hint:
git rmdeletes the file and stages the deletion.
Solution
$ git rm old.txt
rm 'old.txt'
$ git status -s
D old.txt
$ git commit -m "Remove old.txt"
[main 29d3f88] Remove old.txt
1 file changed, 1 deletion(-)
delete mode 100644 old.txt
$ ls
keep.txt
How it works
Din the status means the deletion is staged.- The file is still in the history, so you can bring it back from an older commit.
Read Git rm and mv.
Rename a file with git mv
Description: Rename readme.txt to README.md and commit the change.
Hint:
git mv old newrenames the file and stages the change.
Solution
$ git mv readme.txt README.md
$ git status -s
R readme.txt -> README.md
$ git commit -m "Rename readme to README.md"
[main 6b0ae04] Rename readme to README.md
1 file changed, 0 insertions(+), 0 deletions(-)
rename readme.txt => README.md (100%)
$ git log –oneline –follow — README.md
6b0ae04 (HEAD -> main) Rename readme to README.md
38fa585 Add readme
How it works
Rin the status means renamed.--followkeeps the history of the file after the rename.
Read Git rm and mv.