A work-in-progress list of my favorite Git commands
The following list is a reference to the Git commands I use the most. Beneath every command there is an explanation of its use.
As of September 2024, I updated the list with more helpful Git commands.
git init
- Initialize an empty Git repository in the current folder
git clone <url-of-the-git-repository>
- Clones an online repository.
- use the
--depth <depth-number>
in which depth to clone the commit history, aka shallow clone.
git checkout -b <ranch-name>
- Create a new feature branch.
git checkout <commit-hash> <name-of-file>
- Restores a specific version of the file from a previous commit
git restore <name-of-file>
- Discards the local changes and restores the file to its last committed state, assuming that you haven’t yet committed the changes.
git status
- Get the state of the working directory, for example the modified files, staged changes and the branch name.
git add --all
- Stages all changes in your working directory for the next commit.
git show <commit-hash>:<name-of-file>
- Show the content of the specified file
git branch --list
- A list of all local branches inside the repository.
git branch -D <branch-name>
- Delete a branch locally.
git branch --set-upstream-to=origin/<name-of-the-remote-branch name-of-the-local-branch>
- Connect a remote branch to an existing local branch.
git push --set-upstream origin <name-of-the-local-branch>
- Make a new remote branch, based on the local branch.
git push origin <name-of-local-branch>
- Push the local branch to remote.
git push origin --delete <name-of-local-branch>
-
Delete a remote branch.
git pull
- Update the local branch from the remote one.
git rebase main
- Merge main into a feature branch. A practical way to refresh the feature branch with changes that were made in other feature branches and merged to main.
git fetch --all
- Get & update all (not only the mapped ones) remote branches locally.
git reset --hard
- Discards all local changes to all files.
git reset --hard origin/main
- Discards all local changes and jumps to the latest commit of the
main
branch
- Discards all local changes and jumps to the latest commit of the
git reset --mixed HEAD
- Undoes the staging of files but keeps file changes
git blame <name-of-file>
- Displays the revision and author of each line in a file. This way you can identify the commit that introduced a particular change.
git cherry-pick <commit-hash>
- Applies the changes done in the commit with the specified hash, into the current branch. You can find the hash value by using
git log
in the branch where you initially have done the changes.
- Applies the changes done in the commit with the specified hash, into the current branch. You can find the hash value by using
git log -v
- See a detailed history of what changes were made in each commit.
git log -p
- See the history of commits along with the diff introduced in each commit.
git log <absolute-path-to-file>
- List the commit history of a specific file in your repository.
git revert head
- Revert the last commit you made.
git checkout <commit-hash> <absolute-path-to-file>
- Revert changes of a single file in your repository. You can find the commit hash in the git history of your file.
git reflog
- Displays all reference logs (reflogs). Use it to find the hash of a previously deleted commit and you want to recover it. You can find more information about this command here.
git fsck --lost-found
- Checks the integrity of the repository and lists any commits that aren’t reachable from any branch. Use it to recover lost commits.
git tag <name-of-tag> <commit-hash>
- Adds a new tag to a specific commit. Tags are useful when dealing with CI/CD and releases of your software.
git push origin <name-of-tag>
- Pushes the tag created before to the remote branch.
git diff-tree --no-commit-id --name-only
- Gets the names of the changed files of the current branch and presents them as a list. Useful command for identifying updated files, for example when running the command on a DevOps pipeline.