-
Initialize a Git repository:
git init
-
Add files to the staging area:
git add <filename>
or to add all files:
git add . -
Commit the changes with a message:
git commit -m "Initial commit" -
Add the remote origin URL to your local repo:
git remote add origin https://github.com/username/repo-name.git
-
Push the code to the GitHub repository:
git push -u origin main
Git allows you to configure settings at two levels:
- Apply only to a specific repository.
- Useful when working with different identities across multiple projects.
- Apply system-wide for the current user.
- Ideal for setting a default identity for all repositories.
Set your global user name and email:
git config --global user.name "Ronnie Coleman"
git config --global user.email "example@email.com"- These commands store your identity in the global Git configuration file.
- Git uses this info to associate commits with your name/email.
- The
--globalflag makes this the default for all repositories.
-
Check Git status:
git status
Note: If no Git repository exists in the directory, you'll get an error like:
fatal: not a git repository (or any of the parent directories): .git -
Initialize a Git repository:
git init
-
Initialize a Git repository:
git init
-
Add files to the staging area:
git add <filename>
or:
git add . -
Check the status:
git status
- Staged files are ready to be committed.
-
Stage the files:
git add <filename>
or:
git add . -
Commit with a message:
git commit -m "Your message here"or simply:
git commit
(opens the default editor to write a commit message)
-
View full commit logs:
git log
-
View concise commit history (oneline format):
git log --oneline
.gitignorespecifies which files or directories should be ignored by Git.- Useful for excluding:
- Temporary files
- Build artifacts
- Environment config files
- IDE-specific folders (like
.vscode,.idea)
- Git does not track empty folders by default.
- To include an empty directory in your repo, add a
.gitkeepfile inside it. - This is a common convention (not an official Git feature).
Branching allows you to create independent lines of development within your repository.
git branchgit branch branch-01git switch branch-01or:
git checkout branch-01git switch -c branch-02When you're ready to bring changes from one branch into another (typically from a feature branch into main), use the following:
git merge <branch_name>This integrates changes from <branch_name> into your current working branch.
Sometimes, Git can’t automatically merge changes due to conflicts (e.g., edits on the same line).
- Run the merge command.
- Open the conflicted file.
- Manually remove the conflict markers (
<<<<<<<,=======,>>>>>>>). - Edit to resolve the differences.
- Save the file.
- Add and commit:
git add <file> git commit -m "Resolved merge conflict"
- Run the merge command.
- Open the conflicted file in VS Code.
- Use the UI options (e.g., "Accept Current Change", "Accept Incoming Change", or both).
- Save the file.
- Add and commit:
git add <file> git commit -m "Resolved merge conflict using VS Code"
git diff is a command used to show the differences between two Git data sources (working directory, staging area, commits, etc.)
git diffShows changes between your working directory and the staging area. If nothing is staged, it shows nothing.
a/→ File A (original)b/→ File B (changed)---→ Lines from File A+++→ Lines from File B@@→ Line number context
git diff --stagedgit diff branch-01 branch-02git diff <commit-hash-1> <commit-hash-2>git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else.
If you're working on a branch and need to switch but have uncommitted changes, stash them temporarily.
git stashgit stash save "work in progress"git stash listgit stash applyNote: This keeps the stash after applying it.
git stash apply stash@{0}git stash popgit stash drop stash@{0}git stash clearTags are used to mark specific points in Git history as important, typically used to label release versions.
git taggit tag v1.0git tag -a v1.0 -m "Release version 1.0"git tag -a v1.0 <commit-hash>git push origin --tagsgit tag -d v1.0git push origin --delete tag v1.0Rebase is used to move or combine a sequence of commits to a new base commit — useful for cleaner commit history.
Rebase replays your work from a new point, making your history linear and clean.
Ensure that you are on the branch you want to rebase
- Start a Rebase
git rebase <base-branch>- Rebase Current Branch onto Main
git checkout feature-branch
git rebase main- Abort an Ongoing Rebase
git rebase --abort- Continue Rebase After Conflict Resolution
git rebase --continueReflog is like a logbook of everything you've done in Git — even the changes not shown in regular logs.
It tracks all your HEAD movements, so you can recover lost commits.
- Show Reflog History
git reflog- Recover a Lost Commit
git checkout <commit-hash>- Create a New Branch from Lost Commit
git branch recovered-branch <commit-hash>- Reset to an Earlier Safe State
git reset --hard <commit-hash>