Basic Git Commands
What is git
Git is a free, open-source distributed version control system. It keeps track of projects and files as they change over time with the help of different contributors.
Git helps keep track of changes made to a code. If at any point during coding you hit a fatal error and don’t know what’s causing it, Git allows you to revert back to a stable state. It also helps you see what changes have been made to the code over time.
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 (i.e., heads).
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
Now that you have Git installed, you need to do a few things so that the commit messages that will be generated for you will contain your correct information.
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:
example :
git config --global user.name "your name"git config --global user.email "your email id"
1. git --version
The
git --versioncommand is used to know version of git we are using and to check weather git is installed or notexample :
git --version
2. git init
The
git initcommand is used to initialize the git in our current working directoryexample :
git init
3. git status
The
git statuscommand is used in Git, the version control system, to display the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git.example :
git status
4. git add
The
git add <filename>command It's used to add changes in the working directory to the staging areaexample :
git add README.md
5. git rm --cached <filename>
The
git rm --cached <filename>command is used to remove the files from the staging areaexample :
git rm --cached README.md
6. git checkout -b <name>
The
git checkout -b <name>is used to create a branch followed by the nameexample :
git checkout -b feature2
7. git checkout <name>
The
git checkout <name>is used to switch from current directory to<name>directoryexample :
git checkout master
8. git commit
The
git commitcommand in Git is used to save your changes to the local repository. A commit is a record of what files you have changed since the last time you made a commit. Essentially, you are taking a "snapshot" of your repository at a specific point in time, which allows you to revisit versions later if needed.example :
git commit -m "initial message"
9. git merge
The
git mergecommand is used in Git to combine multiple sequences of commits into one unified history.git mergeis used to integrate changes from one branch into another, such as merging a feature branch into the main branch.example :
git merge feature
10. git remote
The
git remotecommand in Git is used to manage a set of tracked repositories. It allows you to view and set up remote repositories, which are versions of your project that are hosted on the internetexample :
git remote add originhttps://github.com/user/repo.gitgit remote remove origin
11. git push
The
git pushcommand in Git is used to upload local repository content to a remote repository. This command sends commits made on your local branch to a remote repository, allowing other collaborators to see them.example :
git push origin master
12. git pull
The
git pullcommand in Git is used to fetch and integrate changes from a remote repository into your local branchexample :
git pull
13. git rebase
The
git rebasecommand in Git is a powerful feature used to alter the base of your branch from one commit to another, making it as if your branch was created from a different commitexample :
git checkout feature-branchgit rebase main