Site icon Satyam Arya

Important Git and GitHub Commands: A Guide for Developers

In the world of software development, Git and GitHub have become indispensable tools for managing and collaborating on projects. Git, a distributed version control system, enables developers to track changes in their codebase, while GitHub provides a platform for hosting and sharing repositories.

Git vs GitHub

GitGitHub
Distributed version control systemWeb-based hosting platform for Git repositories
Tracks changes in code repositoriesProvides a platform for collaboration and code sharing
Can be used offlineRequires an internet connection
Handles version control locallyAllows remote collaboration and synchronization
Works on all operating systemsAccessible through a web browser on any device
Command-line interfaceGraphical interface
Installed locally on the systemHosted on the web

Basic Commands

This command is to set your global username and email for Git

git config --global user.name "your name"
git config --global user.email "your email"

To initialize an empty git repository

git init

To create a local copy of a remote repository, you can use the git clone command followed by the repository’s URL:

git clone <repository_url>

After making modifications to your code, you need to stage the changes and commit them.

git add <file_name>  # Stage specific file
git add .            # Stage all changes
git commit -m "Commit message"

Shows the status of your git repository

git status

Shows the remote origin URL

git remote -v

Add remote origin URL

git remote add origin <repo_url>

Remove remote origin URL

git remote remove origin

View the commit history and logs

git log

To push your local changes to a remote repository

git push -u origin <branch_name>

To pull your remote changes to a local branch

git pull origin <branch_name>

It allows you to add an additional URL to an existing remote repository named “origin.”

git remote set-url --add origin <new_url>

It allows you to remove a URL from the list of URLs associated with an existing remote repository named “origin”

git remote set-url --delete origin

To restore files in your working directory to a previous state.

git restore

Learn how to push Local Repository to GitHub Using Git: Click here

Branching and Merging

Branches allow you to work on different features or bug fixes without affecting the main codebase.

git branch <branch_name>     # Create a new branch
git checkout <branch_name>   # Switch to the specified branch
git branch                   # Show all the branches in your git repository
git branch -d <branch_name>  # Removes a local branch from git
git push origin --delete <branch>  # Removes a remote branch from git

Once you have completed work on a branch, you can merge it into another branch using the following command:

git merge <branch_name>
git merge dev --squash

Git will apply all the changes from the “dev” branch to the current branch, but instead of creating a new commit for each change in the “dev” branch, it will combine all the changes into a single new commit.

Note: If you need to keep track of the individual commits from the “dev” branch, consider using a regular merge

Note:

If there are conflicts during the merge process, Git will prompt you to resolve them manually

Use a text editor to modify the conflicting files, then stage and commit the changes.

This command is used to show the differences between the changes in your working directory and the committed versions in your Git repository. It compares the current state of your files with the most recent commit.

git diff

If you have made changes to a file but decide you want to discard those changes and revert to the last committed version

git checkout -- <file_name>

This command help you to undo from staging area

git reset

This command help you to undo from staging area and the working directory and discards any changes made after the commit. Be cautious when using --hard as it permanently discards uncommitted changes.

git reset --hard

The git cherry-pick command enables you to pick specific commits from one branch and apply them to another branch.

git cherry-pick

Stashing

The git stash command in Git allows you to temporarily save changes that you have made in your working directory but are not ready to commit yet.

To stash your current changes

git stash save "Description of your changes"

To view a list of your stashes

git stash list

To apply stashed items

git stash apply stash@{<stash_number>}

To remove a specific stash from the stash list

git stash drop stash@{<stash_number>}

To remove all stashes

git stash clear

To undo an existing commit

git revert <commit_id>

It’s important to note that git revert does not modify the existing commits. Instead, it creates new commits to undo the changes.

Forking a Repository:

When you fork a repository, you duplicate the entire repository, including all its branches, commits, and files, into your own account. Once you have forked a repository, you can clone it to your local machine and make changes to it. You can create new branches, make modifications, and commit your changes

Forking is commonly used when you want to contribute to an open-source project or collaborate on a shared codebase. After making changes in your forked repository, you can submit a pull request to the original repository. This allows the repository’s owner to review your changes and potentially merge them into the original project.

Conclusion

In this blog, we explored some of the essential Git and GitHub commands that every developer should know. By mastering these commands, you can effectively manage your codebase, collaborate with others, and contribute to projects hosted on GitHub.

Exit mobile version