What is Git?
git is a free opensource version control system. Most widely used version control system used by developers today. So, you might be thinking
what is this version control???
Don't worry I got you covered here as well. Version control means the management of documents, programs, large websites and other collection of information.
Layman terms, if you are changing anything to your code, git will show you which day you changed what to your code.
Terms Frequently Used
- Directory: Means Folder
- Terminal or Command Line: Interface for Text Command
- CLI : Command Line Interface
- cd: Change Directory
- Code Editor: Writing Code like VS Code
- Repository: Project/ Place where you kept your project
- Git : Tool that tracks the changes of your code over time
- Github: A Website where you host your all repositories
As a programmer I would suggest you guys to go for command line interface (CLI) as you will get accustomed Git commands. You are going to use CLI so much in your developing career. So, starting it from Git command will help you a lot.
Frequently Used Git Commands
- Clone: Bring a repository which is hosted in a site like github to your local machine
- add: Track your file and changes in Git
- commit: Save your file in Git
- push: Upload git commit to a remote repository, like Github.
- pull: Download changes from remote repo to your local machine,the opposite of push
So what are you waiting for signup now and open a github account and explore. Once you will sign-up. page will look something like this:
Create a repository using New
one the left side or +
on the top of right side or you can create one using your local machine.
Using Local Machine Cretae a Repo
- Open Command line
- Check if git already installed or not
- If yes, check version using command: git --version
- If not, go though the below link, Git install Link
- Now you need a code editor. I am going to tell you in Visual Studio.
- Open a Folder in Visual Studio.
- There will a terminal in visual studio.
- Now clone the repo to your local folder. Type below in VS terminal. The link I copied from Github repo.
git clone https://github.com/Ramakm/demo-.git
More Git Commands
-
ls
: in the terminal you will see all the inside folder as well which is not showing in the left side like .git folder(hidden).Now change anything in README.md or add any new file to your DEMO folder. -
git add .
: This will add all your changed file and newly added file to the .git folder. -
git add index.html
: You can add files individually as well to git folder. Or go with previous one to add at a time. -
git status
: To check all the files which are added to the git folder.
-
git commit -m "Updated new and old"
: Here -m means message. The message can be anything, a letter or a sentence but always type what you have done for that commit. Commit is not live in Github yet. Its commited on local now.
Next step is to create a SSH key for your profile. I am adding a link here where step by step process mentioned just go for it. Once done follow below steps.
SSH Key Building Process
GIT Push
git push origin master
:
Origin is set for us here basically means the word for the location set for us here on Github. Master is the branch we want to push to I will explain more about master in another section with details.
How to initialize a repo locally and push to Github?
- Create a folder in your IDE (Here I am using VS Code). Let's say Demo-2
- Add a file to that. It can be any file. .html, .css, .js, .py etc. I am adding a file name README.md
- Add anything inside that file.
- Go to VS Code terminal
-
git init
: This command is to Initialize git and get into the folder .git -
git add README.md
: You can add your respective file. The file will be added to .git folder -
git status
: The newly added file will show now to you. -
git commit -m "Added README file"
: Commit that added file now. -m is for message. Inside " ", you can type any message but good to type message related to the changes. - No Create a blank repository in github.com as already mentioned in this blog.
-
git remote add origin https://github.com/Ramakm/Demo-2.git
: Copy the SSH or HTTPS link of the=at repo from gihub site and paste in after origin. Here we are linking the remote repo. -
git remote -v
: This will show the origins we have inside that remote files. -
git push -u origin master
: Now push your files to master branch in your github repo. All set now. - Refresh your github repo. you will see the newly added files there.
BiNgO!!!!!!!!
Github Workflow
Git Branching
Master Branch:
The default branch name in Git is master . As you start making commits, you're given a master branch that points to the last commit you made. Every time you commit, the master branch pointer moves forward automatically. The “master” branch in Git is not a special branch.
Feature Branch:
The core idea behind the Feature Branch Workflow is that all feature development should take place in a dedicated branch instead of the main branch. This encapsulation makes it easy for multiple developers to work on a particular feature without disturbing the main codebase.
One very common thing in development that, in feature branch we are working on for months, then suddenly you find out you have a major bug to fix it quickly. For that there a term called hotfix branch.
Commands for branching
-
git branch
: Check how many branches are available. For our Demo-2 projects, it will show only one branch that is "*master". * means i am currently inside that branch. -
git checkout -b feature-readme
: Git checkout is use to switch between branches but here we are adding -b is just for creating a new branch. feature-readme is branch name.
- Make some changes to README file again.
- Do commit and all the necessary steps mentioned before to commit the changes.
- Remember we are still in the branch not in the master. If you want to switch to master just do: git checkout master
- All commit has been done inside the branch feature-readme.
-
git diff feature-readme
: Will show the difference you made on that branch -
git push
: Will ask you The current branch feature-readme has no upstream branch. To push the current branch and set the remote as upstream. -
git push --set-upstream origin feature-readme
: -u is the short form of Upstream. you can use any one. Pushed it your newly created branch in github.
What is a pull request
A pull request is an event in Git where a contributor asks a maintainer of a Git repository to review code they want to merge into a project.
Here, We have a feature-readme branch and we want to that branch to be pulled into the master branch. So, we make a pull request from the feature branch to the master branch.
Once we make a request, anyone can review our code and ask us to change anything if needed. We will make those changes and push it the branch again.
Once PR(Pull Request) is get merged to the master branch, You are generally delete that feature/source branch and switch to master branch. Then when you need additional coding changes, you will need a new branch and start your process over.
How to do it manually on github?
Once its done. There is a merge option where you can merge those two master and feature branch now.
- Now we can delete our feature branch as we merge the changes to the main branch.
Command line to pull and delete feature branch
git checkout master
git branch
git pull origin master
git branch -d feature-readme
git branch
Undoing in Git
If you have dome something like some changes in your repo and you want to undo it.
git reset README.md
git status
Undo a commit
Let's say added a index.html file to our main branch.
git add index.html
git commit -m "Added a index file"
-
git reset HEAD~1
: This will reset the last commit made by us. git diff
: You will see the changes nowgit log
: See all the commits you have made by far now.
Forking in Git
- Let's say you want change or work on a project which is available on Github. The first thing you have to do is fork that repo.
- Once you will fork a repo, it will show you in your dashboard/repositories and you can do anything you want.
- If you want to merge the change you made in the one which you forked and want to merge it with original one. Then you need to create a pull request.
YAAYY!
Thanks for reading till this. You almost know everything now related to git and Github.
If you really like this blog, Please give me a follow on Github and comment below if you want me to add anything.
Github-@Ramakm
Twitter-@codewith_ram
Happy Learning
Top comments (0)