GIT Bootcamp
What is GIT ?
GIT is open source version control system developed by Linus Torvalds in 2005. It keeps record of all staged/updated changes of the code and file with a message attached to it (known as commit) thus enabling to keep proper track record of every stage of the project.
Why GIT ?
One can easily switch to a particular stage of the project development history whenever needed avoiding to make multiple copies of the repository. It allows multiple people to work in the same project enabling parellel development and increasing the efficiency of the execution of the project. Also It’s been an industrial standard to know git before getting involved.
Installation
Download git from here: https://git-scm.com/downloads
You will now be able to use git in your terminal.
Verify the installation using
git --help
Github Account , SSH setup
- Let’s create Github account if you already have one you can skip this. Go to github.com and sign up.
- Let’s add ssh of your computer to your github so that you don’t have to add your credentials every-time you interact with the github.
Find_Or_Generate_Your_ssh.
i. Open terminal / Git bash terminal.
ii.ssh-keygen -t ed25519 -C "your_email@example.com"
On prompt for location and passphrase press "Enter" leaving it blank.
iii.eval "$(ssh-agent -s)"
— — — — — — —
FOR MAC USERS ONLY:
If file doesn't exists then:
open ~/.ssh/config
and paste the following:
touch ~/.ssh/config
— — — — — — — —
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
iv.ssh-add ~/.ssh/id_ed25519
v. For Windows:clip < ~/.ssh/id_ed25519.pub
For Mac :pbcopy < ~/.ssh/id_ed25519.pub
- Add your computer ssh to the github.
i. Go to your Github account settings.
ii. Settings -> ssh and gpg key.
iii. Add new ssh key.
iv. Title : Name of your laptop , Key: paste(ctrl+v). - Now create a repository( for now keep it public) and copy the ssh url.
Thats it!
Walkthrough of the basic GIT commands provided by git — help
git init
: Initialize git to new or existing project.git clone “REPO_LINK”
: Download the repository to your machine from the cloud.git add “FILE_NAME”
: Stage the changes made to files.git commit -m “COMMIT_MSG”
: add message to the staged changes.git branch “BRANCH_NAME”
: Create a branch.git checkout “BRANCH_NAME”
: Goes inside the branch.git remote add “REMOTE_NAME” “REPO_LINK”
: Adds remote name with the repo link value.git pull “REMOTE_NAME” “BRANCH_NAME”
: Pulls specific branch from the cloud repository.git push “REMOTE_NAME” “BRANCH_NAME”
: Push specific branch to the cloud repository.git merge “BRANCH_NAME”
: merge changes of another branch to the active branch.git log
: shows commit historygit status
: to know the file status of current working branch
HandsOn Experience- Push your code to the Github:
git init
git config --global user.email "yourgithub@email.com"
Initializes git in your current folder. A .git (hidden) folder is created. By default your active branch will be “master”.
P.S You can create a .gitignore file to avoid the files your don’t want git to keep track of like: configuration, credentials files etc.
git status
See the status of the files and branch.
git add .
Add files in which changes are made to staging.
Note: “.”adds all the file to staging.
git commit -m 'COMMIT_MESSAGE"
The -m flag specifies that what follows is the commit message. This is a custom message intended to let your future self or other developers what was added in that commit.
git log
It shows all of your commit histories.
git remote add origin "repo_url"
Alias to your Github url will be “origin”.
git push -u origin master
Push your code to the github.
To pull your code :
git pull origin BRANCH_NAME
Voila ! You have your local repository pushed to your github account.
Understanding Branch and code merging
Once git is initialized the default branch is “master” or “main”.
These is a little advanced for basic but I feel it lies on a thin line between basic and advanced. Let’s assume you’re working on a huge project and want to add an experimental feature. You’re not sure if it’ll work or will be accepted, you can branch off your main tree by creating a new branch and working on that branch instead. Think of it hypothetically as how a tree has branches, but when you branch off from the main branch however, you can be satisfied with the experimental features and decide to merge the two branches. In the image above, the two green dots branch off the purple branch and and when satisfied with the features, it’s merged back into the purple branch.
You can create as many branches as you like, to create a branch, use the following command.
git branch BRANCH_NAME
To view see the list of branches the project has, use the following command.
git branch --list
To switch between branches, use the following command.
git checkout BRANCH_NAME
Or you can directly create and switch to the branch with single command
git checkout -b BRANCH_NAME
Fetch vs Pull
git fetch brings the remote branch to ref/branchName
git pull does git fetch followed by git merge
Syntax:
git fetch origin BRANCH_NAME
git pull origin branchName:branchName
The process of bringing the changes from a branch / head to existing current branch /head is called merging.
Syntax:
git merge branch_name
Deleting a branch
Once the branch is merged, the standard practice is to delete the branch.
git branch -D BRANCH_NAME
// deletes your branch in local device
git push origin --delete BRANCH_NAME
//deletes branch from the repository/ cloud storage
TODO:
- Store all of your c programs in github using git.
- Store all of your c++ programs in github using git.
- Add your friends to collaborator in a github repo and push and pull your changes.
- Add your friends to collaborator in github repo and work in their own branches and push the changes. Merge branch to master branch and push to the repo.
TakeAways:
A proper understanding and usage of the basic git commands below:
1. git init or git clone
2. git add .
3. git commit -m 'commit_msg'
4. git remote add origin URL (no need if you cloned it).
5. git push origin master
6. git checkout -b branch_name (create and switch to new branch)
7. git push/pull origin master
8. git merge branch_name
9. git branch -D branch_name
10. git push origin --delete branch_name