GIT Info
GIT Info
What is Git? 1
Understanding with a common example 2
Let’s Understand in Detail 2
Remote vs. Local Code 2
Branches 4
Must know Git Commands and their meanings 4
1. Git Configuration 5
2. Starting a Project 5
3. Day to Day Work 6
4. Storing Your Work: 9
5. Git Branching Model: 10
6. Inspect History: 11
7. Tagging Commits: 11
8. Reverting Changes: 12
10. Synchronizing Repositories: 12
GIT CHEAT SHEET 13
BONUS - YOUTUBE VIDEOS 18
What is Git?
Git is like a time machine for your code. It tracks changes you make to your files, so you can
undo mistakes, collaborate with others, and keep a history of your project's progress.
Understanding with a common example
Imagine you have a folder on your computer where you're working on a project, like writing an
essay. Every time you make a change to the essay, you save it. Now, let's say you want to try
something different, but you're worried you might mess up the essay. What if you could take a
snapshot of the essay every time you make a change, so you could always go back to an earlier
version if needed? That's kind of (not exactly) what Git does, but for computer files.
Git also supports branching, which allows you to diverge from the main line of
development and work on separate features or fixes without affecting the main codebase
Local code is the code that lives on your own computer. It's where you write, edit, and test your
projects. Imagine your local code as your personal workspace where you build and shape your
creations.
Example: You're working on a cool website project called "MyWebsite" on your computer. All
the files and folders related to this project, like HTML, CSS, and JavaScript files, reside in a
folder on your desktop.
Remote Code
Remote code, on the other hand, refers to code that exists on a server or another computer,
usually accessed over the internet. This could be code hosted on platforms like GitHub,
Bitbucket, or GitLab. Remote repositories serve as centralized hubs where multiple people can
collaborate on the same project.
Example: You decide to share your "MyWebsite" project with your friend who lives in another
city. You upload your project code to a website like GitHub or Bitbucket, making it accessible to
your friend over the internet. Now, both you and your friend can work on the same project, even
though you're in different locations.
Branches
Imagine you're working on a group project with your classmates, each contributing different
parts of a program. Creation: Branches are like creating separate copies of your project's code.
You start a new copy (branch) to work on a specific feature or fix without changing the main
version/code.
Isolation: Whatever changes you make in your branch stay there until you're ready to share
them. This means you can experiment with new code without affecting the main project.
Collaboration: Your classmates also have their own copies of the project. You can all work on
different parts of the program at the same time, like different functions or modules.
Merging: When you're done with your changes and they've been tested, you merge them back
into the main code. This combines your changes with the main project so everyone can use
them.
Organization: Using separate branches helps keep the project organized. Once your changes
are merged into the main code, you can focus on the next task. By using branches, you and
your classmates can work together on the project without interfering with each other's work. It's
like having your own workspace to experiment and contribute to the final program!
This command sets your name for Git. Replace "Your Name" with your actual name. This
allows Git to associate your name with the changes you make.
Similarly, this command sets your email address for Git.Your email address is used for
communication related to your Git activities, such as notifications and commit logs. git
config --global color.ui auto: This command enables colorization for Git output, making it
easier to read and understand. It's a helpful visual aid, especially when dealing with
complex Git commands or reviewing commit logs.
By configuring these settings globally (--global), you ensure that they apply to all Git
repositories on your system. Once you've run these commands, Git is set up to
recognize you as a contributor to your projects, and your contributions will be properly
credited.
2. Starting a Project
1. Initializing a New Repository: If you're starting from scratch and want to track changes in
your project folder, you'll initialize a new Git repository. This essentially tells Git to start
monitoring the files in that directory for any changes you make. You can do this by navigating to
your project folder in the terminal and run :
git init
After running this command, Git sets up a new repository within the project directory, and you
can start adding files, making changes, and committing them to Git's history. We will
understand about how to add and commit files in git later in this document
This command fetches the entire project and sets up a local copy on your machine, allowing you
to start working on it immediately. Whether you're initializing a new repository or cloning an
existing one, Git provides you with a solid foundation to manage your project's version control
effectively. This ensures that you can track changes, collaborate with others, and maintain a
history of your project's development with ease.
Every day, you'll use Git to manage your coding projects. You'll check what's happening
in your project with git status. When you're happy with changes in a file, you git add [file]
to tell Git to remember them. Finally, you git commit to save those changes with a short
description of what you did and then eventually push your changes to remote, so that
others can see those changes as well.This simple workflow keeps your project organized
and helps you track your progress.
git status:
Imagine you're working on a group project, and you want to know what's happening with the
files. Git status is like asking the project manager for updates. It tells you which files have been
changed, which ones are ready to be saved, and which ones are not being tracked
yet.Example:
When you're happy with the changes you've made to a file and want to include them in
the next save point, you use git add. It's like putting those changes in a box and saying,
"Hey, Git, remember these for me!"Example:
git push
After you've committed your changes, you might want to share them with others or save them
online. Pushing is like sending your saved changes from your computer to a central location,
like GitHub or GitLab. It makes your work accessible to teammates and acts as a backup in
case something happens to your local files.Example
This command sends your committed changes to the "master" branch on the remote repository
named "origin." Others can then see and access these changes.
git pull
Sometimes, while you're working on a project, others may make changes to the same
files you're working on. Git pull helps you stay up-to-date by bringing in those changes
from the remote repository to your local copy. It's like fetching the latest updates from
the central server and merging them into your own work.Example:
This command fetches the latest changes from the "master" branch of the remote
repository named "origin" and merges them into your local branch. It ensures that your
local copy reflects the most current state of the project.
4. Storing Your Work:
● git stash: Imagine you're tidying up your desk by temporarily hiding your work
under a mat. Similarly, git stash hides your changes temporarily so you can work
on something else.
● git stash pop: When you're ready to bring back your hidden work, it's like lifting
the mat off your desk. git stash pop retrieves your hidden changes and applies
them back to your project
● git stash drop: If you've hidden some work that you don't need anymore, you
can get rid of it, like throwing away notes you no longer need.
5. Git Branching Model:
● git log: Imagine keeping a diary of all the changes you've made in your project.
git log lets you see a list of all your entries (commits) in that diary. Example:
● git diff : Comparing versions of your project is like seeing what's changed
between two diary entries. git diff shows you the differences between the current
state of your project and a previous version. Example:
7. Tagging Commits:
8. Reverting Changes:
9.
● git reset: If you've made a mess in your project, you can start over by
resetting it to a previous state, like rewinding to an earlier point in your
diary.
● git revert [commit sha]: Making a new change that undoes an old one is
like writing a correction in your diary. git revert [commit sha] creates a new
entry (commit) that reverses the changes made in a previous one.