Complete Guide To Git Commands

Version Control Part - I

ยท

4 min read

Complete Guide To Git Commands

Introduction

In the last article, we learnt about all the Linux commands. You can read the article here.

It will help you gain mastery over Linux commands and will also be the stepping stone for you to now learn about Git commands which will help you in version control, open source contributions and much more.

This article will take you from basic to intermediate level in Git commands. There will be 3 articles in the Version Control series.

Most Commonly Used Git Commands

git init
To initialise an empty git repository in a directory.

git init

git status
To see the status of tracked and untracked files and wether they are connected or not.

git status

git add
To add files to the main branch in order to commit them.

  • Add abc.txt to the stage for commiting
git add abc.txt
  • Add all files in the current directory to the stage for commiting
git add .

git commit
To commit the files. Use the -m switch to provide a commit message.

git commit -m "This is a commit message"

git log
To see history of all commits you've made.

git log

git reset
Deletes all commits above the specified Hash ID.

git reset hash_id

git stash
When you dont want to commit the changes but rather save them for future. It basically sends the files to the backstage to be worked upon.

git stash

git stash pop
To bring the stashed files back from backstage.

git stash pop

git stash clear
To permanently delete files which are stashed.

git stash clear

git remote add origin
To bring a repository into your terminal, origin being the name of the repository.

git remote add origin

git remote -v
Shows all URLs associated with the repository.

git remote -v

git push origin main
To push changes into the main branch of the origin repository.

git push origin main

git remote set-url origin
Change URL of the origin remote repository.

git remote set-url new_url

git clone
Clone a forked repository.

git clone repo_url

git remote add upstream
Add URL of the original repository which has been forked.

git remote add upstream

git branch
Create a new branch

git branch H1

git checkout
Bring the head to the specified branch.

git checkout -b H1

It is considered good practice if one branch opens only one pull request for one single feature.

git config --global user.email
Configures the global email associated with your Git identity.

git config --global user.email "Email here"

git config --global user.name
Configures the global username associated with your Git identity.

git config --global user.name "Git Username"

git rebase -i
To merge different commits into one. Here, you have to assign either 'pick' or 's' to the commit. The commits having 's' will be merged into the first upper commit that has 'pick'.

git rebase main

Bringing A Forked Project Even With The Main Project

Method I

  • git checkout main
    This switches your local repository to the main branch.

  • git fetch --all --prune
    This fetches all branches and tags from the remote repository and prunes deleted references.

  • git reset --hard upstream/main
    This sets your main branch to the same state as the upstream/main branch. upstream is typically a remote that points to the main repository you forked from.

  • git push origin main(main branch of local system)
    This pushes the changes to your fork's main branch on the remote repository.

git checkout main
git fetch --all --prune
git reset --hard upstream
git push origin main

Method II

  • git pull upstream main
    This command does two things. First, it fetches changes from the upstream/main branch and merges them into your current local main branch. It's equivalent to running git fetch upstream main followed by git merge upstream/main. This assumes you've already set up the upstream remote.

  • git push origin main
    This pushes the merged changes to your fork's main branch on the remote repository.

git pull upstream main
git push origin main

Conclusion

You can read other articles written by me through these links.

System Design Series
Introduction To Parallel Computing
Deep Dive Into Virtualization
Insights Into Distributed Computing

Cloud Computing Series
1. Cloud Service Models
2. Cloud Deployment Models
3. Cloud Security
4. Cloud Architecture
5. Cloud Storage
6. Networking In The Cloud
7. Cloud Cost Management
8. DevOps In Cloud & CI/CD
9. Serverless Computing
10. Container Orchestration
11. Cloud Migration
12. Cloud Monitoring & Management
13. Edge Computing In Cloud
14. Machine Learning In Cloud

Computer Networking Series
1. Computer Networking Fundamentals
2. OSI Model
3. TCP/IP Model : Application Layer
4. TCP/IP Model : Transport Layer
5. TCP/IP Model : Network Layer
6. TCP/IP Model : Data Link Layer

Version Control Series
1. Complete Guide to Git Commands
2. Create & Merge Pull Requests
3. Making Open Source Contributions

Linux
Complete Guide to Linux Commands

Thanks For Reading! ๐Ÿ’™
Garvit Singh

ย