GIT Basic Command

Most developers are currently using GIT as their main repository, but even though we use it every day in our jobs, like myself, I still haven’t fully mastered the basic commands. I often need to revisit certain links to find the commands I need. Most of the blog posts and tutorials I come across are not very concise and tend to have lengthy explanations. In this post, I will collect and list all the basic GIT commands, which will serve as your code companion.

Basic GIT Command:

  • Initialize a Repository: Initializes a new Git repository in the current directory.
Bash
git init
  • Clone a Repository: Clones an existing repository from a URL to your local machine.
Bash
git clone [repository URL]
  • Check Repository Status: Shows the current state of the working directory and staging area.
Bash
git status
  • Track/Stage Files: Stages changes for the next commit (replace [file] with the filename or use . to add all files).
Bash
git add [file]
  • Commit Changes: Commits staged changes with a message describing the change.
Bash
git commit -m "Commit message"
  • View Commit History: Shows a list of previous commits.
Bash
git log
  • View a Single Line Summary of Commit History: Condensed commit log with one line per commit.
Bash
git log --oneline
  • Create a New Branch: Creates a new branch with the specified name.
Bash
git branch [branch name]
  • Switch Branches: Switches to the specified branch.
Bash
git checkout [branch name]
  • Merge Branches: Merges the specified branch into the current branch.
Bash
git merge [branch name]
  • Pull Updates from Remote: Fetches and merges changes from the remote repository into the current branch.
Bash
git pull
  • Push Changes to Remote: Pushes committed changes to the remote repository.
Bash
git push
  • Delete a Branch: Deletes the specified branch locally.
Bash
git branch -d [Branch Name]
  • Discard Changes in a File: Reverts the file to the last committed state.
Bash
git checkout -- [file]
  • Stash Changes: Temporarily saves uncommitted changes and cleans the working directory.
Bash
git stash
  • Apply Stashed Changes: Restores stashed changes and removes them from the stash list.
Bash
git stash pop
  • Show Differences Between Commits/Branches: Shows changes between commits, branches, or files.
Bash
git diff [source branch] [target branch]
  • Set Remote Repository: Adds a remote repository to push to or pull from.
Bash
git remote add origin [repository URL]
  • Rename Branch: Renames the current branch.
Bash
git branch -m [new branch name]
  • Remove a Remote Repository: Removes a reference to a remote repository.
Bash
git remote remove [remote name]

These commands cover the essentials for everyday GIT usage. They should help you with most of your version control needs.

Assi Arai
Assi Arai
Articles: 31