How to use Git and GitHub

Git is an essential tool that every engineer should be familiar with, yet, in my experience, it’s rare to encounter individuals who are truly proficient in its use. Most people seem to rely on just the basics—commit, push, and pull—and little beyond that. In this discussion, I aim to delve deeper into the expansive world of Git and GitHub, exploring their more advanced features and capabilities.

*This post will be updated over time as I will keep adding new contents.

Clone from GitHub

Once you initialize Git, you can start using Git commands locally. Here’s how to clone your GitHub repository to your local environment.

git clone https://github.com/user/sample_repo.git

Navigate into the repository directory

cd sample_repo

Fetch all remote branches

git fetch --all

List all branches (local and remote)

git branch -a

Switch to the master branch

git checkout master

Commit and push

Stage all changes for the next commit

git add .

Commit the staged changes with a descriptive message

git commit -m "Update repository with new version of files"

Push the commit to the remote repository

git push origin master

or

git git push --set-upstream origin master

Merge with another branch

Switch to the new branch

git switch new_branch

Merge changes from master into new_branch

git merge master

Leave a Reply

Your email address will not be published. Required fields are marked *