Github Commands you should know

Chitranjan Gupta
CodeX
Published in
4 min readAug 9, 2024

All basic Commands to control your projects on GitHub.

Canva.com

Version Control Systems?

Version Control Systems (VCS) are essential tools for managing changes to source code over time. They allow developers to track revisions, collaborate with others, and revert to previous versions when necessary. Git is a popular distributed VCS that enables seamless coordination among team members, making it indispensable for modern software development.

Beyond software development, Git and other VCS tools are also very useful in data science and analytics. They help data scientists keep track of data preprocessing scripts, model iterations, and analysis results. By using VCS, teams can ensure reproducibility and collaborate more effectively on projects, which is vital in the rapidly evolving field of data science.

# Installing Git and GitHub

1. Download and Install Git:

  • Visit the official Git website and download the installer for your operating system.
  • Follow the installation instructions to set up Git on your machine.

2. Setting Up GitHub:

  • Create a GitHub account at GitHub.
  • Install GitHub Desktop or configure Git to connect with your GitHub account using the command line.

# Git Basic Commands

1. Initializing a Repository:

#Create a new repository by navigating to your project directory and running:

git init

This command initializes a new Git repository in your current directory. It creates a .git directory that stores all the information about your repository, including configuration, branches, and history.

Example:

mkdir my_project
cd my_project
git init

This sequence of commands creates a new directory called my_project, navigates into it, and initializes a new Git repository there.

2. Cloning a Repository:

#To make a local copy of an existing repository, use:

git clone <repository-url>

This command copies a repository from a remote server (like GitHub) to your local machine.

Example:

git clone https://github.com/username/repo.git

This command clones the repository located at the specified URL into a new directory named repo on your local machine.

3. Staging and Committing Changes:

#Track changes to your files by adding them to the staging area:

git add <filename>
#Save your changes to the repository with a commit message:

git commit -m "Your commit message"

The git add command stages changes (new or modified files) to be included in the next commit, while git commit records those changes in the repository's history with a descriptive message.

Example:

echo "Hello, World!" > hello.txt
git add hello.txt
git commit -m "Add hello.txt with greeting"

This creates a new file hello.txt, stages it, and commits it with a message.

4. Viewing Commit History:

#To see a log of all commits made to the repository, use:

git log

This command shows a list of all the commits in the repository, including the commit hash, author, date, and message.

5. Branching and Merging:

#Create a new branch for developing features independently:
git branch <branch-name> #'master' or 'main' or custom-branch

#Switch to the new branch:
git checkout <branch-name>

#Merge changes from another branch into your current branch:
git merge <branch-name>

Branching allows you to work on different features or fixes simultaneously without affecting the main codebase. Merging integrates these changes back into the main branch.

Example:

git branch new-feature
git checkout new-feature
# Make changes and commit them
git checkout main
git merge new-feature

This sequence of commands creates a new branch called new-feature, switches to it, makes some changes (not shown), and then merges those changes back into the main branch.

6. Handling Merge Conflicts:

If Git cannot automatically merge changes, a conflict occurs. Resolve it by manually editing the conflicting files and then completing the merge:

git add <filename>
git commit -m "Resolved merge conflict"

Merge conflicts happen when changes in different branches conflict with each other. You need to manually resolve these conflicts and then commit the resolved changes.

Example:

# During a merge, a conflict occurs
# Edit the conflicting file(s) to resolve the conflict
git add conflict-file.txt
git commit -m "Resolved merge conflict in conflict-file.txt"

This resolves a merge conflict by manually editing the conflicting file and then staging and committing the resolved file.

# Summary

Mastering Git basics is essential for any developer or data scientist looking to work efficiently and collaboratively. From initializing repositories to handling merge conflicts, understanding these fundamental commands ensures smooth project management and version control.

If you found this guide helpful, please like👏 and share it with others who might benefit from learning about Git.

Happy to welcome new publications! Please reach out to me!

Follow Chitranjan Gupta for more such content. Happy coding!

More Related Articles

--

--

Chitranjan Gupta
CodeX
Writer for

Hi there! I'm a data science practitioner. I talk about data analytics, visualizations, statistics, programming, machine learning, and a lot more.