Information Technology

Basic Git Commands You Need to Know

Basic Git Commands You Need to Know

Basic Git Commands You Need to Know

Embarking on your coding journey? Git, the backbone of version control, is your best ally. But where to begin? In this blog, we'll unravel the mystery behind Git and introduce you to the fundamental commands every developer needs to know. From initializing a repository to navigating branches and merging changes, we'll equip you with the essential tools to manage your projects efficiently. Let's dive in and unleash the power of Git together!

1. Setup git credentials for git-bash globally.

$ git config --global user.name "<first_name> <last_name>"

$ git config --global user.email "<email_id>"

2. Initialize an existing directory as empty Git repository.

$ git init

3. Clones an existing Github repository to your local directory.

$ git clone <repository_url>

4. After using the 'git init' command, link the local repository to an empty Github repository.

$ git remote add origin <remote_url>

5. Creates new branch.

$ git branch <branch_name>

6. Creates branch from existing branch.

$ git branch <branch_name> <existing_branch_name>

7. Creates a new branch and switch to that branch using one command only.

$ git checkout -b <branch_name>

8. Deletes the specified branch.

$ git branch -d <branch_name>

9. Checks the status of the working directory and the staging area for next commit.

$ git status

10. Adds file(s) to the staging area.

$ git add <file1> <file2>

11. Adds all files to the staging area.

$ git add .

12. Commits your staged content as a new commit snapshot.

$ git commit -m "<description_message>"

13. Push local commits to a Github repository. 

$ git push origin <branch_name>

14. Fetch and integrate changes from a Github repository into the current local working branch.

$ git pull origin <branch_name>

15. Synchronize your local repository with the Github repository.

$ git fetch <remote_name>

16. Merge specified branch into current branch.

$ git merge <branch_to_merge>

17. Retains changes in the working directory but removes commits from the branch history.

$ git reset

18. Not only removes commits but also discards changes in the working directory and staging area, resetting them to match the specified commit completely.

$ git reset --hard

19. Reverts the changes upto specific commit.

$ git revert <commit>

20. Put current changes from your working directory into stash for later use.

$ git stash

21. Apply stored stashed content into working directory and clear the stash.

$ git stash pop

22. Deletes a specific stash from all your previous stashes.

$ git stash drop

23. View the commit history of the repository.

$ git log

24. View the remote repositories associated with your local repository.

$ git remote -v


Remember:
This is basic introduction of Git commands. You need to practise above commands with the live environment or with the live project, for better understanding and additional settings.