You can use this article to revise and copy-paste the git commands quickly if there’s a need. This article is For the beginner, By a beginner, and to the beginner.
What is Git
Git is the coolest thing when it comes to tracking changes in any set of files, coordinating work among programmers during software development. Developers rely on Git for version control.
What is Version Control?
Version control, also known as source control, is the practice of tracking and managing changes to software code. It records changes to a file or set of files over time so that you can recall specific versions later.
Git is a Distributed Version Control System. So Git does not necessarily rely on a central server to store all the versions of a project’s files. Instead, every user “clones” a copy of a repository (a collection of files) and has the full history of the project on their own hard drive. This clone has all of the metadata of the original alongside the original, which is stored on a self-hosted server or a third-party hosting service like GitHub.
Few points to remember about Git :
- Maintaining different versions of code is called version controlling.
- Version control stores complete change history like creation, deletion, addition as well as modifications.
- Git facilitates concurrent contributions using branches.
- It enables Traceability i.e. tracing bugs using bug tracking software such as Jira.
- GitHub allows us to host repositories online. So the difference between Git and GitHub is that Git is a version control system that lets you manage and keep track of your source code history whereas GitHub is a cloud-based hosting service that lets you manage Git repositories.
- Repository: Entire collection of files and folders associated with a project.
- Commit: Each snapshot of the code is called a commit.
- Having a full local history makes Git fast.
To understand the Git commands better, let’s first understand the Git workflow.
There are four fundamental elements in the Git Workflow :
- Working Directory
- Staging Area
- Local Repository
- Remote Repository
Git has three main states (in the working directory) that your files can reside in :
- modified means that you have changed the file but have not committed it to your local repository yet.
- staged means that you have marked a modified file in its current version to go into your next commit snapshot.
- committed means that the data is safely stored in your local repository.
Let’s see some of the most used Git commands :
git initcommand creates a new Git repository in the current directory.
git checkout -b <branch-name>is used to check out a new branch.
git statuscommand displays the state of the working directory and the staging area.
git add .command adds all the newly created and modified files in the working directory to the staging area.
git add *command adds all the newly created, modified, and deleted files in the working directory to the staging area.
git add -ucommand adds only the modified files in the working directory to the staging area.
git commit -a -m "Message here"is a command used to add all files that are staged to the local repository with a message.
-aflag is used when there is more than one file to commit.
git push origin <branch-name>is a command used to add all committed files in the local repository to the remote repository.
git logis a command used to display all the commits.
git fetchis a command used to get files from the remote repository to the local repository but not into the working directory.
git mergeis a command used to get the files from the local repository into the working directory.
git pullis a command used to get files from the remote repository directly into the working directory. It is equivalent to a
git fetchand a
git rm <file-name>is a command used to remove files from the working directory and the staging area.
gitignoreis a file that contains names of such files which we don’t want to push to the remote repository.
Steps to add files to Git :
pwdto see the present working directory.
git status: to check the changed files. (Shown in red)
- Add the file to the staging area
git add .
git status: to check the staged files. (Shown in green)
- Commit the files in the staging area so that they can be added to the local repository
git commit -am "New files"
- Push the file to the remote repository
git push origin <branch-name>
That’ll get you going on Git.
I hope you learned something! Thank you for reading.
Click the 👏 to show your support and share it with other fellow Medium users.