Git is a version control software, and GitHub is a service that hosts Git repositories online, enabling collaboration and version tracking.
A VCS tracks changes to files, allowing you to manage and control the versions of your code, and revert changes if needed.
-
Check Git Version
git --version
Displays the current version of Git installed on your system.
-
Show Current Directory
pwdPrints the current working directory.
-
List Directory Contents
ls
Lists the files and directories in the current directory.
-
Set Global User Email and Username
git config --global user.email "Vasu.sahu.8080@gmail.com" git config --global user.name "Vasu10134"
-
View Git Configuration
git config --list
Displays the current Git configuration settings, such as user email and username.
- Write Code: Create or modify files.
- Add Files: Stage the files you want to commit.
- Commit Changes: Record the changes to the repository.
-
Track Repository Status
git status
Displays the current status of the repository, showing which files have been modified, added, or deleted.
-
Change Directory
cd <folder_path>
Changes the current directory to the specified folder.
-
Check Current Directory
pwdAfter using
cd, this confirms you’ve navigated to the correct folder. -
Initialize a Git Repository
git init
Initializes a new Git repository in your current directory, creating a
.gitfolder that tracks changes. -
Add Files to Staging Area
git add <file1> <file2> <file3> git add .
The first command adds specific files, while the second adds all files in the directory to the staging area.
-
Commit Changes with a Message
git commit -m "First commit"Commits the changes made, with a message describing the changes.
-
View Commit History
git log git log --oneline
The first command shows detailed commit history, while the second provides a more concise, one-line view.
A .gitignore file tells Git which files or directories to ignore, preventing irrelevant or sensitive data (like logs, credentials, etc.) from being added to the repository.
Example .gitignore entries:
# Ignore log files
*.log
# Ignore node_modules directory
node_modules/
# Ignore system files
.DS_Store
-
Committing Changes Multiple Times: You can commit as many times as needed. After making changes to files, simply run:
git add . git commit -m "Describe the changes"
-
Git Log Options: You can customize how you view your commit history.
git log --onelineshows a compact log with one commit per line.git log --graph --onelinedisplays a graphical representation of the commit tree.
- Definition: A branch is a lightweight, movable pointer to a commit. It allows multiple lines of development in a repository.
- Use Case: Used to isolate work, for example, developing a new feature or fixing a bug, without affecting the main codebase.
- Definition: HEAD is a pointer to the current branch reference or the last commit in the working directory.
- Use Case: Used to track where you are in the repository's commit history. For example, when you switch branches, HEAD points to the tip of the new branch.
- Definition: Switches to the specified branch in your repository.
- Use Case: Preferred for switching between branches or creating a new branch. Safer and more intuitive than
git checkoutfor branch-related tasks.
- Definition: Used to switch branches or restore files in the working directory to their state in another branch or commit.
- Use Case: Often used for both branch switching and file restoration, though it's now partially replaced by
git switchandgit restore.
- Definition: Combines the changes from the specified branch into the current branch.
- Use Case: Used to integrate changes from one branch into another, such as merging a feature branch into the main branch.
- Definition: Renames an existing branch from
<old-branch-name>to<new-branch-name>. - Use Case: Used to rename a branch locally.
- Definition: Lists all branches in the repository and shows the current branch with an asterisk (*).
- Use Case: Used to view available branches or create/delete branches with additional options.
- Definition: Shows the changes between the working directory and the index or between two commits.
- Use Case: Used to review uncommitted changes before committing or changes between commits.
- Definition: Shows the changes between the staging area and the last commit.
- Use Case: Used to view changes that are staged for the next commit.
- Definition: Temporarily saves changes in the working directory that are not ready to be committed.
- Use Case: Used to store unfinished work when switching branches or pulling updates.
- Definition: Lists all stashed changes in the repository.
- Use Case: Used to view the saved stashes with their identifiers.
- Definition: Creates a lightweight or annotated tag for a specific commit.
- Use Case: Used to mark a specific commit as important, such as marking a version release.
- Definition: Lists all tags in the repository.
- Use Case: Used to view all tags, such as version numbers.
- Definition: Rebasing moves or re-applies commits from one branch onto another.
- Use Case: Used to create a linear commit history by integrating changes from one branch into another without merging.
- Definition: Reflog records updates to the tip of branches or HEAD in the repository.
- Use Case: Used to recover lost commits or track reference updates, even after branch deletion.