Git Commands: A Practical Guide to Version Control


Git is one of the most useful tools in a developer's daily workflow. It records how a project changes, lets you experiment without losing working code, and makes it possible for multiple people to contribute without constantly overwriting one another. Learning Git commands is therefore more than memorising terminal instructions: it is learning how to manage change deliberately.
This practical guide explains the Git commands developers use most, what each command changes, when to use it, and how the commands fit together. It starts with repositories, commits, and the staging area, then moves through branches, remotes, collaboration, history inspection, undo operations, and troubleshooting. Each example uses a typical command-line workflow and can be adapted to projects written in different programming languages.
Git is a distributed version control system. It records changes to files in a repository and allows developers to inspect, compare, share, and restore earlier versions. Distributed means that each developer's local clone contains the project's history, rather than depending on one central server for every operation. You can create commits, inspect history, and work on branches while offline, then exchange changes with a remote service when a connection is available.
Git and GitHub are related but different. Git is the version control tool that runs locally. GitHub is a hosting and collaboration platform for Git repositories. Other hosting services provide similar capabilities, and Git can also be used without any hosting service at all.
A Git workflow revolves around three important areas:
A typical change moves from the working tree to the staging area with git add, then into repository history with git commit. This separation is valuable because it lets you choose exactly which changes belong together in a commit.
Git does not automatically save every edit. Until you stage and commit a change, it remains outside the permanent project history. A commit is also not the same as a backup of every external resource used by a project: databases, deployment settings, uploaded assets, and hosted services may need separate backup procedures.
After installing Git, configure the name and email address that should appear in your commit metadata. For a global configuration that applies to your user account, run:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Use an email address associated with your hosting account if you want commits to be attributed to that account. GitHub also provides a private noreply address for people who prefer not to expose a personal email address in public commit history. Check your account's current email settings before choosing one.
Review the values with:
git config --global --list
git config user.name
git config user.email
The --global option applies to all repositories for the current operating-system user. A repository-specific setting, run from inside that repository without --global, overrides the global value. This is useful when personal and work projects need different identities.
You can choose a default initial branch name, such as main:
git config --global init.defaultBranch main
Git configuration also supports aliases, editor preferences, credential helpers, and other options. Add these only when they solve a real workflow need. Avoid placing access tokens or passwords directly in configuration files, shell history, scripts, or remote URLs. Use a supported credential manager or SSH authentication instead.
Use git init to turn an existing directory into a new local repository:
mkdir sample-project
cd sample-project
git init
Git creates a hidden .git directory that stores repository metadata and history. The project files remain in the working directory. Initialising a repository does not automatically create a remote repository or upload files to a hosting platform.
If a project already exists on a hosting service, use git clone to create a local copy:
git clone https://github.com/owner/project.git
cd project
Cloning downloads the repository and its available history, and normally configures the original repository as a remote named origin. You can also clone using an SSH URL when your SSH key is configured.
To inspect the current repository and its state, use:
git status
git rev-parse --show-toplevel
git remote -v
git branch --show-current
These commands answer different questions: status describes changes, rev-parse identifies the repository root, remote lists connected repositories, and branch shows the current branch. Running them before a significant operation is a simple way to avoid working in the wrong directory or branch.
Use git status --short for a compact summary. It is especially useful in large projects where the full status output contains many files. Do not assume that a directory is a Git repository just because it contains source code; confirm that Git recognises it before running commands that change history.
The git status command is one of the most useful commands to run frequently:
git status
It shows the current branch, staged changes, unstaged modifications, and untracked files. Untracked files are files Git has not yet been told to manage. Modified tracked files have changed since the last recorded version.
Use git add to stage changes for the next commit:
git add README.md
git add src/
git add .
The first example stages one file, the second stages a directory, and the third stages changes under the current directory. Be deliberate with broad staging commands. A command such as git add . can stage generated files, local configuration, or other content that should not be committed if ignore rules are incomplete.
For more control, use interactive patch staging:
git add -p
Git presents individual change hunks and lets you select which ones to stage. This is helpful when one file contains several unrelated edits that should be recorded in separate commits.
Use git diff to inspect unstaged changes:
git diff
Use git diff --staged to inspect what is already staged and will be included in the next commit. You can compare two branches or commits with:
git diff main..feature/search
The distinction between unstaged and staged changes matters. A developer may edit a file after staging it; the staged version and the current working copy can then differ. Reviewing both diffs before committing helps prevent accidental omissions or unrelated changes.
A .gitignore file tells Git which untracked files and paths to ignore. Typical examples include dependency directories, build output, temporary files, editor settings, and local environment files. For example:
node_modules/
dist/
.env
.env.*
!.env.example
.DS_Store
Ignore patterns do not remove files that are already tracked. If a secret has been committed, adding it to .gitignore does not erase it from history; revoke or rotate the exposed credential and follow an appropriate history-cleanup process.
The git commit command records the staged changes as a new commit:
git commit -m "Add account settings page"
A commit records a snapshot of the staged project state, together with metadata such as author, date, and message. A good commit message is concise but specific enough to explain the change. Messages such as "Update files" or "Fix stuff" provide little value when someone later investigates a regression.
Prefer one logical purpose per commit. For example, keep a bug fix separate from a documentation refresh when the changes can be reviewed independently. Small, focused commits make code review, rollback, and troubleshooting easier.
To inspect the most recent commit, use:
git show
git show --stat
git show --name-only
To amend the latest commit message or include additional staged changes, use:
git add README.md
git commit --amend
Amending replaces the latest commit with a new commit. If the original commit has already been pushed and other people may have based work on it, coordinate before rewriting the shared branch. Do not amend a shared commit casually.
You can create a commit with a message directly using git commit -am "message", but this only stages modifications and deletions to files Git already tracks. It does not include new untracked files. Beginners often assume -am stages everything; check status to confirm what will be committed.
A branch is a movable reference to a line of development. Branches allow you to work on a feature or fix without immediately changing the main development line.
List local branches with:
git branch
Create a branch with:
git branch feature/profile-page
Switch to an existing branch with:
git switch feature/profile-page
Create and switch in one command:
git switch -c feature/profile-page
The older git checkout command can also switch branches and restore files, but git switch and git restore provide more focused commands for those tasks.
A useful branch name communicates the purpose of the work. Common prefixes include feature/, fix/, bugfix/, docs/, and chore/. For example, fix/login-validation is easier to understand than test-branch-2.
To rename the current branch, use:
git branch -m feature/new-name
To delete a local branch that has already been merged, use:
git branch -d feature/profile-page
The uppercase force-delete option can remove a branch that Git considers unmerged. Use it only after confirming that the branch's work is no longer needed. Deleting a branch reference does not immediately erase every commit, but recovery can become difficult after unreachable objects are eventually pruned.
Before switching branches, inspect your working tree. Git may prevent a switch if uncommitted changes would be overwritten. Commit the work, stash it, or deliberately restore it only after confirming that the changes are disposable.
A remote is a named reference to another copy of a repository, usually hosted on a service such as GitHub, GitLab, or Bitbucket. List configured remotes with:
git remote -v
Add a remote named origin:
git remote add origin https://github.com/owner/project.git
Change an existing remote URL with:
git remote set-url origin https://github.com/owner/new-project.git
Remove a remote configuration with:
git remote remove origin
Removing a remote does not delete the remote repository on the hosting service. It only removes the local name and URL configuration.
For a newly initialised local repository, create the remote repository on your hosting platform, add its URL, and push the initial branch:
git branch -M main
git remote add origin https://github.com/owner/project.git
git push -u origin main
The -u option sets the upstream branch, which allows later push and pull commands to use the configured tracking relationship. Authentication must be configured separately. Use a credential manager, SSH key, or another supported authentication method rather than embedding passwords in commands.
The git fetch command downloads objects and updates remote-tracking references without automatically merging them into your current branch:
git fetch origin
git fetch --all
Fetching first is a good habit when you want to inspect incoming work before integrating it. After fetching, compare your branch with its upstream:
git log --oneline HEAD..origin/main
git diff HEAD..origin/main
The git pull command fetches changes and then integrates them into the current branch. Depending on configuration and options, integration may use merge or rebase:
git pull
git pull --rebase
Understand your team's convention before choosing a strategy. Rebasing can produce a linear history, while merging preserves the explicit integration point. Neither is universally correct for every repository.
The git push command sends local commits to a remote:
git push
git push -u origin feature/profile-page
A push can be rejected if the remote branch contains work that your local branch does not include. Fetch and inspect the difference before deciding how to integrate it. Do not respond to every rejected push with a force push.
If you need to update a remote branch after intentionally rewriting your own unpublished or coordinated branch history, prefer the safer lease check:
git push --force-with-lease
This option checks that the remote reference has not changed unexpectedly since your last observation. It is still a history-rewriting operation and should not be used on shared branches without agreement.
The git merge command integrates another branch into the current branch. To merge a feature branch into main:
git switch main
git pull
git merge feature/profile-page
If Git can combine the changes automatically, the merge completes. If both branches changed overlapping parts of a file in incompatible ways, Git reports a conflict and pauses the merge.
Run git status to identify conflicted files. Open each file and look for conflict markers:
<<<<<<< HEAD
Current branch version
=======
Incoming branch version
>>>>>>> feature/profile-page
These markers are instructions for the developer, not valid final application content. Decide how the combined code should behave, edit the file to keep the correct result, and remove the markers.
After resolving a file, stage it and complete the merge:
git add src/profile.ts
git status
git commit
If the merge process should be abandoned, use:
git merge --abort
This attempts to restore the pre-merge state. If you had uncommitted changes before starting, preserve them carefully and inspect status before and after aborting. Run relevant tests after resolving conflicts; a syntactically valid resolution can still introduce a logical bug.
Git provides several commands for understanding how a repository reached its current state.
git log
git log --oneline
git log --oneline --graph --decorate --all
The first shows detailed history, the second gives a compact list, and the third visualises branches and merge relationships. Limit output with -n:
git log -n 10 --oneline
Inspect a particular commit with git show COMMIT_SHA. Search commit messages with:
git log --grep="authentication" --oneline
Find commits that changed a particular file with:
git log --oneline -- src/auth/login.ts
Use git blame to see which commit last changed each line of a file:
git blame src/auth/login.ts
Blame is a historical aid, not a tool for assigning fault. A line may have been introduced as part of a larger refactor, generated file, or later change in requirements. Follow the commit history to understand the context.
Compare a file or branch with another version:
git diff main..feature/profile-page
git diff HEAD~1..HEAD
git show COMMIT_SHA -- src/auth/login.ts
These commands are useful during code review and when investigating regressions. A diff shows textual changes, not whether the application behaves correctly, so pair it with tests and appropriate runtime checks.
Git offers several ways to undo changes, but they operate at different stages. Understand the difference before running a destructive command.
To discard unstaged edits to a tracked file:
git restore README.md
This replaces the working copy with the staged or committed version, depending on the state of the file. Uncommitted edits to that file can be lost.
To unstage a file while keeping its working-tree edits:
git restore --staged README.md
To create a new commit that reverses an earlier commit, use:
git revert COMMIT_SHA
Revert is generally the safer choice for changes already shared with others because it preserves history and records the reversal as a new commit.
To move the current branch pointer to an earlier commit, use git reset. The modes have different effects:
git reset --soft COMMIT_SHA moves the branch pointer but keeps changes staged.git reset --mixed COMMIT_SHA moves the pointer and unstages changes while keeping them in the working tree.git reset --hard COMMIT_SHA also replaces tracked working-tree files and can permanently discard uncommitted work.Never use git reset --hard as a routine way to fix a confusing repository state. Inspect status, create a backup branch or copy when necessary, and confirm exactly which commit you are targeting. Resetting a branch that has already been pushed may require a history rewrite and can disrupt collaborators.
If a commit or branch seems to have disappeared, inspect the reflog:
git reflog
The reflog records recent movements of local references and can help locate a previous commit. It is not a permanent backup and entries eventually expire. If important work is missing, stop running cleanup commands and preserve the repository state before attempting recovery.
The git stash command temporarily shelves tracked working-tree changes so you can switch context without committing incomplete work:
git stash push -m "Work in progress: profile page"
git stash list
git stash show -p
git stash apply
git stash pop
Use apply when you want to restore the changes but keep the stash entry. Use pop when you want to restore and remove the entry after successful application. Stashes can conflict with later work, so inspect the result and run tests. By default, untracked files may not be included; use the appropriate option if you intentionally need them.
Tags mark particular commits, often to identify releases:
git tag
git tag -a v1.0.0 -m "First stable release"
git show v1.0.0
git push origin v1.0.0
An annotated tag stores additional metadata and is commonly used for release markers. To push all local tags, use git push --tags, but do so only when the tags are intended to be shared.
Other useful commands include:
git rm FILE removes a tracked file and stages the deletion.git mv OLD NEW moves or renames a tracked file and stages the change.git clean -n previews the untracked files that a clean operation would remove.git remote show origin displays information about a remote and its tracked branches.git cherry-pick COMMIT_SHA applies the change introduced by a commit onto the current branch.git ls-files lists files tracked by Git.Be especially careful with git clean -f and git clean -fd: they remove untracked files and directories. Preview with -n first and make sure no untracked work needs to be kept.
A branching strategy should match the project's size, release process, and testing capability. It should make work easier to coordinate rather than adding ceremony without benefit.
GitHub Flow is a lightweight approach: keep main in a deployable state, create a short-lived branch for a change, commit focused updates, open a pull request, review and test, then merge. It works well for many web projects and teams that release frequently.
Git Flow uses longer-lived branches such as main and develop, alongside feature, release, and hotfix branches. It can suit projects with planned release cycles and multiple supported versions, but it introduces more branch-management overhead.
Trunk-based development emphasises frequent integration into a shared main branch, using very short-lived branches or direct commits under appropriate controls. It depends on reliable automated tests, small changes, and the ability to keep incomplete functionality safely disabled.
For a personal project, a simple main branch plus short-lived feature or fix branches is often sufficient. For a team, agree on naming, review requirements, merge strategy, and release rules before multiple contributors begin working.
A collaborative pull request should explain the purpose of the change, outline important implementation decisions, list tests performed, and include screenshots when the user interface changes. Reviewers should focus on correctness, security, maintainability, performance, and project conventions. Resolve requested changes on the same branch and confirm that required checks pass before merging.
After a pull request is merged, verify the default branch and any resulting deployment. A merged commit is not proof that a production application deployed successfully. Check the deployment environment, build result, health checks, and logs when the project is deployed automatically.
A reliable Git workflow depends as much on habits as on commands.
Check before you change. Run git status, confirm the current branch, and inspect the latest commits before a risky operation. This simple check prevents many accidental changes to the wrong branch.
Commit in logical units. Group related changes together and write messages that explain the purpose. Smaller commits are easier to review and revert.
Review the staged diff. Before committing, run git diff --staged. Check for debug output, unrelated formatting changes, generated files, secrets, and accidental deletions.
Keep secrets out of Git. Do not commit passwords, API keys, private keys, access tokens, or production environment files. Store secrets in an appropriate secret manager or deployment environment. If a credential is exposed, revoke or rotate it immediately; deleting the line in a later commit does not make the old value safe.
Use ignore rules thoughtfully. Exclude dependencies, caches, local settings, build output, and files that can be regenerated. Do not ignore source files or lockfiles that the project needs to build consistently.
Synchronise regularly. Fetch remote changes and integrate them according to the team's agreed merge or rebase policy. Long-lived branches increase the chance of difficult conflicts.
Protect important branches. Use hosting-platform branch rules to require reviews and passing status checks where appropriate. Local Git commands alone cannot enforce a team's remote repository policy.
Test after integration. Run tests after merges, rebases, cherry-picks, and conflict resolution. Inspect the final diff and verify the application or service when relevant.
Preserve recovery options. Push important branches, use tags for releases, and maintain backups appropriate to the project's value. A Git clone contains repository history but not necessarily every issue, discussion, release asset, deployment setting, or external data source.
Git says the current directory is not a repository. You may be in the wrong folder, or the project may not have been initialised. Use pwd or your file manager to confirm the location, then run git status from the repository root. Do not initialise a new repository inside an existing project without first checking its structure.
Git reports that your identity is unknown. Configure user.name and user.email with git config. Check whether a repository-specific setting overrides the global value.
A push is rejected as non-fast-forward. The remote branch contains commits your local branch does not have. Run git fetch, inspect the incoming commits, and integrate them with the agreed merge or rebase approach. Avoid force pushing until you understand why the histories differ.
Git reports a merge conflict. Use git status to identify conflicted files, edit the conflict markers, stage the resolved files, and complete the merge or rebase. Run tests after resolution. If the operation should be cancelled, use the matching abort command, such as git merge --abort or git rebase --abort.
A file is missing from a commit. It may not have been staged, may be ignored, or may have been created after the commit. Run git status --short, inspect git check-ignore -v FILE, and check the staged diff before committing again.
A large file makes the repository difficult to clone. Build outputs, archives, datasets, and binary assets can increase repository size. Avoid committing files that can be generated. For assets that genuinely need versioning, evaluate Git Large File Storage or a suitable artifact-storage service. Removing a large file from the latest commit does not remove it from earlier history.
Authentication fails. Check the remote URL, credential manager, SSH key configuration, and account permissions. A valid account may still lack access to a particular repository or branch. Never paste a token into a public issue or commit.
You committed to the wrong branch. Stop and inspect the commit and working tree before attempting to move it. If the commit has not been pushed, you may be able to create the intended branch at that commit and restore the original branch carefully. If it has been pushed, coordinate with collaborators and use a deliberate corrective commit or branch operation. Avoid resetting shared history without agreement.
You accidentally deleted local work. Stop making changes and inspect the reflog, stash list, recent commits, and filesystem backups. Recovery is not guaranteed, especially for uncommitted files that were removed by destructive commands. This is why previewing clean operations and keeping regular commits matter.
The following example demonstrates a common feature workflow in an existing repository. It assumes the project already has a remote named origin and that main is the default branch.
First, confirm the repository and update your local view of the remote:
git status
git branch --show-current
git fetch origin
git switch main
git pull --ff-only
The --ff-only option prevents pull from creating an unexpected merge commit if the local branch has diverged. If it fails, inspect the history and resolve the divergence deliberately rather than forcing the update.
Create a short-lived branch:
git switch -c feature/profile-settings
Make the change in your editor. Inspect the working tree and stage only the relevant files:
git status
git diff
git add src/profile/
git diff --staged
Commit the work with a clear message:
git commit -m "Add profile settings"
Run the project's tests and build commands. Then publish the branch:
git push -u origin feature/profile-settings
Open a pull request on the hosting platform. Describe what changed, explain how it was tested, and provide screenshots or additional context if useful. Address review feedback with further commits and verify the required checks.
After approval, merge using the repository's chosen strategy. Update your local main branch and remove the feature branch when it is no longer needed:
git switch main
git pull --ff-only
git branch -d feature/profile-settings
git fetch --prune
Finally, verify the deployed application if the repository is connected to a deployment service. Check that the expected commit was deployed, the build succeeded, and the application responds correctly. This last step separates successful source-code integration from successful delivery.
Start with git status, git init, git clone, git add, git diff, git commit, git log, git branch, git switch, git pull, and git push. These cover repository creation, change tracking, commits, branches, and collaboration. Learn git restore, git revert, and git reset carefully because they affect existing work in different ways.
git add places selected changes into the staging area. git commit records the staged changes in repository history. Staging gives you an opportunity to review and organise a logical set of changes before recording them.
git fetch downloads remote updates and refreshes remote-tracking references without integrating them into the current branch. git pull fetches and then integrates those updates, usually through merge or rebase depending on configuration and options.
Merge combines branch histories and may create a merge commit. Rebase replays commits onto a different base, creating new commit identities and often a linear history. Rebase is useful for cleaning up private branch history, but avoid rewriting commits that other people already depend on unless the team has agreed to it.
Use git revert COMMIT_SHA to create a new commit that reverses an earlier commit. Use git reset only when you understand how it changes the current branch and staging state. Revert is generally safer for commits already shared with a remote team.
Run git status for a summary, git diff for unstaged edits, and git diff --staged for staged changes. Use git diff --name-only when you only need a list of changed file paths.
Yes. Git is a standalone version control system. You can create local repositories and manage commits and branches without hosting them on GitHub. A remote hosting service becomes useful when you need off-device collaboration, code review, issue tracking, or hosted automation.
It is safe only when you intentionally want to discard tracked working-tree and staging changes and have confirmed that nothing important will be lost. It can destroy uncommitted work. Inspect status and preserve important changes before using it.
Revoke or rotate the credential immediately, then investigate where it was used. Remove the secret from the current code and follow the hosting provider's sensitive-data removal guidance if it remains in repository history. A later deletion commit does not invalidate a leaked credential.
Commit whenever you complete a small, coherent unit of work that can be described and reviewed independently. Avoid both extremes: one enormous commit for weeks of unrelated work and a large number of meaningless commits that do not explain the project's evolution.
Git behaviour can vary with version and configuration, so use official documentation when checking command options or advanced workflows.
For related guidance, read the Comprehensive GitHub Guide, which explains repository management, collaboration, security, and GitHub Actions.

Learn the Git commands developers use every day, from staging and branching to collaboration, conflict resolution, recovery, and safer team workflows, with practical terminal examples.
Read
A practical GitHub guide to creating repositories, using Git, managing branches and pull requests, automating workflows, protecting code, and releasing projects.
Read
Explore practical free developer tools for coding, version control, testing, APIs, databases, containers, deployment, security, and collaboration—with the limits to check before you depend on them.
Read