1. Basic Workflow
- Initialize Repository: Create an empty Git repository.
- Check Status: View the status of files in the working directory.
git status
: Detailed status.
git status -s
: Short status with two characters:
- 1st: Staging area status (
A
: staged, M
: staged and modified, ??
: untracked, UU
: conflicted).
- 2nd: Working directory status (
M
: modified, blank: unchanged).
- Stage Files: Add files to the staging area for commit.
git add <file name>
: Stage a specific file.
git add .
: Stage all modified/new files.
- Commit Changes: Create a version of staged files in the repository.
git commit -m <message>
: Commit with a descriptive message.
- View History:
git log
: List all commits.
git log --oneline --graph --color
: Show commits in one line with a graph and color.
- Compare Changes:
git diff
: Show differences between the latest and previous versions.
- Restore Files: Revert to the last version of a file from the repository.
git checkout <file name>
: Restore a file (even if deleted locally).
- Reset Changes:
- Soft Reset: Move staged changes back to the working directory (no data loss).
- Hard Reset: Discard all changes in staging and working directory, revert to last committed version (use cautiously).
- Remove Repository: Delete all Git metadata and history.
rm -rf .git
(irreversible, use with caution).
2. Branches
- Definition: A branch is a reference to a commit in the repository’s history.
- List Branches:
git branch
: Show all branches (current branch marked with ).
git status
: First line shows the current branch.
- HEAD: Reference to the current branch.
- Create Branch:
git branch <branch name>
: Create a new branch (does not switch to it).
- Switch Branch:
git checkout <branch name>
: Switch to an existing branch.
- Create and Switch:
git checkout -b <branch name>
: Create a new branch and switch to it.
- Merge Branches:
- Example: Merge
prime-number
branch into main
.
git checkout main
: Switch to the target branch.
git merge prime-number
: Merge changes from prime-number
into main
.
- Delete Branch:
git branch -d <branch name>
: Delete a branch (cannot delete the current branch).
3. Conflict Scenarios