GIT Concepts
Collaboration Workflow:
- Clone a repository.
- Create a branch for your work.
- Make changes, commit them.
- Push your branch to the remote repository.
- Create a pull request (PR) for code review.
- Review, discuss, and make necessary changes.
- Merge the PR into the main branch.
Git is a popular distributed version control system used for tracking changes in source code during software development. It's an essential tool for collaboration and managing code repositories. This tutorial will cover the basics of Git, from installation to common commands and workflows.
Table of Contents:
• Installation
• Configuration
• Basic Git Commands
• git init
• git clone
• git add
• git commit
• git status
• Branching and Merging
• git branch
• git checkout
• git merge
• Remote Repositories
• git remote
• git fetch
• git pull
• git push
• Gitignore
• Undoing Changes
• git reset
• git revert
• Git History
• git log
• git blame
Let's start with the basics:
1. Installation: Download and install Git from the official website: https://git-scm.com/downloads.
2. Configuration: After installation, set up your name and email, which will be associated with your Git commits:
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
3. Basic Git Commands:
git init ( To create a new Git repository in your project folder )
git init ( To Initialize local git repository to your local machine )
git clone ( To clone an existing repository to your local machine )
git clone repository_url
git add . ( Stage changes for the next commit )
git add file_name
git commit ( Commit your staged changes with a message )
git commit -m "Commit message"
git status (Check the status of your working directory )
4. Branching and Merging:
git branch ( List all branches in your repository )
Git checkout -b newBranchName ( To Create & move to new Branch together )
# or if that branch is already exists use git checkout ( To Switch to a different branch )
git checkout branch_name (First, switch to the target branch git checkout target_branch)
# Then, merge the source branch into the target branch git merge source_branch
4.1 Git merge branchName ( Merge changes from one branch into another : will do fast forward / ORT auto merging process)
4.2 Git merge --squash branchName (Squash Merge )
4.3 Git Rebase
which can be looks like after REBASE, now the feature1 on rebase will come in front or head of the commit.Comparison :
which can be looks like after REBASE, now the feature1 on rebase will come in front or head of the commit.
4.4 Cherry-pick :
If we dont want the full merge and want to merge some specific changes made from other branch we can use cherry-pick
by first check all the log of the desired brach commit from where you want to pick the commits like here its NAV, so check by
git log nav --oneline
then check out to main branch or where ever you want to merge
If we dont want the full merge and want to merge some specific changes made from other branch we can use cherry-pick
by first check all the log of the desired brach commit from where you want to pick the commits like here its NAV, so check by
git log nav --oneline
then check out to main branch or where ever you want to merge
then
git cherry-pick 384c52 3a3bd61. ( here we are passing two commits we can pass one as well and what ever the sequence we pust it produced commits on merge in that sequence only like below:
git cherry-pick 384c52 3a3bd61. ( here we are passing two commits we can pass one as well and what ever the sequence we pust it produced commits on merge in that sequence only like below:
5. Remote Repositories:
git remote ( List remote repositories )
git remote -v
git fetch ( To Fetch changes from a remote repository)
git fetch remote_name
git pull ( Fetch and merge changes from a remote repository )
git pull remote_name branch_name
git push. (Push your local changes to a remote repository )
git push remote_name branch_name
6. Gitignore:
Create a .gitignore file to specify files or patterns you want Git to ignore. This is useful for excluding files like logs, temporary files, or build artifacts.
7. Undoing Changes:
Case -1 : Stage Changes
git reset ( Unstage changes ) for all together
git reset file_name
git revert ( Create a new commit that undoes a previous commit )
Case -2 : Committed Changes
git reset HEAD~1
Case -3 : Committed Many changes
git revert commit_hash
git reset commit_hash
git reset --hard commit_hash (To deleted permanently)
8. Git History:
git log ( View the commit history )
git log --all
git log --graph
git blame ( To Find out who last modified each line in a file )
git blame file_name
9. Git Best Practices:
• Commit frequently with clear messages.
• Use branches for different features or bug fixes.
• Keep your commits small and focused.
• Pull frequently to stay updated.
• Write meaningful commit messages.
10. Git Stash
This is used to SAVE the recent in-progress work under main branch or any other branch, sodo the git add . BUT w/o doing commit we can do git stash to save that work and which results it save and provide us a HASH code where its saved. And now we can switch to any other branch if needed w./o commit, becuase if we made any chnages on the current branch and when we try to shift to other branch git will not allow w/o commit, so there git stash works.
And when we come back to the current branch, we can continue the work by
git stash pop ( which will retrun the saved data )
or if we want to see all data in stash if we have many
git stash list ( this rarely needed )
This is used to SAVE the recent in-progress work under main branch or any other branch, sodo the git add . BUT w/o doing commit we can do git stash to save that work and which results it save and provide us a HASH code where its saved. And now we can switch to any other branch if needed w./o commit, becuase if we made any chnages on the current branch and when we try to shift to other branch git will not allow w/o commit, so there git stash works.
And when we come back to the current branch, we can continue the work by
git stash pop ( which will retrun the saved data )
or if we want to see all data in stash if we have many
git stash list ( this rarely needed )
Comments
Post a Comment