All basics about git
Quick Learning, Developer from INDIA
Git is a distributed version control system (VCS) widely used by developers to manage and track changes in source code and collaborate on software development projects. It allows multiple developers to work on the same codebase simultaneously and facilitates efficient code management, collaboration, and version tracking.
Repository :
A repository (commonly referred to as repo) is a collection of source code. A repository has commits to the project or a set of references to the commits
Commits :
A commit logs a change or series of changes that you have made to a file in the repository. A commit has a unique SHA1 hash which is used to keep track of files changed in the past. A series of commits comprises the Git history.
Branches :
A branch is essentially a unique set of code changes with a unique name. Each repository can have one or more branches. The main branch the branch where all the changes eventually get merged into - is called the master.
Setting up Git
The easiest way of doing this is through the git config command. Specifically, we need to provide our name and email address because Git embeds this information into each commit we do. We can go ahead and add this information by typing:
git config --global user.name = "name"
git config --global user.email = "mail@gamil.com"
Different commands used in git :
initialization git : This command initializes a new Git repository in the existing directory if only no directory is specified otherwise it will initializes in mentioned directory.
$git init[directory_name]adding files : The
git addcommand is used to add changes made to files in working directory to the staging area in Git. The staging area is where we prepare changes before committing them to the repository.To add all files in single time
$git add .Adding files using their name
$git addfirst.txt second.txtabout commits : This represent a snapshot of the state of a Git repository at a specific point in time. Each commit records a set of changes made to the files in the repository since the last commit, along with metadata such as the author, commit message, and a unique identifier (hash).
Command for commit
$git commit -m "write your_message_here"about branch : The
git branchcommand in Git is used to manage branches in a repository. Branches are separate lines of development that allow programmer to work on features or fixes independent of the main codebase.Command for creating new branch
$git branchbranch_nameCommand for listing all existing branches
$git branchCommand for switching branch
$git checkoutbranch_nameCommand for deleting branch
$git branch -dbranch_name
about merge : The
git mergecommand in Git is used to combine changes from one branch into another. It's commonly used to integrate changes from a feature branch into the main branch (often called merging a feature branch into the main branch).First, switch to the branch into which you want to merge changes. This typically the branch you want to update with changes from another branch.Once you're on the target branch, use the
git mergecommand followed by the name of the branch you want to merge into the target branch.$git mergefeature-branchResolve Merge Conflicts : If there are conflicting changes between the branches being merged, Git will stop the merge process and prompt you to resolve the conflicts manually.
git status command : The
git statuscommand is used to display the current state of the Git repository. It provides information about which files have been modified, which files are staged for the next commit, and which files are untracked.Command for git status :
$git statusgit log command : The
git logcommand is used to display the commit history of a Git repository. It shows a chronological list of commits, along with relevant information such as the commit hash, author, date, and commit message.Command for git log :
$git loggit pull command : The
git pullcommand in Git is used to fetch the latest changes from a remote repository and integrate them into the current branch. It combines two operations: fetching changes from the remote repository and merging them into the current branch.Command for git pull :
$git pull[<remote>][<branch>]git push command : The
git pushcommand in Git is used to upload local repository content to a remote repository. It transfers commits from your local branch to the corresponding branch on the remote repository.Basic syntax for
git push:$git push[<remote>][<branch>]Command for git push :
$git push -u origin mastergit remote command : The
git remotecommand in Git is used to manage remote repositories. Remote repositories are copies of a Git repository that are hosted on another server, typically on a platform like GitHub, GitLab.To view the list of remote repositories associated with local repository :
$git remoteTo add a new remote repository :
$git remote add<remote-name> <remote-url>To rename a remote repository :
$git remote rename<old-name><new-name>To remove a remote repository from the list of remotes :
$git remote remove<remote-name>
git rebase command : Rebase essentially changing the base of our branch from one commit to another. This can be useful for incorporating changes from one branch into another, cleaning up your commit history, or resolving conflicts.
Commands for git rebase :
- $git checkoutfeature_branch- $git rebase main
