What Is Git?
Git is version control software, which means it manages changes to a project without overwriting any part of that project.
Why
use something like Git? Say you and a coworker are both updating pages
on the same website. You make your changes, save them, and upload them
back to the website. So far, so good. The problem comes when your
coworker is working on the same page as you at the same time. One of you
is about to have your work overwritten and erased.
Command Line:
The computer program we use to input Git commands. On a Mac, it’s
called Terminal. On a PC, it’s a non-native program that you download
when you download Git for the first time (we’ll do that in the next
section). In both cases, you type text-based commands, known as prompts,
into the screen, instead of using a mouse.
Repository:
A directory or storage space where your projects can live. Sometimes
GitHub users shorten this to “repo.” It can be local to a folder on your
computer, or it can be a storage space on GitHub or another online
host. You can keep code files, text files, image files, you name it,
inside a repository.
Version Control:
Basically, the purpose Git was designed to serve. When you have a
Microsoft Word file, you either overwrite every saved file with a new
save, or you save multiple versions. With Git, you don’t have to. It
keeps “snapshots” of every point in time in the project’s history, so
you can never lose or overwrite it.
Commit:
This is the command that gives Git its power. When you commit, you are
taking a “snapshot” of your repository at that point in time, giving you
a checkpoint to which you can reevaluate or restore your project to any
previous state.
Branch:
How do multiple people work on a project at the same time without Git
getting them confused? Usually, they “branch off” of the main project
with their own versions full of changes they themselves have made. After
they’re done, it’s time to “merge” that branch back with the “master,”
the main directory of the project.
Git-Specific Command
Since
Git was designed with a big project like Linux in mind, there are a lot
of Git commands. However, to use the basics of Git, you’ll only need to
know a few terms. They all begin the same way, with the word “git.”
1. git config :-
Short for “configure,” this is most useful when you’re setting up Git for the first time.
2. git help :-
Forgot a command? Type this into the command line to bring up the 21
most common git commands. You can also be more specific and type “git
help init” or another term to figure out how to use and configure a
specific git command.
3. git clone <url>
Clones a repository into a newly created directory, creates
remote-tracking branches for each branch in the cloned repository
(visible using git branch -r
), and creates and checks out an
initial branch that is forked from the cloned repository’s
currently active branch.
After the clone, a plain git fetch
without arguments will update
all the remote-tracking branches, and a git pull
without
arguments will in addition merge the remote master branch into the
current master branch, if any (this is untrue when "--single-branch"
is given; see below).
2. git help :-
Clones a repository into a newly created directory, creates remote-tracking branches for each branch in the cloned repository (visible using
git branch -r
), and creates and checks out an
initial branch that is forked from the cloned repository’s
currently active branch.After the clone, a plain
git fetch
without arguments will update
all the remote-tracking branches, and a git pull
without
arguments will in addition merge the remote master branch into the
current master branch, if any (this is untrue when "--single-branch"
is given; see below).
4. git status :-
Check the status of your repository. See which files are inside it,
which changes still need to be committed, and which branch of the
repository you’re currently working on.
5. git add :- This does not
add new files to your repository. Instead, it brings new files to Git’s
attention. After you add files, they’re included in Git’s “snapshots”
of the repository.
i) git add <file name>
This command is used to add a single file name into repository, that file will be available to commit, if you are not add file to repository you can't able to commit.
ii) git add -u
This command is used to add all modified files to repository.
iii) git add .
This command is used to add all modified and newly added files to repository.
5. git commit -m "commit message"
Git’s
most important command. After you make any sort of change, you input
this in order to take a “snapshot” of the repository. Usually it goes.
6. git push origin <branch name>
If
you’re working on your local computer, and want your commits to be
visible online on GitHub as well, you “push” the changes up to GitHub
with this command.
7. git branch :-
Working
with multiple collaborators and want to make changes on your own? This
command will let you build a new branch, or timeline of commits, of
changes and file additions that are completely your own. Your title goes
after the command. If you wanted a new branch called “cats,” you’d type git branch cats.
8. git checkout <branch name> :-
Switch branches or restore working tree files.
Ex: If you are current working directory master, if you want to switch from master to sandbox.
git checkout sandbox
If you want to restore of current modified files to previous stage use the below command
git checkout <file name>
5. git add :- This does not add new files to your repository. Instead, it brings new files to Git’s attention. After you add files, they’re included in Git’s “snapshots” of the repository.
6. git push origin <branch name>
If you’re working on your local computer, and want your commits to be visible online on GitHub as well, you “push” the changes up to GitHub with this command.
7. git branch :-
8. git checkout <branch name> :-
Switch branches or restore working tree files.
Ex: If you are current working directory master, if you want to switch from master to sandbox.
git checkout sandbox
If you want to restore of current modified files to previous stage use the below command
git checkout <file name>
9. git pull origin <branch name>
If
you’re working on your local computer and want the most up-to-date
version of your repository to work with, you “pull” the changes down
from GitHub with this command.
10. git merge <branch name>
When
you’re done working on a branch, you can merge your changes back to the
master branch, which is visible to all collaborators.
Ex: you are currently working on feature branch i.e your branch xyz name.
would take all the changes you made to the “xyz” branch and add them to the master.
git merge xyz
11. How to delete a branch from local repository ?
git branch -D <branch name>
12. How to take particular commit from one branch to another branch ?
git cherry-pick <commit-sha>
commit-sha - take from original branch which commit you want.
Below is the image where to take commit sha
(deff1f5b3a86bea8128e8eb18f689bc74ba0e213)
13. How to save your local changes temporarily in your local repository ?
git stash
When ever you want to pull out your changes we can use the below command
git stash apply
Above command will pullout your last stashed files.
git stash list
Above command will show all the local stashed commits.
If you want to pullout specific local commit we can use below command
git stash apply stash@{0}
stash@{0} - this is the latest local stashed commit.
{0} - here we can also pass specific stash number to pop out previous changes like below. Ex :- git stash apply stash@{1}
14. If you want to fix up your latest commit, you can undo the commit, and
unstage the files in it, by doing:
git reset HEAD~1
Below is the command we can use to reset N number of last commits.
git reset HEAD~N
If you want to get rid of your latest commit, and do not want to keep the code
changes, you can do a "hard" reset.
git reset --hard HEAD~1
15. To remove untracked files from git use below command
git clean -fd
No comments:
Post a Comment