This project aims to provide a comprehensive understanding of Git repositories and their components, including the workspace, index, local repository, and remote repository. The motivation behind this project is to simplify the concepts for both new and experienced users, ensuring they can effectively manage their codebase using Git.
The workspace, or working directory, is where you modify and create files. This is the directory on your local machine where you make changes to your project files. The files in it can be in an untracked, modified, or deleted state.
git status: Shows the status of files in the working directory.
The index, or staging area, is where you prepare changes before committing them. It acts as a middleman between your working directory and the local repository. It holds snapshots of your changes that will be included in the next commit.
git add <file>: Adds changes to the staging area.git reset <file>: Removes changes from the staging area.
The local repository is where Git stores the history of your project. This is the .git directory inside your project folder, containing all commits, branches, and tags. It stores the committed history of your project.
git commit -m "message": Commits staged changes to the local repository.git log: Shows the commit history.
A remote repository is a version of your project hosted on the internet or another network, allowing you to collaborate with others. Common hosting services include GitHub, GitLab, and Bitbucket. It serves as a centralized repository for collaboration.
git push <remote> <branch>: Pushes local commits to the remote repository.git pull <remote> <branch>: Fetches and merges changes from the remote repository.git clone <url>: Creates a local copy of a remote repository.
In Git, HEAD is a pointer that references the current commit in your repository. It's a critical concept in understanding how Git manages the state of your working directory and your history of commits.
HEADpoints to the latest commit in the currently checked-out branch.- For example, if you're on the main branch,
HEADwill point to the latest commit inmain.
- When
HEADpoints directly to a specific commit (not a branch), you are in a "detached HEAD" state. - This can happen if you checkout a specific commit using its hash:
git checkout <commit-hash>.
- You can use
HEADto navigate relative to the current commit. HEAD^refers to the parent ofHEAD.HEAD~2refers to the grandparent ofHEAD(2 commits before the current one).
git log -1: Shows the latest commit (the commit whereHEADis pointing).git checkout <branch-name>: MovesHEADto the latest commit of the specified branch.git reset --hard HEAD~1: Resets the current branch to the previous commit, discarding changes in the working directory and staging area.
