Video Link HERE
If you are working on a project,
- You need to have some sort of way to keep track of all the changes that is happening in your project.
- You need to be able to see what changed, when, by whom
- You need to be able to see the difference in the files you just worked on and compare it with how it was in a point in time
- You need to be able to revert a mistake you made
- Multiple people should be able to work on the same code base withou messing up everything etc
Solution? Version Control Software
Git is an open source version control software, used to, well, version control Version control simply means keeping track of changes to your projects as it grows
- You can install git on windows on WSL or install Git Bash
- On MacOS, you can do
brew install git
- On any Linux distro
sudo apt-get install git
orsudo dnf install git
orsudo yum install git
should work
- Git is the open source tool
- Github is an online web based "git" hosting service
- Don't use GUI tools to learn git, just don't. Use the command line
- Don't try to cram everything at once. Learn the basics, use them regularly and use Google when you get stuck
- If you mess up your repository, instead of trying to clone a fresh copy of the repository, try to fix it. That is how you learn
A repository simply means a place to store your project and the changes to it
There are two types of repositories Local and Remote
Means it sits on your computer's hard drive (or SSD, you know).
Sits on another computer. Git can work over HTTP or SSH. More on it later If you have multiple people working on the project, you probably need a remote repository Also, if you want to have a copy of your project somewhere other than your computer, you need a remote repository
This will be quick
Before you start, let's first configure your name and email so that git knows who is making the changes. More on it later
git config --global user.name "Mansoor"
git config --global user.email "[email protected]"
git init
- Initializes a local repository Means, the current directory is under git now.
Look at ls -la .git
and you can see where the git magic happens
git status
- Shows the current status of the repo
Create a new file and use git add <filename>
to add it to staging area
git add .
- add the current directorygit add directory/another
- Add a specific directory to staginggit add *.py
- add all .py filesgit add f*
- Add everything starting withf
.
You get the idea
Commiting means to commit to the change you just made. Meaning git will store
this change in it's magic storage (.git
directory)
git commit
- Opens the editor (set by the $EDITOR environment variable - You can change it usingexport EDITOR=editor_name
) where you can write a commit message
Commit message is what will be recorded as a message identifier for your change
-
git commit -v
- Does the same thing as above, but shows the changes too, in the editor -
git commit -m "A beatuful short description of the changes"
- Commits instantly with that message
Why do we need staging area? Because you may not want to store all the files in git. Like temporary files, log files etc
You can use the .gitignore
file to do that. Just create a file of the name
in the git repository and add what you want to be ignored
Example:
foobar
cache/
This will ignore foobar
and cache
from being stored in git
git log
- View all the commits in the current repositorygit log -p
- View all the commits and the changesgit log -p <filename/folder>
- Show changes, but for the file/folder
After you made a change to a file, before commiting, if you want to see what change you just did
git diff
- Show the diff since last commit
Git allows multiple people to work on the same repository without messing up everything. This is achieved using branches
master
is the default branch. It should be respected and try not to mess it up
If you want to work on two independent changes to your code base, branches are your friend
Example: fix-bug
branch to fix an emergency bug, and feature1
to parallely work on a
feature
Branches do not know of the changes in each other until you merge them
git branch
- Shows the current branchgit checkout <branchname>
- Switches to branchnamegit checkout -b <branchname>
- Create a new branch "branchname" and switches to it
Note: When working on a new feature, we usually do
git checkout -b featurename
from themaster
branch
Once you are done with all the changes in your feature
branch, You can merge it to master
using git merge feature
while on the master branch
While on feature
branch, git merge master
will merge the master branch onto your feature
branch
This is usually needed to keep your feature
branch up to date with the master branch