1. Basic Git Workflow
- Initialize a Repository:
git init
: Creates an empty Git repository in the current directory.
- Check Status:
git status
: Displays the status of all files in the working directory (tracked, untracked, modified, etc.).
git status -s
: Provides a short status with two-character codes:
- 1st character: Status relative to the staging area:
A
: File is staged and ready to be committed.
?
: Untracked file (not recognized by the repository).
- 2nd character: Status relative to the working directory:
M
: File is modified in the working directory.
- (Space): No changes in the working directory.
- Stage Files:
git add <file name>
: Adds a specific file to the staging area.
git add .
: Adds all modified and untracked files in the current directory to the staging area.
- Commit Changes:
git commit -m <message>
: Creates a snapshot (commit) of all files in the staging area with a descriptive message.
- View Commit History:
git log
: Lists all commits with details (commit hash, author, date, message).
git log --oneline --graph --color
: Displays a concise, colorful commit history with a graphical branch view.
- Compare Changes:
git diff
: Shows differences between the latest commit and the current working directory or staging area.
- Restore Files:
git checkout <file name>
: Reverts a file to its last committed version from the repository (useful if a file is deleted or modified).
- Soft Reset:
- Moves changes from the staging area back to the working directory without losing any changes.
2. Git Reset
- Hard Reset:
git reset --hard
: Discards all changes in the staging area and working directory, reverting to the last committed version.
- Warning: Use with caution, as it permanently removes uncommitted changes.
- Remove Repository Metadata:
rm -rf .git
: Deletes the entire .git
directory, erasing all repository history and metadata.
- Warning: This action is irreversible and removes all version control data.
3. Shared (Remote) Repository
- Types of Repositories:
- Local: Stored on the local machine.
- Shared/Remote: Hosted on a server (e.g., GitHub, GitLab, BitBucket).
- Connecting to a Remote Repository:
- Create a project/repository on a remote server.
- Link the local repository to the remote one.
- Remote Repository Commands:
git remote -v
: Lists the URLs of connected remote repositories.
git remote add <server alias> <repository url>
: Links a local repository to a remote one (e.g., git remote add origin <url>
).
git push <alias> <branch name>
: Uploads local commits to the specified remote branch.
git pull <alias> <branch name>
: Downloads and integrates changes from the remote branch to the local repository.
git remote remove <alias>
: Disconnects a remote repository from the local one.
4. Key Takeaways
- Git is a powerful version control system for tracking changes and collaborating on code.
- Core workflow: Initialize → Add → Commit → Push/Pull for remote collaboration.
- Use
git status
and git log
frequently to monitor repository state and history.