Skip to content
Laviyx Learn

Try

Git Basics Practice

Practise everyday Git: commits, the staging area, history, diffs, .gitignore, undoing changes, branches, merging and conflicts. Every solution has been run with Git and shows its real output.

Beginner12 problemsUpdated

Review first: Your First Repository and Commit, The Staging Area, Viewing History with git log, Seeing Changes with git diff, Ignoring Files with .gitignore, Undoing Changes, Branches, Merging Branches, Resolving Merge Conflicts, Saving Work with git stash

  1. 1.Make two commits

    Beginner

    Create todo.txt containing Learn Git, and commit it with the message Add todo. Add a second line, Practise daily, commit that with the message Add habit, and print the history in one-line format, showing only the messages.

    Starter code (Terminal)
    git init -q demo
    cd demo
    Show hint

    --format=%s prints only the subject of each commit.

    Show solution
    Terminal
    echo "Learn Git" > todo.txt
    git add todo.txt
    git commit -q -m "Add todo"
    echo "Practise daily" >> todo.txt
    git commit -q -am "Add habit"
    git log --format=%s
    Output
    Add habit
    Add todo
  2. 2.Commit only one file

    Beginner

    Both a.txt and b.txt have been changed. Commit only a.txt with the message Update a, then print the short status.

    Starter code (Terminal)
    git init -q demo
    cd demo
    echo "1" > a.txt
    echo "1" > b.txt
    git add .
    git commit -q -m "Start"
    echo "2" >> a.txt
    echo "2" >> b.txt
    Show hint

    git add a.txt stages one file. The other stays modified.

    Show solution
    Terminal
    git add a.txt
    git commit -q -m "Update a"
    git status --short
    Output
     M b.txt
  3. 3.Unstage a file

    Beginner

    You ran git add . by mistake, and secret.txt is now staged. Unstage it without deleting it, ignore it with .gitignore, and print the short status.

    Starter code (Terminal)
    git init -q demo
    cd demo
    echo "code" > app.txt
    git add .
    git commit -q -m "Start"
    echo "password" > secret.txt
    git add .
    Show hint

    git restore --staged removes a file from the staging area.

    Show solution
    Terminal
    git restore --staged secret.txt
    echo "secret.txt" > .gitignore
    git status --short
    Output
    ?? .gitignore
  4. 4.Count commits and read an old file

    Beginner

    Print the number of commits on the current branch, then print a.txt as it was in the first commit.

    Starter code (Terminal)
    git init -q demo
    cd demo
    echo "one" > a.txt
    git add .
    git commit -q -m "First"
    echo "two" >> a.txt
    git commit -q -am "Second"
    echo "three" >> a.txt
    git commit -q -am "Third"
    Show hint

    git rev-list --count HEAD counts commits. git show <commit>:<file> prints an old version. HEAD~2 is two commits back.

    Show solution
    Terminal
    git rev-list --count HEAD
    git show HEAD~2:a.txt
    Output
    3
    one
  5. 5.Check what will be committed

    Beginner

    Add the line four to a.txt and stage it. Print the staged changes as a summary, then print only the names of the staged files.

    Starter code (Terminal)
    git init -q demo
    cd demo
    echo "one" > a.txt
    git add .
    git commit -q -m "First"
    echo "two" >> a.txt
    git commit -q -am "Second"
    echo "three" >> a.txt
    git commit -q -am "Third"
    Show hint

    git diff --staged with --stat or --name-only.

    Show solution
    Terminal
    echo "four" >> a.txt
    git add a.txt
    git diff --staged --stat
    git diff --staged --name-only
    Output
     a.txt | 1 +
     1 file changed, 1 insertion(+)
    a.txt
  6. 6.Throw away an edit

    Beginner

    Someone has broken a.txt. Put it back to its last committed version and print it.

    Starter code (Terminal)
    git init -q demo
    cd demo
    echo "one" > a.txt
    git add .
    git commit -q -m "First"
    echo "two" >> a.txt
    git commit -q -am "Second"
    echo "three" >> a.txt
    git commit -q -am "Third"
    echo "broken" > a.txt
    Show hint

    git restore <file>.

    Show solution
    Terminal
    git restore a.txt
    cat a.txt
    Output
    one
    two
    three
  7. 7.Fix the last commit message

    Beginner

    The last commit has a typo. Change its message to Third line and print the last two messages.

    Starter code (Terminal)
    git init -q demo
    cd demo
    echo "one" > a.txt
    git add .
    git commit -q -m "First"
    echo "two" >> a.txt
    git commit -q -am "Second"
    echo "three" >> a.txt
    git commit -q -am "Third"
    Show hint

    git commit --amend -m.

    Show solution
    Terminal
    git commit -q --amend -m "Third line"
    git log --format=%s -n 2
    Output
    Third line
    Second
  8. 8.Undo a commit safely

    Intermediate

    The latest commit, Third, was a mistake, but this branch is shared, so history must not be rewritten. Undo it with a new commit, then print a.txt and the last two messages.

    Starter code (Terminal)
    git init -q demo
    cd demo
    echo "one" > a.txt
    git add .
    git commit -q -m "First"
    echo "two" >> a.txt
    git commit -q -am "Second"
    echo "three" >> a.txt
    git commit -q -am "Third"
    Show hint

    git revert --no-edit HEAD creates a new commit.

    Show solution
    Terminal
    git revert --no-edit HEAD > /dev/null
    cat a.txt
    git log --format=%s -n 2
    Output
    one
    two
    Revert "Third"
    Third
  9. 9.Branch and merge

    Beginner

    Create a branch docs, add and commit README.md, go back to main, merge docs, delete the branch and print the branches.

    Starter code (Terminal)
    git init -q demo
    cd demo
    echo "start" > a.txt
    git add .
    git commit -q -m "Start"
    Show hint

    git switch -c docs. This merge is a fast-forward.

    Show solution
    Terminal
    git switch -q -c docs
    echo "# Demo" > README.md
    git add README.md
    git commit -q -m "Add README"
    git switch -q main
    git merge -q docs
    git branch -q -d docs
    git branch
    ls
    Output
    * main
    README.md
    a.txt
  10. 10.Merge branches that have moved apart

    Intermediate

    main and feature each have a new commit. Merge feature into main, then print the graph without commit IDs.

    Starter code (Terminal)
    git init -q demo
    cd demo
    echo "start" > a.txt
    git add .
    git commit -q -m "Start"
    git switch -q -c feature
    echo "work" > f.txt
    git add .
    git commit -q -m "Add feature"
    git switch -q main
    echo "more" > m.txt
    git add .
    git commit -q -m "Add more"
    Show hint

    --format=%s with --graph hides the IDs.

    Show solution
    Terminal
    git merge -q --no-edit feature
    git log --graph --format=%s
    Output
    *   Merge branch 'feature'
    |\  
    | * Add feature
    * | Add more
    |/  
    * Start
  11. 11.Resolve a conflict

    Intermediate

    main and green both changed theme.txt. Merge green, keep Colour: green, finish the merge, and print the file and the last commit message.

    Starter code (Terminal)
    git init -q demo
    cd demo
    echo "Colour: blue" > theme.txt
    git add .
    git commit -q -m "Add theme"
    git switch -q -c green
    echo "Colour: green" > theme.txt
    git commit -q -am "Use green"
    git switch -q main
    echo "Colour: red" > theme.txt
    git commit -q -am "Use red"
    Show hint

    Overwrite the file with the version you want, then git add and git commit --no-edit.

    Show solution
    Terminal
    git merge green > /dev/null
    echo "Colour: green" > theme.txt
    git add theme.txt
    git commit -q --no-edit
    cat theme.txt
    git log --format=%s -n 1
    Output
    Colour: green
    Merge branch 'green'
  12. 12.Stash and come back

    Beginner

    You have an unfinished edit in a.txt. Stash it, confirm that the status is clean by printing git status --short and the word clean, then bring the edit back and print the short status again.

    Starter code (Terminal)
    git init -q demo
    cd demo
    echo "one" > a.txt
    git add .
    git commit -q -m "First"
    echo "two" >> a.txt
    git commit -q -am "Second"
    echo "three" >> a.txt
    git commit -q -am "Third"
    echo "draft" >> a.txt
    Show hint

    git stash and git stash pop. Use -q to keep the output short.

    Show solution
    Terminal
    git stash -q
    git status --short
    echo clean
    git stash pop -q
    git status --short
    Output
    clean
     M a.txt