What is Version Control System?
Version or Source control system is an application that keeps track of the project and manages changes to code.
Why do we need Version Control System?
problem statement
let's assume we are building an application and it's working perfectly. Then you changed the code and now the application broke. Now it is very hard to find that old code. Version Control System solves this problem
Solution
The version control system keeps track of all the files. If you change something it will store that and the main advantage is It will save the history of that project and If we want we can access that. So if we make any mistake and the application breaks we can revert back to any previous checkpoint.
What are Git & Github?
What is Git?
- Git is a very popular free, and open-source distributed version control system.
- It can handle everything from small to large projects with speed and efficiency.
Components of Git
Repositories
- A GitHub repository works like a folder.
- we keep our code inside this repository.
- we can create folders inside the repository.
Branches
- If we want to work with different versions of a repository we use branches. For example, if we want to create new functionality for an existing application we create a new branch and we can work on that branch so the main branch always stays the same, and if the new functionality works then we can merge those two branches.
- The default branch is the main branch
- We can create as many branches as we want.
What are the Master(main) branch and Head branches?
- That HEAD is a label or pointer to the most recent commit of the branch you are currently on. It will change if we move to a new branch.
- Master(Now main) is the default branch created when you initialized a git repository.
- If we want to can not delete the Head pointer but we can delete the master branch.
What are the Remote & Local branches?
- A local branch is a branch that only we (the local user) can see. It exists only on our local machine.
- A remote branch is a branch in a remote location.
Some important Commands related to the git branch.
Create Branch:
- we can create a new branch with the help of this command.
git branch <branch name>
List Branch:
- we can List all the available branches in your repository by using this command.
- we can use any of these two commands.
git branch --list
or
git branch
Delete Branch:
- we can delete the specified branch.
- It is a safe operation because Git will not allow us to delete the branch if it has any unmerged changes.
git branch โd <branch name>
Delete a Remote Branch
- We can delete remote branches also.
git push origin -delete <branch name>
Switch Branch
- we can switch between the branches without making a commit.
git checkout<branch name>
Switch to master branch
- We can switch to the master branch from any other branch.
git branch -m master
Rename Branch
- We can rename the branch if we want to.
git branch -m <old branch name><new branch name>
Merge Branch
- This is probably the most important command.
- If we want to merge two branches we can do that
- we can merge the other branch with the currently active branch.
git merge <branch name>
Commits
- if we make any changes and we want to save them we have to commit that change.
- If we want to commit the changes we have to write a commit message explaining why the change was made.
git commit -m โ<commit message>โ
Pull Requests
- This is the most important feature if we want to contribute to open source projects.
- If we made any changes inside the codebase and we want to commit we can send a proposal to the project owner so that we can merge the code we have to use pull requests.
git request-pull [-p] <start> <URL> [<end>]
Git Commands
Initialize a Git repository
- If we want to use git we need some initial file.
- If we use this command git will give us the initial files
git checkout<branch name>
Initialize a Git repo
- we can switch between the branches without making a commit.
git init
Add changed files
- Stages only the specified changes for the next commit. Replace with a to change a specific file.
git add <file name>
- Stages all files
git add .
or
git add -A
or
git add *
or
git add -u
Checking Status
- List which files are staged unstaged and untracked.
git status
Check History log
- Displays the entire commit history using the default format.
git log
Pull Code
- git pull Fetchs the remote copy of the current branch.
git pull
- Fetches the remote copy of the current branch and rebases it into the local copy.
- Use git rebase instead of the merge to integrate the branches.
git pull --rebase
Push Code
- Push all of your commits to the master(main) branch
git push origin main
- Forces the git push even if it results in a non-fast-forward merge.
git push --force
Clone repository
- Clones a remote repository
git clone <origin link>
Add Origin
- If we want to link our local repository to a remote repository.
git remote add origin <origin link>
What is GitHub?
problem statement
let's assume we are building an application and we want to collaborate with other people. So we need space or a server where we can store our code and other contributors can access it. Github solves this problem.
Solution
with the help of GitHub developers around the world can collaborate and create applications.
Installing Git on your local machine
- We can download Git from git-scm.com/downloads
Setup
Setting up a global username and email
First, we have to set the global username and password.
git config --global user.name "Your Name"
git config --global user.email youremail@email.com
Generating SSH key
ssh-keygen -t ed25519 -C "youremail@email.com"
- This command will generate a new ssh key
- Then you have to add that new ssh key to GitHub.
Now you can upload anything to GitHub
Hopefully, now you can perform basic operations using git. I will add more commands in the future. Till then Happy learning.