3. Basic Git Commands
git add
git addThe 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:
git add [file/directory]
Examples:
To add a single file:
git add filename.txtTo add all files in the current directory (and subdirectories):
git add .To add all files with a certain extension (e.g., all
.txtfiles):git add *.txt
git commit
git commitOnce 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:
git commit -m "Your commit message here"
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:
bashCopy code
git commit -m "Add initial version of the README file"
git status
git statusThe 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 status
git log
git logThe git log command displays the commit history, showing details about each commit, including the author, date, and commit message.
Usage:
git log
For a more concise view with one line per commit, you can use:
git log --oneline
git diff
git diffThe 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:
git diffTo see differences in the staged changes compared to the last commit:
git diff --staged
git revert
git revertPurpose: Used to reverse the changes made by a specific commit by creating a new commit. This ensures that the commit history remains intact.
Usage:
git revert [commit-hash]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:
# Ignore all .log files
*.log
# Ignore the build directory
/build/
# Ignore the secret configuration
secret-config.json
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