3. Basic Git Commands
git add
git add
The git add
command is used to stage changes for commit. In other words, it tells Git that you want to include updates from certain files in the next commit.
Usage:
Examples:
To add a single file:
To add all files in the current directory (and subdirectories):
To add all files with a certain extension (e.g., all
.txt
files):
git commit
git commit
Once you've staged your changes with git add
, you use git commit
to capture your changes in a snapshot, which can be thought of as a "save point" in your project's history.
Usage:
The -m
flag allows you to add a short message describing the commit. It's essential to write clear and descriptive commit messages to understand the project's history later.
Example:
git status
git status
The git status
command shows the status of changes in the working directory and staging area. It'll tell you which changes have been staged, which haven't, and which files aren't being tracked by Git.
Usage:
git log
git log
The git log
command displays the commit history, showing details about each commit, including the author, date, and commit message.
Usage:
For a more concise view with one line per commit, you can use:
git diff
git diff
The git diff
command shows the differences between your working directory and the last commit. It's useful to see what changes you've made before staging them.
Usage:
To see differences in the working directory not yet staged:
To see differences in the staged changes compared to the last commit:
git revert
git revert
Purpose: Used to reverse the changes made by a specific commit by creating a new commit. This ensures that the commit history remains intact.
Usage:
Example Scenario: If you've introduced a change that, in hindsight, isn't needed or is problematic, but you want to keep a record of all actions (including the mistake and its correction), you'd use
git revert
.
.gitignore
.gitignore
.gitignore
isn't a command, but a special file that you place in the root of your repository. It tells Git which files or directories to ignore and not track. This is useful for excluding files that you don't want in your repo, like temporary files, logs, or files containing sensitive information.
Example .gitignore
content:
To make Git ignore the files/directories listed in .gitignore
, simply create or edit the .gitignore
file in your repository's root directory and list the patterns for files or directories you want to exclude.
Last updated