-
Git is a software and GitHub is a service to host it online. It is a version control system which allows us to track changes in files and folders
-
Git creates checkpoints for the changes you make while doing.
-
It tracks the changes.
-
It was not the only services which were used. There were proprietary softwares there before git.
- Basics
- Daily Use
- Facing the problems
- Solving problems
- Learning more...
git --version
- 'ls' to check the list of files contain in that file
- How to config git for the first time
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
git config --list
- First check the status of file: It will tell you about the files status either about changes or about initalised for git or not
git status
- Initialise git
git init
-
Keep commits centric to one feature, one component or on fix. Focus on one thing.
-
Present or Past Message:
- Depends {Present Tense, Imperative}
- Give order to code base
- Don't Care
git log
git log --oneline
Git
has .gitignore
file which contains the some important files.
.env
: This is a files where contains API secrets, API Key, Payment Gateway Code
These are the files which you will never want to commit in your repository and never want to add in your GitHub
.gitignore
file contains files name which you don't want to show to others or push
in your repository. These files can be virtual environment files
or node-modules
or .env
etc.
.gitkeep
: Due to presence of this file, git
will not track empty folders.
- Use git add:
git add file-name
- Add a message to show what changes have been done:
git commit -m "Your message regarding changes done"
- git push
git push
- Isolation: You can work on a new feature or fix bugs without disrupting the main codebase.
- Collaboration: Teams can work on different branches, and changes can be integrated later.
- Experimentation: You can try out new ideas without the risk of breaking the main project.
- Feature branches: For working on new features.
- Bugfix branches: For fixing bugs.
- Hotfix branches: Used for quick fixes in the production code.
- Release branches: For preparing a new release, allowing you to focus on polishing the code.
Branches are one of the key components that make Git a powerful tool for version control.
- In Git, a branch is a lightweight, movable pointer to a commit. Branches allow you to work on different versions of a project simultaneously without affecting the main codebase. Here's a breakdown of how they work:
- Default Branch (main or master):
-
When you initialize a Git repository, Git creates a default branch, usually named
main
ormaster
. This branch serves as the base of your project and often contains the production-ready code. -
To see the branches:
git branch
- Creating New Branches:
- You can create new branches to work on features, bug fixes, or experiments. New branches are typically created from an existing branch, often from
main
, so that you can isolate your work and avoid disturbing the main codebase.
git branch feature-new-function
- To switching between branch, here checkout will not create a branch if not exist:
git checkout feature-new-function
- Or, to create and switch to the new branch in one step:
git checkout -b feature-new-function
- Or with switch:
git switch feature-new-function
- switch moves to the branch it has asked for, if that branch not exist even then it creates that branch and switches to that new branch
git switch -c feature-new-function
-c
works as to create here.
- Merging Branches:
- After completing work on a branch, you can merge the changes back into another branch, typically the main branch.
- Example of merging:
git checkout main
git merge feature-new-function
- Branch Management:
- You can view all branches in a repository by running:
git branch
- To delete a branch that is no longer needed:
git branch -d feature-new-function
git branch -m new_branch_name
git branch -m old_branch_name new_branch_name
The `git diff` command in Git shows the differences between various states of a repository. It is commonly used to compare changes between files, commits, branches, and more. Here's how git diff works and some common use cases:
- Show Unstaged Changes:
- If you want to see the changes in your working directory that haven't been staged (i.e., not added by git add), you can run:
git diff
2.Show Staged Changes:
- To see the changes that have been staged for the next commit (i.e., after using git add), but not yet committed, use:
git diff --staged
- Compare Two Branches:
- You can compare the changes between two branches. For example, to compare feature-branch with main, you can run:
git diff main feature-branch
In Git, git stash
is a useful command that temporarily saves your changes (both tracked and untracked files) without committing them, allowing you to switch branches or perform other tasks without losing your work. Once you're ready to return to those changes, you can "unstash" them and continue working where you left off.
- Stashing Your Changes: When you're in the middle of working on something and need to switch branches or do something else, you can stash your changes with:
git stash
- Naming the stash:
git stash save "Work in progress on X feature"
- Want to see the stash list:
git stash list
- Apply the stash:
git stash apply
Tags are a way to mark a specific point in your repository.
git tag <tag-name>
git tag -a <tag-name> -m "Release 1.0"
git tag
Git rebase is used to change the base of a branch
git reflog
shows history of your commits and allows you to see the changes that you have made to your repository over time.
git reflog
If you accidently deleted a branch or made changes that are no longer visible in history, you can often recover them using reflog. First find the reference to the branch or changes existed, and then reset your branch to that reference.
git reflog <commit-hash>
git reset --hard <commit-hash>