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 init inside the folder.
  • A file must be staged with git add before the commit.

Solution

Terminal output

$ 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 init creates the hidden .git folder, which makes the folder a repository.
  • git add stages the file, and git commit -m saves it with the message.
  • git log --oneline shows 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 -s for the short format.

Solution

Terminal output

$ 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

  • A in the first column means a.txt is staged (added).
  • ?? means b.txt is 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

Terminal output

$ 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 --oneline and the option -n (or -2).

Solution

Terminal output

$ 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

  • --oneline shows the short hash and the title of each commit.
  • -2 limits 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 diff shows unstaged changes, and --staged shows staged changes.

Solution

Terminal output

$ 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, plain git diff is 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 *.log and build/ in a file named .gitignore.

Solution

Terminal output

$ 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 .gitignore and app.js are shown. The log file and the build folder are hidden from Git.
  • git check-ignore -v tells 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 rm deletes the file and stages the deletion.

Solution

Terminal output

$ 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

  • D in 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 new renames the file and stages the change.

Solution

Terminal output

$ 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

  • R in the status means renamed.
  • --follow keeps the history of the file after the rename.

Read Git rm and mv.