Git commands
Git commands
1. git fetch
Basic Syntax
git fetch
What Does It Do?
This command gets the latest updates from the remote repository (like
GitHub or GitLab) to your local machine but doesn’t change your working
files. It’s like checking if there are any new changes on a shared project,
but not applying them yet.
Why Use?
To get updates from the team without altering your current work.
It’s a safe way to check if there are any changes in the shared project
before merging them into your work.
Best Practices
Use git fetch regularly to stay up-to-date with the team’s work.
After fetching, you can review the changes before merging them.
Summary
Fetch brings the latest updates to your machine but doesn’t affect your
current work until you decide to apply them.
2. git pull
Basic Syntax
git pull
What Does It Do?
This command pulls the latest changes from the remote repository and
automatically merges them into your working files. It’s like fetching
updates and immediately applying them.
Why Use?
To get the most recent changes and instantly integrate them into your
current project.
Use it when you’re ready to get the latest version and you want to apply
those changes right away.
Best Practices
Always use git pull when you're about to start working, to ensure you have
the most up-to-date version.
Be cautious when pulling if you have uncommitted changes, as this could
cause conflicts.
Summary
git pull is a combination of fetching updates and immediately applying
them to your project.
3. git push
Basic Syntax
git push
What Does It Do?
This command sends your local changes (committed files) to the remote
repository so others can see and use them. It’s like uploading your work to
a shared server.
Why Use?
To share your work with the team.
To back up your local changes to a remote server.
Best Practices
Always commit your work before pushing.
Use descriptive commit messages so others can understand your changes.
Summary
git push sends your changes from your local project to the shared server,
allowing others to see and use your work.
4. git commit
Basic Syntax
git commit -m "Your message"
What Does It Do?
This command saves your changes locally in Git with a message
describing what you've done. It’s like saving your work with a note.
Why Use?
To record your progress and create checkpoints in your project.
To make your changes permanent in Git (locally).
Best Practices
Write clear and concise commit messages to describe your changes.
Commit regularly to avoid losing your work.
Summary
git commit saves your changes with a message explaining what was done,
marking milestones in your work.
5. git branch
Basic Syntax
git branch
What Does It Do?
This command shows all the branches (versions) of your project. A branch
is like a copy of your project where you can work on changes without
affecting the main project.
Why Use?
To view the different versions of your project or to create new ones for
specific tasks.
Useful when you're working on a new feature or bug fix without disturbing
the main project.
Best Practices
Use branches to separate tasks (e.g., bug fixes, new features) to avoid
mixing changes.
Always work on a branch and merge it back when ready.
Summary
git branch lets you see and create branches to organize different versions
of your project.
6. git merge
Basic Syntax
git merge <branch-name>
What Does It Do?
This command combines changes from one branch into another. It’s like
merging two different versions of your project back into one.
Why Use?
To bring changes from one branch (like a new feature) into the main
project.
To keep your project updated and organized.
Best Practices
Always pull the latest changes before merging to avoid conflicts.
Resolve conflicts carefully if there are any.
Summary
git merge combines changes from one branch into another, keeping your
project up-to-date.
7. git stash
Basic Syntax
git stash
What Does It Do?
This command temporarily saves your uncommitted changes, so you can
switch to another task without losing your progress.
Why Use?
To quickly save your current work and switch to something else without
losing changes.
Useful when you need to change branches but don’t want to commit
unfinished work.
Best Practices
Use git stash when you need to change tasks quickly but don’t want to
commit unfinished changes.
Always remember to apply or pop your stashes later to get back to your
work.
Summary
git stash temporarily saves your changes so you can switch tasks without
losing progress.
8. git status
Basic Syntax
git status
What Does It Do?
This command shows you the current state of your project, including which
files are changed, which are staged, and which are untracked.
Why Use?
To see the current state of your project and know what changes you’ve
made.
Helps you decide what to commit or stash next.
Best Practices
Use git status often to stay aware of the changes in your working directory.
Summary
git status gives you a snapshot of your project, helping you track changes
and decide what to do next.
9. git diff
Basic Syntax
git diff
What Does It Do?
This command shows the differences between your working directory and
the last commit. It highlights what’s changed.
Why Use?
To review what you’ve changed before committing or stashing.
Helps you make sure you’re committing the correct changes.
Best Practices
Use git diff to double-check your changes before committing them.
Summary
git diff shows you exactly what’s changed in your project compared to the
last saved version.