What is Git?
Git is a Version Control System. Git helps us manage our project files. One of the primary things that git does and also the primary reason it exists is to keep track of the entire history of things that you are working on.
What is Github?
GitHub is a web-based interface that uses Git, the open-source version control software that lets multiple people make separate changes to web pages at the same time. It allows for real-time collaboration, and GitHub encourages teams to work together to build and edit their site content.
By understanding these must-know commands you will become an even better web developer.
Windows
Download and install Git for Windows . Once installed, you’ll be able to use Git from the command prompt or PowerShell.
Basic setup If you’d like, you can go ahead and save your git username and email so that you won’t have to enter them in again for future git commands.
git config --global user.name "User Name"
git config --global user.email "email"
git config --list
Run git config --list, showing system, global, and (if inside a repository) local configs
Before starting to understand the git command you should know this basic command of the terminal. I am using the git bash terminal
I made one folder called mark6 inside that folder I will create three file
Hp@LAPTOP-5QQC5VDH MINGW64 /h/Neoog/neoglevelZero/Mark6
$ touch index.html script.js style.css
touch command used to create a file.
Hp@LAPTOP-5QQC5VDH MINGW64 /h/Neoog/neoglevelZero/Mark6
$ ls
index.html script.js style.css
ls The Linux ls command allows you to view a list of the files and folders in a given directory. You can also use this command to display details of a file.
Hp@LAPTOP-5QQC5VDH MINGW64 /h/Neoog/neoglevelZero/Mark6
$ start .
start . this command used to show directory GUI actually shows the folder. open . for mac
like this 👇
ls samplefolder // it will show all files inside and folders inside samplefolder folder
start samplefolder // you can also open this folder
clear // used for clear screen of the terminal
PWD //shows the current location path
cd // go inside the folder
cd .. // back to thre folder
touch // used for making file
mkdir // t used to make a folder
rm // for delete file
rm -rf // used to delete the folder
ls -al // shows the hidden file or / _a
Now all the basic Linux commands we understood. Now time to move to the git command
git init
This will create a hidden .git folder inside your current folder — this is the "repository" (or repo) where git stores all of its internal tracking data. Any changes you make to any files within the original folder will now be possible to track.
git add .
This is the first command that you'll run after making some changes to the project files.
git commit -m "all done"
//or
git commit -am "your commit message"
git commit -am adds the changed files into a commit with a commit message as stated inside the inverted commas(in the hading). Using the option -am allows you to add and create a message for the commit in one command.
The -a flag is used in git to add all the files to your commit and then you'll have to run another command where you write your commit message. The m flag is used for connecting a commit message to your commit for example `git commit -m "your message".
its combination of git add . + git commit -m "all done" = git commit -am "your commit message"
git status
This will print some basic information, such as which files have recently been modified.
You should check your status anytime you’re confused. Git will print additional information depending on what’s currently going on in order to help you out.
git log
Git log is a utility tool to review and read a history of everything that happens to a repository.
git log --oneline
By default, the git log statement returns a full log entry for each commit made to a repository. You can retrieve a list of commit IDs and their associated commit messages using the – -oneline flag. We can see the commit IDs and the first line of the messages associated with a commit
How to edit commit message in git ?
if we want to edit the last commit or previous commit then you need this command
git commit --amend
then it will open vim editor
then insert I button
then edit commit
now we need to exits
type one time escape key then :wq
done.
oR
if you don't want to used vim. for that, you need to open git bash or terminal and type
git config --global core.editor "code --wait"
next
git commit --amend
it will open vs code edit you commit then close the file. check commits edited or not for that type on terminal git log
About branches in git👇
git branch
this command is used for, In which branch you are now and it shows the list of branches, views all branch
git remote -v
git clone create a clone, or copy of the target repository.
git clone <repo.id>
git branch <branch-nmae> // this command used for create a new branch
git switch <branch-nmae> // for switch the branch
git checkout <branch-nmae> === git switch <branch-nmae> // it's same
git switch -c <branch-nmae> // it used to create you branch and switch
git checkout -b <branch-nmae> // it used to create you branch and switch
// before the switch, you should commit your work. otherwise, you will lose your work.
git branch -D <branch-nmae> // it used to delete your branch
git branch -m <branch-new-name> // it will change your branch name
git merge <branch-nmae> // it used to create merge branch just we need to switch branch
git diff <branch-nmae>..<branch-nmae> // it will shows you different between two branches