DEV Community

Sudha Subramaniam
Sudha Subramaniam

Posted on • Updated on

Git and Github

Git is a software that allows you to keep track of changes that you made to a project overtime. Git works by recording changes you make to a project, storing those changes and allowing to reference them as need.

All git commands follow the pattern git and in order to use git for a project, a project must first be initialized using the git init command in the project's root directory.

Git Project Workflow
A Git project has three parts:

A Working Directory: where files are created, edited, deleted, and organized
A Staging Area: where changes that are made to the working directory are listed
A Repository: where Git permanently stores changes as different versions of the project
The Git workflow consists of editing files in the working directory, adding files to the staging area, and saving changes to a Git repository.

Image description

Git commands

git init : The command sets up all the tools Git needs to begin tracking changes made to the project. It creates a new Git repository
git status: This command helps to check the status of changes made to the project
git add : In order for git to start tracking the project, it needs to be added in the staging area. We can add a file to staging area with git add filename
git diff : Using this command we can check the differences between the working directory and the staging area.git diff filename
git commit : Git commit is the last step in git workflow. Commits permanently stores changes from staging area inside the repository
git log : Often with Git, you’ll need to refer back to an earlier version of a project. Commits are stored chronologically in the repository and can be viewed with: git log (shows a list of all previous commits)
git show HEAD: In Git, the commit you are currently on is known as the HEAD commit. In many cases, the most recently made commit is the HEAD commit.

An overview of a typical Git project workflow:

Create a Git repository: Initialize a Git repository on your local machine or create one on a Git hosting service such as GitHub or GitLab.

Create a branch: Create a new branch for each new feature or bug fix you want to work on. This allows you to work on changes without affecting the main codebase.

Make changes: Make changes to the files in your branch using your preferred text editor or integrated development environment (IDE).

Stage changes: Use the Git command git add to stage the changes you've made. This tells Git which files you want to include in the next commit.

Commit changes: Use the Git command git commit to create a new commit with a descriptive message summarizing the changes you've made. Commits should be small and focused, addressing one specific feature or bug fix at a time.

Push changes: Use the Git command git push to push your committed changes to the remote repository on GitHub or GitLab.

Create a pull request: If you're working with a team, create a pull request to merge your changes into the main codebase. This allows other team members to review your changes before they are merged.

Resolve conflicts: If there are conflicts between your changes and the main codebase, resolve them by editing the affected files and committing the changes.

Merge changes: Once your changes have been approved and any conflicts have been resolved, merge your branch into the main codebase.

Update local repository: Use the Git command git pull to update your local repository with any changes that have been made by other team members.

Top comments (0)