Unit 3 - Staging and Commits
Unit 3 - Staging and Commits
GIT Init:
The git init command is used to initialize a new Git repository in a directory, creating a new repository
from scratch. When you run git init, Git sets up the necessary files and directories to start tracking
changes in your project. This allows you to use Git's version control features to manage the history of
your files and collaborate with others.
1. Navigate to the Project Directory: Open a terminal or command prompt and navigate to the
directory where you want to create the new Git repository. You can use the cd command to
change directories.
2. Run git init: Once you're in the desired directory, run the following command to initialize a new
Git repository:
git init
This command creates a hidden .git directory in your project directory. The .git directory contains all the
metadata and configuration that Git uses to manage the repository.
3. Start Tracking Files: After running git init, you can start adding files to your repository and
tracking their changes. To start tracking a file, you need to add it to the staging area using the git
add command, and then commit the changes using the git commit command.
4. Setting Up Remote Repositories (Optional): If you want to collaborate with others or use
remote repositories (like GitHub, GitLab, or Bitbucket), you'll need to set up remote connections
using the git remote command.
5. Adding .gitignore (Optional but Recommended): It's common practice to create a .gitignore file
in your repository to specify which files or directories should be ignored by Git (not tracked). This
is useful for excluding build artifacts, temporary files, and other items that shouldn't be part of
your version control history.
6. Committing Changes: As you make changes to your files, you can use the git add command to
stage changes and then git commit to create a commit that records those changes in your
repository's history.
.gitignore file:
The .gitignore file is used to specify files, directories, or patterns that should be ignored by Git when
tracking changes in a repository. Ignoring certain files or directories is important to avoid cluttering your
version control history with irrelevant or sensitive information. Here are some common entries you
might find in a .gitignore file:
1. Specific Files:
Unit 3 - Staging and Commits in GIT
filename.ext
*.ext
3. Directories:
directory/
4. Nested Directories:
directory/subdirectory/
5. Comments:
# This is a comment
Example:
2. Git Commit: The git commit command creates a new commit in your version history. It takes the
changes you've staged using git add and records them permanently in the repository, along with
a commit message that describes the changes.
Example:
3. Git Clone: The git clone command is used to create a copy of a remote repository on your local
machine. It sets up a new Git repository, downloads the complete history, and configures a
remote connection to the original repository.
Example:
Unit 3 - Staging and Commits in GIT
4. Git Stash: The git stash command allows you to temporarily save changes you've made in your
working directory without committing them. This is useful when you need to switch to a
different branch or perform other operations that require a clean working directory.
To stash changes:
git stash
Reverting is a safe way to undo changes, as it preserves the history and avoids rewriting commit
history.
2. Git Reset: The git reset command is used to reset the state of your repository, either partially or
completely. It's a powerful command that can modify commit history, so use it with caution.
There are different modes of reset:
Soft Reset: Moves the HEAD and branch pointer to a specific commit, preserving
changes as uncommitted in the staging area.
Mixed Reset (Default): Similar to a soft reset, but also clears the staging area, effectively
uncommitting changes.
Hard Reset: Resets to a specific commit, discarding all changes in the working directory
and staging area. Irreversible if you don't have a backup or a way to recover the
discarded changes.
Be cautious when using git reset, especially with the hard reset mode, as it can result in data
loss.
GIT log:
To view all commits in a Git repository, you can use the git log command. This command displays
the commit history in reverse chronological order (newest commits first). Here's how you can use it:
git log
By default, git log displays a list of commits with information such as commit hash, author, date, and
commit message. You can navigate through the log using keyboard shortcuts. To exit the log view, press
the q key.
git log has various options to customize the output and filter the commit history. For example:
To limit the number of displayed commits, you can use the -n option:
To view a condensed summary of each commit, you can use the --oneline option:
To see the changes introduced by each commit, you can use the -p (or --patch) option:
git log -p
To view commits for a specific branch or file, you can provide additional arguments: