Open CheatSheet Back To List Next Tutorial

Beginning Git Exercise (using Git commands)

Intro

In the Beginning Git tutorial, we created a simulated project called git-demo, initiated a Git repository, made commits, learned Git commands, and worked with branches. 
We also learned how to use Git both from the command line, and by using VS Code's Source Control Graphical User Interface.

Following the Beginning Git, Intermediate Git, and the GitHub tutorials there will be two practice projects that repeat the steps from the tutorial, without the explanation.
  • One version will use Git commands only. 
  • The next version will use the VS Code Source Control GUI.

You may find it useful to follow the steps below, rebuilding the demo app one or more times until you become comfortable with the commands and actions. 

This practice page uses Git commands only. The next practice page uses the Source Control GUI. 



Getting Started with Git

See if git is installed:
  • which git - Get git executable file location.
  • git --version - Get give version.
  • Compare version with latest Git version available:  git-scm.com

Help/Docs:



Setup git

Configure Git:
  • git config --global user.name "your-name" - Set your name globally.
  • git config --global user.email "your-email" - Set your email name globally.
  • git config --global init.defaultBranch main - Set initial branch name to main.
  • git config --global core.editor "code --wait" - Set VS Code as your default text editor.
  • git config --global alias.olog 'log --oneline' - Set aliases to shorten long commands that you use frequently. git olog will log your commits on one line each. Execute with git olog
  • git config --global alias.glog 'log --graph --oneline' - Set alias for graph log. Execute with git glog
  • git config --global --unset variableName - Format to global variable or alias.
  • git config --global --unset alias.olog - Remove the olog.
  • git config --global alias.olog 'log --oneline' - Reset the olog alias.
  • git config --global --list List all global git variables.

Create project folder:
  • mkdir git-demo-v2 
  • cd git-demo-v2 

Create files:
app.txt
This is a demo app to learn Git.

.env
Sensitive Information Goes here

ignoreme.txt
Git ignores this whole file.



Set up a Local Git Repository

Initialize git repository:
  • git init

.gitignore: Create a .gitignore file:
.gitignore
.DS_Store
.env
ignoreme.txt




Commits

Make initial commit:
Add files to staging area:
Add all files:
  • git add .
Or add specific files: 
  • git add app.txt .gitignore  

Commit staged files:
  • git commit -m 'First commit' 

  • Log the commit: git log


Make changes and make second commit:
Modify app.txt and add file2.txt file:
app.txt
This is a demo app to learn Git.
Second line.

file2.txt
Some text.

Stage Changes:
  • git add .

Commit staged files:
  • git commit -m 'Add file2' 

  • Log the commit: git log


Stage and commit file change in one step:
Modify app.txt file:
app.txt
This is a demo app to learn Git.
Second line modified.

Stage and commit modified file with one command:
git commit -am 'Modify app.txt' 

This only works on files already tracked by Git (i.e., previously committed).

Log the three commits: git log



Status, Diff, Log, and Show commands


Clear screen:
  • clear or on Mac Command+K 

View status and changes since last commit:
  • git status 

View file changes since last commit for a specific file:
  • git diff filename.ext 

Print list of commits for the current branch:
  • git log - all commits.
  • git log -2 - last 2 commits.
  • git log --oneline - one line per commit.
  • git olog - alias of one line per commit.

Show details of a commit
  • git show commitID - Add commit ID.
  • git show head - latest commit.
  • git show head^ - One commit prior to latest commit.
  • git show head^^ - Two commits prior to latest commit.
  • git show head~2 - Two commits prior to latest commit.
  • git show head~3 - Three commits prior to latest commit.

Fourth commit
Add two files and modify app.txt:
file3.txt
Some text.

file4.txt
Some text.

app.txt
This is a demo app to learn Git.
Second line modified.
Third line


clear | Cmd+K
git add file3.txt
git status
git diff app.txt
git add app.txt
git status
git add file4.txt
git status
git commit -m 'Add file3 and file4'
git log
git log --oneline
git olog


Branches

  • git branch - List all branches. Current branch starts with an asterisk *


Step1: Create a new branch called feature1.

Checkout new branch named feature1
  • git checkout -b feature1 


Step 2: Make file changes.

Add new file named feature1.txt and modify app.txt
feature1.txt
Some text.

  • Then modify app.txt by adding "Feature 1" text to it.
app.txt
This is a demo app to learn Git.
Second line modified.
Third line.
Feature 1.


Step 3: Stage and commit changes.

Stage and commit changes to feature1 branch. Confirm status:
  • git add . 
  • git commit -m 'Add feature 1' 
  • git status 


Step 4: Switch back to the main branch.

Checkout main branch, check status
  • git checkout main 
  • git status 


Step 5: Merge feature1 branch into main.
  • git merge feature1 


Step 6: Delete feature1 branch.
Delete feature1 branch, confirm there is only one branch, then view last 3 commits:
  • git branch -d feature1
  • git branch 
  • git log -3


Add and merge another branch

1. Checkout a new branch: git checkout -b feature1 
2. Create/modify files:
feature2.txt
Some text.

app.txt
This is a demo app to learn Git.
Second line modified.
Third line.
Feature 1.
Feature 2.

3. Stage and commit files:
  • git add .
  • git commit -m 'Add feature 2' 

4. Checkout main: git checkout main 
5. Merge branch in: git merge feature2 
6. Delete feature2 branch: git branch -d feature2 

  • Confirm there is only the main branch: git branch

Open CheatSheet Back To List Next Tutorial