Basic Git commands
- Create Git repository
- Add files/directories to index
- Commit changes to local repository
- Undo changes from previous commit
- Show working tree status
- Show changes to working tree and index
- Show commit log
- Show commit details
- Rename files
- Remove files from working tree and index
- Remove untracked files from working tree
- Restore files to working tree
- Remove files from index
- Add only modified and deleted files to index
Create Git repository
$ git init
Run the command init in the directory in which you want to create a repository.
See:
- Git basics > Creating repositories
- How to use Git > Create a local repository (Windows)
- How to use Git > Create a local repository (Mac)
- How to use Git > Create a local repository (Command Line)
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:
- Git basics > Committing changes
- How to use Git > Commit a file (Windows)
- How to use Git > Commit a file (Mac)
- How to use Git > Commit a file (Command Line)
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.