38
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.
1. To Create
empty git repo
in specified directory. Run with no argumentsgit init
2. For
cloning
the repo onto your local machinegit clone <repo--HTTP or SSH>
3. To define
author name
to be used for all commits in current repogit config user.name <name>
4. To
stage
all changes in <directory>
or <file>
for the next commitgit add <directory or file>
you can also use
git add .
5. to
List
which files are staged, unstaged, and untrackedgit status
6. for
commit
the staged snapshot with a commit messagegit commit -m "<message>"
7. to display entire commit history
git log
8. to show unstaged changes b/w your index and working directory or to analyze the current state of the repo
git diff
1. List all of the branches in your repo. Add a argument to create a new branch.
git branch
2. Create and checkout a new branch named .
git checkout -b <branch>
-b
flag is for checking out an existing branch3. Merge into the current branch
git merge <branch>
1. Create a new connection to a
remote repo
.git remote add <name> <url>
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 refsgit fetch <remote> <branch>
3. Fetch the specified remote's copy of the current branch and immediately merge it into the local copy.
git pull <remote>
4.
Push
the branch to , along with necessary commit objects. Creates named branch in the remote repo if it doesn't existgit push <remote> <branch>
1. Interactively rebase current branch onto
<base>
.Launches editor to enter commands for how each commit will be transferred to the new basegit rebase -i <base>
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>
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>
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 doinggit push <remote> --force
or
git push -f <remote> <branch>
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>
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>
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
1. Replace the last commit with the staged changed and last commit combined
git commit --amend
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>
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
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.

38