Skip to main content
  1. Learn
  2. Software Development
  3. Guides
  4. Git tutorial
  5. Git commands & settings
  6. Basic Git commands
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

Basic Git commands

As a developer, understanding and utilizing fundamental Git commands is essential for effectively managing version control and collaborating on projects.

In this section, we will explore a range of basic Git commands that form the building blocks of your Git workflow. From initializing a repository and committing changes to adding and removing files, we will cover the essential commands that enable you to navigate your codebase.

By becoming familiar with these basic Git commands, you will gain a strong foundation in Git version control and enhance your ability to work alongside fellow developers. Whether you are a beginner seeking to grasp the essentials or an experienced developer looking to solidify your knowledge, these commands will equip you with the necessary tools to navigate your Git workflow with confidence.

Explore these basic Git commands and empower your software development journey with efficient version control practices.


Create Git repository

$ git init

Run the command init in the directory in which you want to create a repository.

See:

Add files/directories to index

$ git add <filepattern>

In the file pattern, you can specify individual or multiple files and directory names to be added to the index. You can specify the file name directly or use wild card symbols such as *.txt in the code. Putting . in the file pattern will stage all current changes to the index, including files within subdirectories.

If you add a -p option, you will be prompted to accept/reject specific sections of a changed file. If you add a -i option, you can stage changes interactively.

Commit changes to local repository

$ git commit

The -a option is like a shortcut that detects changed files (except for newly added files), adds them to the index, and commits them.

The -m option allows you to commit and specify a commit message at the same time. If you do not specify -m, a text editor will open, prompting you to enter a commit message.

See:

Undo changes from previous commit

$ git revert HEAD

The git revert command takes a commit as an argument and creates a new commit that undoes the changes made by that commit.

See:

Show working tree status

$ git status

Adding the -s option will only display the names of files that have been changed.

Adding the -s option followed by the -b option will include the branch name in the output.

Show changes to working tree and index

$ git diff

The diff command will, by default, show the differences between the working tree and the index.

If you add the --cached option, the differences between the index and HEAD will be shown.

If you specify a commit hash, the differences between the working tree and the current HEAD/commit will be shown.

Show commit log

$ git log

The log will, by default, show a list of commits of the current branch.

Specifying a file name will show a commit log only for that given file.

Show commit details

$ git show <commit>

Specify the commit hash that can be found through the git log command or HEAD in the argument of the command.

Rename files

$ git mv <oldfilename> <newfilename>

Remove files from working tree and index

$ git rm <file>

Remove untracked files from working tree

$ git clean

Adding the -n option will only show the files that will be removed. Adding the -f option will actually remove the files.

By default, files listed under the .gitignore configuration file will not be removed. If you specify the -x option, however, the files listed under “.gitignore” will be removed from the working tree.

Restore files to working tree

$ git checkout -- <file>

Remove files from index

$ git reset HEAD -- <file>

Add only modified and deleted files to index

$ git add -u

This command will register changes from only files that have been added to the index. It will not stage untracked files.

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life