February 10, 2025
O. Wolfson
Git operates through three main areas:
The workflow generally follows this sequence:
git add).git commit).git push).git init)To start tracking files with Git, initialize a new repository:
shmkdir my_project
cd my_project
git init
This creates a hidden .git directory that holds all version control information.
After initializing a repository, Git does not automatically track files. You must explicitly add them:
shecho "# My Project" > README.md
Check the status of the repository:
shgit status
Stage the file:
shgit add README.md
Commit the changes with a meaningful message:
shgit commit -m "Initial commit: Added README"
git status, git diff)git status shows which files are modified, staged, or untracked.git diff displays differences between the working directory and the staging area.git diff --staged compares the staging area with the last commit.git log, git show)git log displays the commit history in reverse chronological order.git log --oneline gives a concise summary of commits.git show <commit_hash> provides details of a specific commit.Example:
shgit log --oneline
Output:
text4f6b3c2 Initial commit: Added README
To view details of a commit:
shgit show 4f6b3c2
shmkdir my_git_project
cd my_git_project
git init
shecho "Hello, Git!" > hello.txt
git status
shgit add hello.txt
git commit -m "Added hello.txt with a greeting message"
shecho "Welcome to version control." >> hello.txt
git diff
shgit add hello.txt
git commit -m "Updated hello.txt with an additional message"
shgit log --oneline
This exercise helps solidify the Git workflow and best practices for managing source code effectively.