DEV Community

Cover image for Beginner Guide to Git !
paul-coder-22
paul-coder-22

Posted on

Beginner Guide to Git !

What is Git ?

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 🤪

Repository

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.

.git Directory
The .git directory contains all the configurations, logs, branches, HEAD, and more.

Working Tree
This is basically the directories and files in your repository. It is often referred to as your working directory.

Commit

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

Branch
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

Modified
Staged
Committed

Let see , how can we make use use those things in VS code or in gitbash.

Commands

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 . Enter

If you have vscode in your system.it'll open the vscode.

image

Gitbash

If you're in gitbash terminal you don't have to swicth on anything just stay on git bash terminal

git init
Enter fullscreen mode Exit fullscreen mode
Vscode

Ctrt + shift + g

Alt Text

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”

config

Gitbash
 git config --global user.email "MyEmail@gmail.com"
 git config --global user.name "My Name"
Enter fullscreen mode Exit fullscreen mode
Vscode
Ctrl + shift + `

//type same thing in the vscode terminal

git config --global user.email "MyEmail@Zoho.com"
git config --global user.name "My Name"
Enter fullscreen mode Exit fullscreen mode

Alt Text

Help

Common for both place,type in the terminal

# Quickly check available commands
$ git help

# Check all available commands
$ git help -a
Enter fullscreen mode Exit fullscreen mode

ignore files

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
Enter fullscreen mode Exit fullscreen mode
Vscode
First

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
Enter fullscreen mode Exit fullscreen mode

Alt Text

Second
Ctrl + shift + g

//right click on a file , and click on .add to ignore
Enter fullscreen mode Exit fullscreen mode

Alt Text

status

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
Enter fullscreen mode Exit fullscreen mode

Add

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
Enter fullscreen mode Exit fullscreen mode
VScode

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
Enter fullscreen mode Exit fullscreen mode

Alt Text

Branch

Gitbash
# 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
Enter fullscreen mode Exit fullscreen mode
Vscode

list existing branches & remotes

Alt Text

create a new branch

Ctrl + shift + p

Type branch , your are able to see the features that are listed in
Enter fullscreen mode Exit fullscreen mode

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.

image

Clone

Gitbash
# Clone repository 
 git clone https://github.com/adambard/learnxinyminutes-docs.git
Enter fullscreen mode Exit fullscreen mode
Vscode
ctrl + shift  + p
Type --> git clone ---> enter ---> paste the URL 

Enter fullscreen mode Exit fullscreen mode

Alt Text

Alt Text

commit

Stores the current contents of the index in a new “commit.” This commit contains the changes made and a message created by the user.

Gitbash
# commit with a message
git commit -m "Added multiplyNumbers() function to HelloWorld.c"
Enter fullscreen mode Exit fullscreen mode
VScode
ctrl + shift  + p
type a commit message

Enter fullscreen mode Exit fullscreen mode

Click on the 1st and then 2nd.

Alt Text

push

Push and merge changes from a branch to a remote & branch.

Gitbash

# 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
Enter fullscreen mode Exit fullscreen mode
VSCode

First way

Click on the circular button

Alt Text

Click on push

Alt Text

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

Top comments (0)