Git is a distributed version control and source code management system.
In simple word Git is a type of version control system (VCS) that makes it easier to track changes to files.
Ummm!! What is version control then?
Version control is a system that records changes to a file(s), over time.
What is the importance of git in development world ?
Not a definition
Suppose you thought you want to work on x project and n number of persons working on a same repository(We'll come in to repository later, As if now it's your working folder that's hosted on somewhere else). Everyday everyone did some changes on that. One day you've made a lot of changes in to it. And now what you'll do, are you going to your peer place to show your changes or will you bring them your place to review ? 😶🌫️
I know that's a lame one 😐
I'll tell you the importance of git
- Can work offline.
- Git is flexible.
- Merging is easy.
- Branching is fast!
- Collaborating with others is easy!
- Can work offline 🤪
A Git repository is the .git/ folder inside a project. This repository tracks all changes made to files in your project, building a history over time. Meaning, if you delete the .git/ folder, then you delete your project’s history.
The .git directory contains all the configurations, logs, branches, HEAD, and more.
This is basically the directories and files in your repository. It is often referred to as your working directory.
A git commit is a snapshot of a set of changes, or manipulations to your Working Tree.
you'll going to use it in your development journey a lot
A branch is essentially a pointer to the last commit you made. As you go on committing, this pointer will automatically update to point to the latest commit.
Stages of Git
Let see , how can we make use use those things in VS code or in gitbash.
First create a folder and open the gitbash terminal into it or you can do with vscode.
Go to empty folder --> open cmd --> type code .
If you have vscode in your system.it'll open the vscode.
If you're in gitbash terminal you don't have to swicth on anything just stay on git bash terminal
Ctrt + shift + g
click on it
Create an empty Git repository. The Git repository’s settings, stored information, and more is stored in a directory (a folder) named “.git”
git config --global user.email "MyEmail@gmail.com" git config --global user.name "My Name"
Ctrl + shift + ` //type same thing in the vscode terminal git config --global user.email "MyEmail@Zoho.com" git config --global user.name "My Name"
Common for both place,type in the terminal
# Quickly check available commands $ git help # Check all available commands $ git help -a
To intentionally untrack file(s) & folder(s) from git. Typically meant for private & temp files which would otherwise be shared in the repository.
$ echo "temp/" >> .gitignore $ echo "private_key" >> .gitignore
Go to the file that you want to ignore
Ctrl + shift + p //type add to ignore // press enter , your file will be added in ignore file
Ctrl + shift + g //right click on a file , and click on .add to ignore
Common for both ,type it in the terminal
To show differences between the index file (basically your working copy/repo) and the current HEAD commit
// Will display the branch, untracked files, changes and other differences $ git status
To add files to the staging area/index. If you do not git add new files to the staging area/index, they will not be included in commits!
<!-- add a file in your current working directory --> git add HelloWorld.java <!-- add a file in a nested dir --> git add /path/to/file/HelloWorld.c <!--Regular Expression support! --> git add ./*.java <!-- You can also add everything in your working directory to the staging area.--> git add -A
you can add discard here also in Vs code
if you click on the `+` button the files will be staging area if you click on the `🔙` button (I've no alter native) Then your lastest changes will discard. if click on the file it open you file that's on the beginning
# list existing branches & remotes git branch -a # create a new branch git branch myNewBranch # delete a branch git branch -d myBranch # rename a branch # git branch -m <oldname> <newname> git branch -m myBranchName myNewBranchName
list existing branches & remotes
create a new branch
Ctrl + shift + p Type branch , your are able to see the features that are listed in
I'm going through each of it,just show some other useful thing in git.In below image you're able to see all the features that're in vscode for git.
# Clone repository git clone https://github.com/adambard/learnxinyminutes-docs.git
ctrl + shift + p Type --> git clone ---> enter ---> paste the URL
Stores the current contents of the index in a new “commit.” This commit contains the changes made and a message created by the user.
# commit with a message git commit -m "Added multiplyNumbers() function to HelloWorld.c"
ctrl + shift + p type a commit message
Click on the 1st and then 2nd.
Push and merge changes from a branch to a remote & branch.
# To link up current local branch with a remote branch, add -u flag: git push -u origin master # Now, anytime you want to push from that same local branch, use shortcut: git push
Click on the circular button
Click on push
That's it for now I'll keep on updating the article about.
I hope this blog gives you a basic understanding of how git works behind the hood. How easily we can manage through vscode also