DEV Community

Cover image for Useful Git Commands
Utkarsh Mishra
Utkarsh Mishra

Posted on

Useful Git Commands

Hi there! I have made this blog because I forgot some of the git commands every time while using git or GitHub. So I am going to put all the useful git command in this blog so that anyone who is going to get started with open-source get some help and whenever I need I can quickly find those at one place.

Git is so powerful. There are a lot of really cool features. Some other basic functions you'll want to learn is how to git pull, git clone, git merge, git rebase, git stash, git checkout, and git branch. Those are the ones that are needed for base level proficiency in git.

Git Basics

1. To Create empty git repo in specified directory. Run with no arguments

git init
Enter fullscreen mode Exit fullscreen mode

2. For cloning the repo onto your local machine

git clone <repo--HTTP or SSH>
Enter fullscreen mode Exit fullscreen mode

3. To define author name to be used for all commits in current repo

git config user.name <name>
Enter fullscreen mode Exit fullscreen mode

4. To stage all changes in <directory> or <file> for the next commit

git add <directory or file>
Enter fullscreen mode Exit fullscreen mode

you can also use

git add .
Enter fullscreen mode Exit fullscreen mode

5. to List which files are staged, unstaged, and untracked

git status
Enter fullscreen mode Exit fullscreen mode

6. for commit the staged snapshot with a commit message

git commit -m "<message>"
Enter fullscreen mode Exit fullscreen mode

7. to display entire commit history

git log
Enter fullscreen mode Exit fullscreen mode

8. to show unstaged changes b/w your index and working directory or to analyze the current state of the repo

git diff
Enter fullscreen mode Exit fullscreen mode

Git Branch

1. List all of the branches in your repo. Add a argument to create a new branch.

git branch
Enter fullscreen mode Exit fullscreen mode

2. Create and checkout a new branch named .

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

-b flag is for checking out an existing branch

3. Merge into the current branch

git merge <branch>
Enter fullscreen mode Exit fullscreen mode

Remote Repositories

1. Create a new connection to a remote repo.

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

after doing this you can use <name> as a shortcut for <url> in other commands.

2. Fetches a specific , from the repo. Leave off <branch> to fetch all remote refs

git fetch <remote> <branch>
Enter fullscreen mode Exit fullscreen mode

3. Fetch the specified remote's copy of the current branch and immediately merge it into the local copy.

git pull <remote>
Enter fullscreen mode Exit fullscreen mode

4. Push the branch to , along with necessary commit objects. Creates named branch in the remote repo if it doesn't exist

git push <remote> <branch>
Enter fullscreen mode Exit fullscreen mode

Git Rebase

1. Interactively rebase current branch onto <base>.Launches editor to enter commands for how each commit will be transferred to the new base

git rebase -i <base>
Enter fullscreen mode Exit fullscreen mode

Git Pull

1. Fetch the remote's copy of current branch and rebases it into the local copy. Uses git rebase instead of merge to integrate the branches

git pull --rebase <remote>
Enter fullscreen mode Exit fullscreen mode

Git Push

1. to link a local branch with the remote branch. The -u flag is used to set origin as the upstream remote in your git config

git push -u <remote> <branch>
Enter fullscreen mode Exit fullscreen mode

2. Forces the git push even if it results in a non-fast-forward merge. Do not use the --force or -f flag unless you're absolutely sure you know what you're doing

git push <remote> --force
Enter fullscreen mode Exit fullscreen mode

or

git push -f <remote> <branch>
Enter fullscreen mode Exit fullscreen mode

Undoing Changes

1. Create new commit that undoes all the changes made in , then apply it to the current branch. here we need to a commit reference id

git revert <commitId>
Enter fullscreen mode Exit fullscreen mode

or git revert Head to create a new commit with the reverted changes which had been done in the last commit.

2. To remove file from staging area but leave the working directory unchanged. This unstages a file without overwriting any changes

git reset <file>
Enter fullscreen mode Exit fullscreen mode

3. used to remove unwanted files from your working directory. -n flag is used for double checking before changing, to execute use -f in place of -n

git clean -n
Enter fullscreen mode Exit fullscreen mode

Rewriting History

1. Replace the last commit with the staged changed and last commit combined

git commit --amend
Enter fullscreen mode Exit fullscreen mode

2. Rebase the current branch onto . <base> can be a commit ID, branch name, a tag, or a relative reference to HEAD.

git rebase <base>
Enter fullscreen mode Exit fullscreen mode

3. Show a log of changes to the local repository's HEAD. Add --relative-date flag to show date time info or --all to show all refs.

git reflog
Enter fullscreen mode Exit fullscreen mode

That's all! I think this is more than enough git commands for getting started with open-source and in the last just look at the image below cuz, it is super helpful.

for up and going

Thank You

Top comments (4)

Collapse
 
bobbyiliev profile image
Bobby Iliev

This is a great post!

For anyone who is just getting started with Git and GitHub, I could also suggest this open-source eBook here:

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
Collapse
 
umishra1504 profile image
Utkarsh Mishra

Thank you!

Collapse
 
waylonwalker profile image
Waylon Walker • Edited

Great post, I'll cross link my favorite git config line

Collapse
 
umishra1504 profile image
Utkarsh Mishra

that's really useful! Thank you for sharing