How do you create a project, and how would you manage changes?
Cons :
Project can be lost, if your hard-disk is corrupted.
Repository
A directory where your project resides. It can be a local folder in your computer or a remote directory on a server.
There are 2 types of reposity :
1. Local Repository
2. Central Repository
Centralized VCS contain just one repository i.e central repository and each user gets to work on the same.
Centralized VCS contain just one repository i.e central repository and each user gets to work on the same.
Cons :
Git was created in 2005 by Linus Torvalds. (creator of Linux kernel)
?
2. Git stores snapshot of your project (not differences)
Snapshots Over Time
# to check the git version
git --version
# to check the git version
git --version
#configuration of global variables
git config --global user.name "Name"
git config --global user.email "username2010@gmail.com"
# to check the git version
git --version
#configuration of global variables
git config --global user.name "Name"
git config --global user.email "username2010@gmail.com"
git config --list
Types of Git Commands
Creating Repository
Types of Git Commands
Creating Repository
Making Changes
Types of Git Commands
Creating Repository
Making Changes
Parallel Development
Types of Git Commands
Creating Repository
Making Changes
Parallel Development
Syncing Repository
Let's create a git repository
git init
This will create a hidden .git folder.
That's where git stores everything.
git status
To track files & folders.
Tracking your files
Each file in your working directory can be in one of two states: tracked or untracked.
- Tracked files are files that Git knows about.
- Any file in your working directory that was not in your last snapshot and is not in your staging area is Untracked file
# for one specific file
git add <filename>
# for all files & folders
git add .
Tracking New files :
Stages the latest version of the file.
Moving files from staged area to the git folder is called a commit.
Making Commits, creates version/snapshots of your project.
git commit -m "Your commit message"
You can see all the commits/version of your git project.
git log
You can see all the commits/version of your git project.
git log
git log --oneline
Deleting a Git Project
rm -rf .git
Skipping the staging area.
git commit -a -m "Commit Message"
if you want to know exactly what you changed, not just which files were changed — you can use the
command
git diff
if you want to know exactly what you changed, not just which files were changed — you can use the
command
to see what you’ve staged that will go into your next commit
git diff
git diff --staged
Git Ignore
Moving & Removing Files
These command stage the changes automatically. You don't have to stage explicitly.
git rm filename.txt
git mv filename.txt newfilename.txt
Unstaging & Unmodifying Files
git restore --staged filename.txt
Unstaging & Unmodifying Files
git restore --staged filename.txt
This command takes your file to the last version/commit.
git checkout --filename.txt
Note:
This can be a dangerous command, as it discards all your current changes in the working directory
git checkout -f
(Rollback to the previous commit. losing all newly modified files.)
Setting Alias
git config --global alias.st status
Usuage:
git st
Setting Alias
git config --global alias.st status
git config --global alias.unstage 'restore --staged --'
Usuage:
Usuage:
git st
git unstage file.txt
What is Branching?
Why Branching is required?
98ca9
Commit 1
34ac2
Commit 2
f30ab
Commit 3
master
v1.0
HEAD
Creating new branches
Creating new branches
git branch
git branch -v
Get all the branches in your project.
Creating new branches
git branch branchname
git branch
git branch -v
Get all the branches in your project.
Creating a new branch.
98ca9
34ac2
f30ab
master
HEAD
develop
Moving between branches
git checkout branchname
Checkout an existing branch.
Moving between branches
git checkout branchname
Checkout an existing branch.
git checkout -b branchname
Creating and Checkout to a new branch.
98ca9
34ac2
f30ab
master
develop
HEAD
98ca9
34ac2
f30ab
master
develop
HEAD
98ca9
34ac2
f30ab
master
develop
HEAD
Making a commit on Branch
98ca9
34ac2
f30ab
master
87ab2
develop
HEAD
Making a commit on Branch
98ca9
34ac2
f30ab
master
87ab2
develop
HEAD
Making a commit on Branch
98ca9
34ac2
f30ab
master
87ab2
develop
HEAD
Making a commit on Branch
c2b9e
Deleting branches
git branch -d branchname
Deleting a merged branch.
Deleting branches
git branch -d branchname
Deleting a merged branch.
git branch -D branchname
Deleting a non-merged branch.
How to Merge Branches?
Why is it important to merge branches?
Merge Branches : Example
98ca9
34ac2
f30ab
master
develop
Merge Branches : Example
98ca9
34ac2
f30ab
master
98ca9
34ac2
f30ab
master
87ab2
develop
Merge Branches : Example
98ca9
34ac2
f30ab
master
98ca9
34ac2
f30ab
master
87ab2
develop
28bd1
impfix
Merge Branches : Example
98ca9
34ac2
f30ab
master
98ca9
34ac2
f30ab
master
87ab2
develop
28bd1
impfix
Merge Branches : Example
98ca9
34ac2
f30ab
98ca9
34ac2
f30ab
87ab2
28bd1
master
43ef8
develop
Merge Branches : Example
98ca9
34ac2
f30ab
98ca9
34ac2
f30ab
87ab2
28bd1
master
43ef8
develop
Snapshot to Merge into
Snapshot to Merge In
Common
Ancestor
Merge Branches : Example
98ca9
34ac2
f30ab
98ca9
f30ab
87ab2
28bd1
master
43ef8
develop
56ab3
Merge Conflicts
Changes in a file made by 2 different branches, trying to get merge results in a Merge Conflicts
Merge Conflicts
Changes in a file made by 2 different branches, trying to get merge results in a Merge Conflicts
Let's see how a Conflict look like.
Resolving Conflicts
As a programmer, you would have to resolve the merge conflicts manually.
Let's resolve a Merge Conflict
Merge Conflicts in a Project
git branch --merged
git branch --no-merged
To display merged & non merged branches only
Git Branching Workflow in Production
Branching Workflow
Long Running Branches
Topic Branches
Master
Development
Authentication
UI changes
Integrate Changes from one branch into another
Merging
Rebasing
With the rebase command, you can take all the changes that were committed on one branch and replay them on a different branch.
How Git performs rebase?
How Git performs rebase?
How Git performs rebase?
How Git performs rebase?
How Git performs rebase?
git merge experiment
No difference in the end product of the integration.
but rebasing makes for a cleaner history.
The log history looks like a linear history: it appears that all the work happened in series, even when it originally happened in parallel.
Often, you’ll do this to make sure your commits apply cleanly on a remote branch.
Key 🔑 Points on Rebasing
When you should not use Rebase 💀
Do not rebase commits that exist outside your repository and that people may have based work on.
When you rebase stuff, you’re abandoning existing commits and creating new ones that are similar but different.
Merge Vs. Rebase
One point of view is
Commit history is a record of what actually happened.
The opposing point is
the commit history is the story of how your project was made.
Git Vs. Github
Git is a version control system that lets you manage and keep track of your source code history.
GitHub is a cloud-based hosting service that lets you manage Git repositories.
If you want to work with open-source projects that use Git, then GitHub is designed to help you better manage them.
The git fetch command downloads commits, files, and refs from a remote repository into your local repo.
git fetch
The git fetch command downloads commits, files, and refs from a remote repository into your local repo.
git fetch
The git fetch command downloads commits, files, and refs from a remote repository into your local repo.
git merge
git pull
git pull
git pull = git fetch + git merge
git show commitid
Display the changes in a commit
Stashing
Stashing takes the dirty state of your working directory — i.e (your modified tracked files and staged changes), and saves it on a stack of unfinished changes that you can reapply at any time.
git stash
Stashing
Stashing takes the dirty state of your working directory — i.e (your modified tracked files and staged changes), and saves it on a stack of unfinished changes that you can reapply at any time.
git stash
git stash list
Stashing
Stashing takes the dirty state of your working directory — i.e (your modified tracked files and staged changes), and saves it on a stack of unfinished changes that you can reapply at any time.
git stash
git stash list
git stash apply stash@{0}
Stashing
Stashing takes the dirty state of your working directory — i.e (your modified tracked files and staged changes), and saves it on a stack of unfinished changes that you can reapply at any time.
git stash
git stash list
git stash apply stash@{0}
git stash drop stash@{0}
Cleaning your working directory
You may not want to stash some work or files in your working directory, but simply get rid of them; that’s what the git clean
command is for.
git clean -f -d
git commit --amend
Changing the message of the last commit
Changing the actual content of the last commit.
- Make changes in the working directory.
- Add files to the staging area.
- Run the same command.
Changing the actual content of the last commit.
- Make changes in the working directory.
- Add files to the staging area.
- Run the same command.
Note: don’t amend your last commit if you’ve already pushed it.
git checkout commitid
you can travel the time to go in the past to any specific commit.
git checkout commitid
you can travel the time to go in the past to any specific commit.
git switch -
git checkout commitid
you can travel the time to go in the past to any specific commit.
git switch -c branchname
git switch -
git revert commitid
you can undo the changes of any commit that you want.
git revert commitid
you can undo the changes of any commit that you want.
# explicitly commit the revert changes
git revert -n commitid
git revert --abort
git reset --soft commitid
git reset --hard commitid
very dangerous command.
- delete all the commits from the
given commit id onwards.
git rebase -i commitid
git push -f origin master
git reset --hard commitid
git push -f origin master
Alternative Method
git cherry-pick commitid
Pick the changes from a specific commit from any branch and merge with another branch.