Week 1
Week 1
Fundamentals of Git
Git is a powerful version control system widely used in software development for tracking
changes in source code during development. Here’s a rundown of the fundamental concepts and
commands you should be familiar with:
1. Repositories
• Repository (Repo): A Git repository is a directory that contains your project files and a
.git subdirectory with all the version history and metadata.
2. Basic Commands
• git init: Initializes a new Git repository in the current directory. This creates a .git
directory that stores all version control data.
• git clone <url>: Clones an existing repository from a remote server to your local
machine.
3. Staging and Committing
• git add <file>: Stages changes in the specified file(s) for the next commit. You can use
git add . to stage all changes.
• git commit -m "message": Commits the staged changes to the repository with a
descriptive message.
4. Branching and Merging
• git branch: Lists all branches in the repository. With git branch <branch-name>, you can
create a new branch.
• git checkout <branch-name>: Switches to the specified branch.
• git merge <branch-name>: Merges the specified branch into the current branch.
5. Remote Repositories
• git remote -v: Lists the remote repositories associated with your local repository.
• git fetch: Fetches updates from a remote repository but does not merge them.
• git pull: Fetches and merges updates from a remote repository into your current branch.
• git push: Pushes your local commits to a remote repository.
6. Viewing Changes
• git status: Shows the current status of your working directory and staging area.
• git diff: Shows the differences between the working directory and the index (staging
area). You can also compare different branches or commits.
7. Undoing Changes
• git reset: Moves the HEAD pointer to a previous commit and optionally updates the
index and working directory.
• git revert <commit>: Creates a new commit that undoes the changes of a specified
commit, preserving history.
8. Logs and History
• git log: Shows the commit history for the current branch. You can use various flags to
format the output.
9. Tagging
• git tag <tag-name>: Creates a tag at the current commit. Tags are useful for marking
release points.
10. Configuration
• git config --global user.name "Your Name": Sets the name for commits.
• git config --global user.email "[email protected]": Sets the email for
commits.
Best Practices
• Commit Often: Make frequent, small commits with meaningful messages.
• Use Branches: Keep different features or fixes in separate branches to keep your work
organized.
• Pull Before You Push: Always pull the latest changes from the remote repository before
pushing your own changes to avoid conflicts.