0% found this document useful (0 votes)
13 views4 pages

Unit 3 - Staging and Commits

This document provides an overview of staging and commits in Git, including commands such as git init, git add, git commit, and git log. It explains the purpose of the .gitignore file, the use of git stash for temporary changes, and the differences between git revert and git reset. Additionally, it highlights best practices for managing a Git repository and collaborating with others.

Uploaded by

mecap69891
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Unit 3 - Staging and Commits

This document provides an overview of staging and commits in Git, including commands such as git init, git add, git commit, and git log. It explains the purpose of the .gitignore file, the use of git stash for temporary changes, and the differences between git revert and git reset. Additionally, it highlights best practices for managing a Git repository and collaborating with others.

Uploaded by

mecap69891
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Unit 3 - Staging and Commits in GIT

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.

Here's how you use git init:

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

Ignore a specific file with the given name and extension.

2. All Files of a Type:

*.ext

Ignore all files with a certain extension (e.g., *.log).

3. Directories:

directory/

Ignore an entire directory and its contents.

4. Nested Directories:

directory/subdirectory/

Ignore a specific subdirectory within a directory.

5. Comments:

# This is a comment

Use comments to document your .gitignore file.

GIT Add, Commit, Clone, Stash:


1. Git Add: The git add command is used to stage changes for commit. It adds modified and new
files to the staging area, which is a buffer where you assemble changes before committing them.
This command doesn't commit the changes; it prepares them for commit.

Example:

git add file1.txt # Stage file1.txt for commit

git add . # Stage all changes in the current directory

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:

git commit -m "Add feature X" # Commit changes with a message

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

git clone https://github.com/user/repo.git # Clone a repository from GitHub

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

 To apply the stashed changes back:

git stash apply

 To apply and remove the stashed changes:

git stash pop

GIT Revert and Reset:


1. Git Revert: The git revert command creates a new commit that undoes the changes introduced
by a specific commit. It doesn't remove the original commit from history; instead, it creates a
new commit that effectively "reverts" the changes.

 To revert a specific commit:

git revert commit_hash

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.

git reset --soft commit_hash

 Mixed Reset (Default): Similar to a soft reset, but also clears the staging area, effectively
uncommitting changes.

git reset commit_hash

 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.

git reset --hard commit_hash


Unit 3 - Staging and Commits in GIT

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:

git log -n 10 # Show the latest 10 commits

 To view a condensed summary of each commit, you can use the --oneline option:

git log --oneline

 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:

git log <branch_name>

git log <file_path>

You might also like