Git is a Distributed Version Control System (DVCS) created by Linus Torvalds in 2005, and has been maintained by Junio Harmano since then.
- Tracking codes
- Tracking who made the changes
- Coding collaboration
- Manage projects with repositories
- Clone a project to work on a localcopy
- Control and track changes with Staging and Commiting
- Branch and Merge to allow for working on different parts and versions of project
- Push local updates to the main project
- Pull latest version of the project to a local copy
- Initialize git on a folder, making it a repository
- Git now creates a hidden folder to keep track of changes in that folder
- When a file is changed, added or deleted, it is considered modified
- You select the modified files you want to stage
- The staged files are committed, which prompts git to store a parmanent snapshot of the files
- Git allows you to see the full history of every commit.
- You can revert back to any previous commit
- Git does not store a separate copy of every file in every commit but keeps track of changes made in each commit
mkdir my project
cd myproject
Here,
- mkdir -> makes a new directory
- cd -> changes the current working directory
git init
Now Git knows that it should watch the folder you initiated on i.e. it is now a Git Repository. Git creates a hidden folder to keep track of changes.
ls
ls -> will list all the files in the present working directory
git status
git status -> to see if the file is part of our repo or overall repo information
- Tracked - Files that Git knows about and are added to the repo
- Untracked - Files that are in your working directory, but not added to the repo
When you first add files to an empty repo, they are all untracked. To get Git to track them, you need to stage them or add them to the staging environment.
Staged files are the files that are ready to be committed to the repo you're working on.
Add only one file to the staging environment:
git add index.html
Add all files in the repo:
git add --all
or
git add -A
or
git add .
Adding commits keep track of our progress and changes as we work. Git considers each commit as "change point" or "save point". It is a point in the project you can go back to if you find a bug or want to make a change.
git commit -m "first release on Hello world"
Sometimes small changes in files are made. Staging feels like a waste of time. We can directly commit then. But it is not recommended.
git commit -a -m "Updated index.html with a new line"
To view the history of commits for a repository
git log
git status --short
- ?? - Untracked files
- A - Files added to stage
- M - Modifies files
- D - Deleted files
If you're having a trouble remembering commands or options for commands, git help is used.
git command -help
Example:
git commit -help
- see all the available options for the specific command
git help --all
- see all the possible commands that git holds
- will display a very long list of commands
- In Git, a branch is a new or separate version of the main repository.
- Branches will allow you to work on different parts of a project without impacting the main branch.
- When the work is complete, a branch can be merged with the main project.
- You can even switch between branches and work on different projects without them interfering with each other.
- Branching in Git is very lighweight & fast.
git branch hello-world
It creates a new branch.
git branch
To see the list of branches
git branch -r
shows remote branches only
git branch -a
To see all local and remote branches
git checkout hello-world
moves our current workspace from maste branch to the new branch
git checkout master
switched back to masterb branch
git checkout -b emergency-fix
Creates a new branch repo named emergency-fix and directly moves to that branch
git branch -d emergency-fix
git merge hello-world
GitHub is the largest host of source code in the world. Owned by Microsoft since 2018. GitHub makes tools that use Git.
It lets Git know who you are. It is important for version control system as each Git commit uses this information.
git config --global user.name "Cosmic Nomad"
git config --global user.email "cosmicnomad@gmail.com"
Here, global is used to set username & email for every repository on computer. To set for only current repo remove the global.
Hello world