Git & GitHubBasicsbeginner

Git Commands List: Basic to Advanced with Examples

A Git command guide for saving code, pushing, branches, conflicts and safe undo, from basic to advanced.

Published
TL;DR – Quick Answer

The main daily Git commands are git status, git add, git commit, git pull and git push. Use git switch -c followed by a name to create and enter a new branch. Read the status before you change or push anything.

On This Page

Git commands let you save versions of a project, work on changes separately and share them. For daily work, you mainly need git status, git add, git commit, git pull and git push. This page explains those first, then branches, undo commands and advanced tools.

Git is the version control tool on your computer. GitHub is a website that hosts Git projects. You can use Git without GitHub.

Git commands cheat sheet

Task Command
Start a repository git init
Copy an online repository git clone <url>
See changed files git status
See unsaved line changes git diff
Choose one file to save git add <file>
Save a version git commit -m "message"
See short history git log --oneline
Create and enter a branch git switch -c <branch>
Enter an existing branch git switch <branch>
Get and combine remote changes git pull
Send commits git push
Make an undo commit git revert <commit>

Replace words inside < > and do not type those signs. For example, git switch <branch> becomes git switch login-page.

Git moves work through four places:

Working files -> Staging area -> Local commits -> Remote
             add              commit          push

The staging area is the list of changes for your next commit. A commit is a saved version. A remote is another copy, often on GitHub.

Set up Git and start a project

Check the installation, then set your name and email:

git --version
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main

The name and email are added to your commits. If your email should stay private, use the private email option from your Git host.

Start a new local project:

mkdir notes
cd notes
git init
git status

Or copy a project that already exists online:

git clone https://github.com/example/notes.git
cd notes

The example address is a placeholder. Use your repository address.

Create a .gitignore file before your first commit:

node_modules/
.env
target/
*.class

Git will ignore matching untracked files. If a secret was already committed, adding it to .gitignore does not remove it from history. Change the secret at once.

Git commands to commit and push code

Create one practice file:

echo "Learn Git" > notes.txt
git status --short

Git prints:

?? notes.txt

?? means the file is untracked. Stage and commit it:

git add notes.txt
git status --short
git commit -m "Add study notes"
git log -1 --format=%s

The status output and last log line are:

A  notes.txt
Add study notes

A means added to the staging area. Git also prints a commit ID, which will differ on your computer.

Connect an empty online repository and push code for the first time:

git remote add origin https://github.com/you/notes.git
git branch -M main
git push -u origin main

Use your own address. origin is the usual name for the first remote. -u remembers the remote branch, so later you can run only git push.

For the next change, the Git commands to push code are:

git status
git diff
git add notes.txt
git commit -m "Add branch examples"
git pull --ff-only
git push

git pull --ff-only updates only when your branch can move forward without making a merge. If it stops, the local and remote histories differ. Look before choosing a merge or rebase.

Common mistake: git add . can add a secret or a large file you did not mean to save. Run git status first and choose files by name while learning.

Git commands to create a new branch

A branch is a name for a line of work. It lets you change the project without putting unfinished work on main.

Check that your files are clean with git status, then create a new branch:

git switch -c add-tips
git branch --show-current

Output of the second command:

add-tips

Make and save a change:

echo "Commit small changes" >> notes.txt
git add notes.txt
git commit -m "Add commit tip"

Share this branch with git push -u origin add-tips. Teams usually review it in a pull request before combining it with main.

To combine it locally:

git switch main
git merge add-tips
git log --oneline
git branch -d add-tips

-d deletes a branch only when its commits are safely combined with its upstream or the current branch. -D forces deletion, so do not use it unless you know the work is not needed.

Old command Clearer newer command
git checkout main git switch main
git checkout -b login git switch -c login
git checkout -- file git restore file

Use GitHub collaboration basics to practise pull requests.

Fix a merge conflict

A conflict happens when Git cannot decide how to combine changes. It marks the file like this:

<<<<<<< HEAD
Study Git at 6 PM
=======
Study Git at 8 PM
>>>>>>> add-time

The top part is your current branch. The lower part is the branch being merged. Choose the right text and delete all three marker lines. Then run:

git add notes.txt
git commit

Test the project before you push. If you started the merge by mistake and have not completed it, git merge --abort tries to return you to the earlier state. Start merges with clean files, because unsaved work can make the return harder.

Pro tip: Do not choose one side of every conflict without reading it. You may need parts of both versions.

Undo changes safely

The correct undo command depends on where the change is:

Situation Command Result
Unwanted unstaged file change git restore notes.txt Discards the edit
File staged by mistake git restore --staged notes.txt Keeps the edit, removes it from staging
Last private commit message is wrong git commit --amend Replaces the last commit
Shared commit is wrong git revert HEAD Makes a new commit that undoes it
Keep changes but undo a private commit git reset --soft HEAD~1 Moves changes back to staging
Pause unfinished work git stash push -m "login work" Stores tracked edits
Return paused work git stash pop Applies the edits and drops the stash if successful

git restore notes.txt can lose uncommitted work. Check git diff first. Avoid git reset --hard unless you are sure, because it also discards tracked changes.

Use revert for a commit that others may have. Amend and reset replace history, so they are best for private work.

Lost a recent commit? git reflog lists where HEAD pointed on this computer. Find the commit and create a branch at it, for example git switch -c rescue abc1234. Use the actual ID from your reflog.

Basic to advanced Git commands list

Once the daily commands feel easy, learn these:

Command When it helps
git fetch See remote work without changing your branch
git diff main...feature See a branch's changes since it split
git blame file See who last changed each line
git cherry-pick <commit> Copy the change from one commit
git rebase main Replay private work on a newer main
git bisect Find the commit that brought a bug
git tag v1.0.0 Name a release point
git worktree Open another branch in another folder

merge keeps two lines of history and may create a joining commit. rebase replays commits and gives them new IDs. Do not rebase commits that other people are using unless the team agrees.

After rebasing a branch that you alone use, a push may need git push --force-with-lease. This checks that the remote has not changed from what your Git last knew. Run git fetch and look at the changes first. Do not use a plain force push to fix a rejected push on main.

Interview note: git fetch downloads remote information. git pull fetches and then combines it with your current branch. Combining may be a fast-forward, merge or rebase, based on your command and settings.

Practise the full workflow

Use a small public project or make a private one. Follow this routine:

  1. Read the open task and update main.
  2. Create a branch with a short name.
  3. Make one change and test it.
  4. Read git diff, stage only the needed files and check git diff --staged.
  5. Commit with a clear message, such as "Fix empty name error".
  6. Push the branch and open a pull request.
  7. Reply to review, test again and merge with permission.

Write commit messages as a short action. "Add login check" says more than "changes". Keep unrelated work in different commits.

You do not need to memorise every command. Bookmark this Git commands list, use git help <command> and read the output before guessing. Follow GitHub for freshers to set up your profile and write a good README for the project.

When you can explain staging, branches and safe undo commands, practise the Git interview questions. Then learn the terminal basics in our Linux commands guide.

Frequently Asked Questions

What are the first Git commands I should learn?
Start with git status, git add, git commit, git log, git pull and git push. Then learn git switch for branches and git restore for undoing local file edits.
What is the difference between git fetch and git pull?
git fetch downloads remote information without combining it into your current branch. git pull fetches and then combines it, using a fast-forward, merge or rebase.
How do I undo a commit that I already pushed?
If others may have the commit, use git revert. It creates a new commit that undoes the change. Then push that new commit.
Why was my Git push rejected?
A common reason is that the remote has commits your branch does not have. Fetch and inspect those changes before combining them. Do not fix it with a plain force push on a shared branch.
Should I commit a .env file?
Usually no, because it often holds passwords or secret keys. Add it to .gitignore before committing. If a secret was pushed, change the secret as well as removing the file.

Want to Build Your Career in Java Full Stack with AI?

Join CodeBegun and train with working industry engineers — See the Java Full Stack course in Hyderabad

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 3 December 2026 LinkedIn
Chat with us