Let's start exploring GitHub together ..
Points to remember:
It is a open-source version control
tool that manages different version
of your codes/files, create a history of changes
and tracks the changes
. Centralised and Distributed Version Control System (DVCS) are most common version control system existing in community.
- All the files, project files version history should be saved in central server
- To make changes you need to work locally once done push it to central server
- You will need access to central server
- You’ll only checkout the files you needed to modify therefore you won’t have complete version of code locally
- If something happens to central server, you may not be able to connect to your files
- Softwares such as
Subversion
uses centralised VCS - Slow(er), network dependant, long urls, manual merges. To be used with care
- You’ll always have access to copy of entire project and its version history locally
- To make changes you can work both locally or remotely github, if you choose to work locally you will just need to connect to server for a few mins to sync your files to remote server
- You can access your files from anywhere across the globe
- Softwares such as
GitHub
,Mercurial
etc. uses disttributed VCS - Fast(er), network independent, often automatic merges. To be used early and often
- Information is a set of snapshots, every committed change, Git takes another Snapshot
- Snapshots are
full version of file
that has changednot just changes to it
- Snapshots are
- If we think about Subversion information is set of files plus
deltas of changes over time
– does not have full version
Install git using terminal or command prompt
Linux (Debian)>
sudo apt-get install git
Linux (Fedora)>
sudo yum install git
MacOS>
brew install git
Windows user can use this link>
[Using this link](https://git-scm.com/downloads)
Once you complete the download, you can verify its installation using>
git -- version or git version
Git is primarily a command line
tool, but it also provides GUI based feature. If you would like to have GUI feature
you can install
GitHub Desktop in your local PC or laptop
.name <name>
: Setup your name.
.email <email>
: Setup your email.
Set your name and email that will be attached to your commits
Examples:
git config --global user.name "your-git-user-name"
git config --global user.email "your-git-email"
I'm sure most of us like alias, if so why not setting some alias as well to make our life even easier 😄 Alias is a short form of frequently used commands
Some Examples, you can set it as you would prefer, (not mandatory)
git config --global alias.s status
git config --global alias.c commit
git config --global alias.br branch
git config --global alias.co checkout
git config --global alias.df diff
git config --global alias.lg 'log -p'
Repository
: It is the collection of all the files and history of those files, consist all your commits, often shortened as repo
Repo can stay in local machine or remote server github, the act of copying remote repo to local machine is called cloning
Commit
: The act of creating snapshot; a project is made up of lots of commits, contains three piece of information
- How the files have changed from previous version
- A reference to commit that came before
- A commit id # also called hash code
Pull
: Suppose you make some changes on your repo remotely that change will not be synced in your local machine, the process of downloading the remote commit to local machine is called pulling, we use “git pull command” conversely
Push
: When you made changes locally that will not be reflected in remote server until you push them, we use “git push” command
.git directory
(History) : Place where the metadata and object database of the project is stored, this is what's copied when you clone a repository.
-
Working Directory
: Place where all the work takes place create, edit, delete, organize and save files, this is a version of the project that is checked out and placed on disk this is a file in the .git directory, that stores information about what will go into your next commit -
Staging Area
: Revised or modified files are staged, we add snapshots to the files in the staging area -
Git Repository
: Now we do commit that stores snapshot to your git repo and will be saved as version history
Start tracking a project in Git, this command creates the .git directory
Show the status of modified files in the working directory.
These can be untracked, unstaged or staged for commit.
- go into new folder
- do git init
- do git status
- no files
- touch readme.md
- git status - see untracked files
This command save a snapshot of the staging area to the history as a commit
Points to remember:
An important thing to note - is that every time a file changes, if you want to include that change with the next commit - you need to tell git to re-add it, as it is adding a snapshot of the file at the time the command is run.
-- edit file -- git add -- edit file again -- need to add it again
We are humans, sometimes its normal to make mistakes i.e. to say NOT every commit is perfect
Git provides some ways to work with your history using reset, revert, and checkout
Point to remember:
Git revert is one of the safer ways of to undo a changes, as it does not rewrite the history of your project. Git tries to figure out how to undo a change, and create a new commit for that change
This command copy files from the latest commit to staging area
Effectively undoes git add <files>
by replacing files in the staging area by those last committed
Omit <files>
to unstage everything.
Points to remember:
git revert
does not move the project back to a previous state but git reset
does. Git revert also allows you to revert changes at any point in history, where git reset can only work backwards from the current commit
Git reset can be considered a ' very dangerous
' command also, as it is possible to lose work as you are unable to recover your original work
This command copy files from the staging area to the working directory.
Omit <files>
to throw away all local changes
This command remove files from the working directory and the staging area
--cached
: Remove files from the staging area only and keep them in the working directory.
This command track the movement (renaming) of files
It essentially moves the file on the filesystem, git rm
s and then git add
s the moved file.
This command copies an existing Git repository to your local machine and checks out a working copy of the latest version of the default branch
This commad shows the exact lines added and removed in the working directory.
--cached | --staged
: Compare your staged changes to those of your last commit.
This command helps you to browse and inspect the evolution of the project.
Points to remember:
Branching
is done to develop ceratin features (module) isolated from other. When you create a repo master branch is the default
branch. We should use other branches for development and merge them back to the master after completion
Suppose we want to create some new branch with name Test-A Test-B to develop some different functionality with our existing code
git checkout -b Test-A
git checkout master (switch back to master branch)
git branch -d Test-A (deletes the Test-A branch)
git merge <Test-A> (merges the changes made in Test-A feature to master branch)
git merge <Test-B> (merges the changes made in Test-B feature to master branch)
git diff <master> <Test-A> (shows difference between master and Test-A branch)
git diff <master> <Test-B> (shows difference between master and Test-B branch)
Created with ❤️ by Akshay Anand