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:
mkdir 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:
echo "# My Project" > README.md
Check the status of the repository:
git status
Stage the file:
git add README.md
Commit the changes with a meaningful message:
git 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:
git log --oneline
Output:
4f6b3c2 Initial commit: Added README
To view details of a commit:
git show 4f6b3c2
mkdir my_git_project
cd my_git_project
git init
echo "Hello, Git!" > hello.txt
git status
git add hello.txt
git commit -m "Added hello.txt with a greeting message"
echo "Welcome to version control." >> hello.txt
git diff
git add hello.txt
git commit -m "Updated hello.txt with an additional message"
git log --oneline
This exercise helps solidify the Git workflow and best practices for managing source code effectively.