DEV Community

Cover image for Useful Git Commands.💻
Shubham Yadav
Shubham Yadav

Posted on • Edited on

Useful Git Commands.💻

Git is an example of a distributed version control system commonly used for open source and commercial software development.

Let's go :
initalize git in files/directories.

git init
Enter fullscreen mode Exit fullscreen mode

1.Clone the repository in your local system.

git clone https://github.com/<your-user-name>/<repo-name>
Enter fullscreen mode Exit fullscreen mode

2.Connect to remote

git remote add origin <url>
Enter fullscreen mode Exit fullscreen mode

3.To check the current status of the repository.

git status
Enter fullscreen mode Exit fullscreen mode

4.To add specific file to the staging area.

git add <file-name>
Enter fullscreen mode Exit fullscreen mode

5.To add all changed file to staging area.

git add .
Enter fullscreen mode Exit fullscreen mode

6.To unstage a certain file

git restore --stagged <filename>
Enter fullscreen mode Exit fullscreen mode

7.To see recent changes in the repository.

git diff
Enter fullscreen mode Exit fullscreen mode

8.To give a message and commit.

git commit -m "your-message"
Enter fullscreen mode Exit fullscreen mode

9.To see the commit history.

git log
Enter fullscreen mode Exit fullscreen mode

10.To see last specific commits (eg. Last 3 commits).

git log -3
Enter fullscreen mode Exit fullscreen mode

11.To discard the specific commit.

git revert <commit-token>
Enter fullscreen mode Exit fullscreen mode

12.To undo the commit and bring back changes to staging area.

git reset --soft HEAD <no._of_commit_to_revert>
Enter fullscreen mode Exit fullscreen mode

13.To show remote URLs

git remote -v
Enter fullscreen mode Exit fullscreen mode

14.To fetch the changes from origin to your local system.

git pull origin
Enter fullscreen mode Exit fullscreen mode

15.To create a branch named branch-name.

git branch <branch-name> 
Enter fullscreen mode Exit fullscreen mode

16.To make changes in the specific branch.

git checkout <branch-name>
Enter fullscreen mode Exit fullscreen mode

17.To merge sub branch to main branch.

git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode

18.To delete a specific branch.

git branch -d <branch-name> 
Enter fullscreen mode Exit fullscreen mode

19.To push the recent commits.

git push origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

20.Connect to remote

git remote add origin <url>
Enter fullscreen mode Exit fullscreen mode

21.To push the current branch and set the remote as upstream

git push --set-upstream origin master
Enter fullscreen mode Exit fullscreen mode

22.To rename branch name.

git branch -m <branch-name>
Enter fullscreen mode Exit fullscreen mode

23.change url for git remote repo

git remote set-url origin
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
bobbyiliev profile image
Bobby Iliev

Great list!

For anyone who is just getting started, I could also suggest this free opensource eBook here on how to get started with git and github:

GitHub logo bobbyiliev / introduction-to-git-and-github-ebook

Free Introduction to Git and GitHub eBook

💡 Introduction to Git and GitHub

This is an open-source introduction to Git and GitHub guide that will help you learn the basics of version control and start using Git for your SysOps, DevOps, and Dev projects. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can use Git to track your code changes and collaborate with other members of your team or open source maintainers.

The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Git, GitHub and version control in general.

🚀 Download

To download a copy of the ebook use one of the following links:

📘 Chapters

  • About the book
  • Introduction to Git
  • Version Control
  • Installing Git
  • Basic Shell Commands
  • Git Configuration
  • Introduction to GitHub
  • Initializing a Git project
  • Git Status
  • Git Add
  • Git…