Starting your coding journey can feel overwhelming, especially when you come across tools like Git and GitHub for the first time. The good news is that you don't need to learn every Git command at once.
Git is a version control system that helps developers track changes in their code, work with different versions of a project, collaborate with other developers, and safely manage their source code. GitHub, on the other hand, is a platform where Git repositories can be hosted and shared online.
If you're just getting started with Git, learning a few commonly used commands will give you a solid foundation.
In this guide, we'll go through 24 useful Git commands, starting with basic configuration and repository setup and moving on to branches, commits, remote repositories, merging, reverting, stashing, and viewing history.
1. Configure Git username and email globally
Before creating commits, it's a good idea to configure the name and email address that Git will associate with your commits.
Command:
git config --global user.name "<first_name> <last_name>"
git config --global user.email "<email_id>"The --global option applies these settings to all Git repositories for the current user.
These settings identify the author of your commits. They are not the same as your GitHub authentication credentials.
2. Initialize a Git repository
If you already have a project directory and want to start tracking it with Git, use:
Command:
git initThis creates a .git directory inside the current folder and turns it into a Git repository.
You can then start adding files and creating commits.
3. Clone an existing repository
If a repository already exists on GitHub or another Git hosting service, you can download a copy to your local computer using:
Command:
git clone <repository_url>This creates a local copy of the repository, including its files, commit history, branches, and remote configuration.
4. Connect a local repository to a remote repository
If you created a local repository using git init and want to connect it to a remote repository, use:
Command:
git remote add origin <remote_url>For example:
git remote add origin https://github.com/username/project.gitHere, origin is the conventional name given to the remote repository.
Once the remote is configured, you can push your local commits to it.
5. Create a new branch
Branches allow you to work on features or changes independently from other development work.
Command:
git branch <branch_name>For example:
git branch feature-loginThis creates the branch but does not switch you to it.
6. Create a branch from an existing branch
You can create a new branch starting from the current state of another branch.
Command:
git branch <branch_name> <existing_branch_name>For example:
git branch feature-login developThis creates feature-login from the current tip of the develop branch.
7. Create a new branch and switch to it
A common workflow is to create a branch and immediately start working on it.
Command:
git checkout -b <branch_name>For example:
git checkout -b feature-loginModern versions of Git also provide:
git switch -c feature-loginBoth approaches create a new branch and switch to it.
8. Delete a branch
Once you're finished with a branch, you can delete it using:
Command:
git branch -d <branch_name>The -d option safely deletes a local branch when Git determines that its changes have already been merged.
If you intentionally want to delete a branch containing unmerged changes, you can use:
git branch -D <branch_name>Use -D carefully because it can remove a branch containing work that has not been merged elsewhere.
9. Check the status of your repository
One of the most frequently used Git commands is:
Command:
git statusIt shows the current state of your working directory and staging area.
For example, it can tell you which files have been modified, which files are staged for the next commit, and which files are currently untracked.
10. Add specific files to the staging area
Before creating a commit, you need to add the changes you want to include to the staging area.
Command:
git add <file1> <file2>For example:
git add index.html style.cssOnly the specified files will be added to the staging area.
11. Add all changes to the staging area
To stage all changes in the current directory and its subdirectories, you can use:
Command:
git add .This is convenient when you want to include all relevant changes in your next commit.
However, it's still a good practice to run git status before committing so you know exactly what will be included.
12. Create a commit
Once your changes are staged, you can create a commit using:
Command:
git commit -m "<description_message>"For example:
git commit -m "Add login form validation"A commit records the staged changes as a new point in the repository's history.
It's generally better to write short and meaningful commit messages that explain what the commit changes.
13. Push local commits to a remote repository
After committing your changes locally, you can upload them to a remote repository using:
Command:
git push origin <branch_name>For example:
git push origin mainThis pushes the commits from your local main branch to the main branch on the remote named origin.
When pushing a newly created local branch for the first time, you can also use:
git push -u origin <branch_name>The -u option sets the upstream tracking relationship, making future pushes easier.
14. Pull changes from a remote repository
If changes have been pushed to the remote repository and you want to retrieve and integrate them into your current branch, you can use:
Command:
git pull origin <branch_name>For example:
git pull origin mainIn simple terms, git pull fetches changes from the remote repository and then integrates them into the current branch.
Depending on your Git configuration and branch history, this integration may happen through a merge or a rebase.
15. Fetch changes from a remote repository
The git fetch command downloads new commits and updated references from a remote repository without automatically changing your current working branch.
Command:
git fetch <remote_name>For example:
git fetch originUnlike git pull, git fetch does not automatically merge the downloaded changes into your current branch.
This makes git fetch useful when you want to inspect the latest remote changes before deciding how to integrate them.
16. Merge one branch into another
The git merge command combines the changes from one branch into the branch you are currently on.
Command:
git merge <branch_to_merge>For example, if you are currently on main and want to merge the feature-login branch:
git merge feature-loginGit will attempt to combine the two branches. If Git cannot automatically resolve conflicting changes, you may need to resolve the merge conflicts manually.
17. Unstage changes
The git reset command has several uses. One common use is to remove changes from the staging area while keeping the actual file modifications in your working directory.
Command:
git resetFor example, if you staged a file using git add but then decided not to include it in the next commit, git reset can remove it from the staging area without deleting your changes.
Git reset can also be used to move the current branch to another commit, so its behavior depends on the options and commit specified.
18. Reset a repository and discard local changes
The --hard option makes reset much more destructive.
Command:
git reset --hard <commit>For example:
git reset --hard HEADThis resets the current branch, staging area, and working directory to match the specified commit.
Using git reset --hard can permanently discard uncommitted changes in tracked files, so it should be used with caution.
If no commit is specified, Git uses HEAD.
19. Revert a commit
If you want to undo the changes introduced by an existing commit while keeping the project history intact, use:
Command:
git revert <commit>For example:
git revert a1b2c3dUnlike git reset, git revert does not remove the existing commit from history. Instead, Git creates a new commit that reverses the changes introduced by the specified commit.
This makes git revert particularly useful when working with shared branches.
20. Temporarily stash your changes
Sometimes you have unfinished changes in your working directory but need to switch branches or work on something else.
Instead of committing unfinished work, you can temporarily store those changes using:
Command:
git stashGit saves your local modifications and returns the working directory to a clean state.
You can then retrieve those changes later.
21. Apply the latest stash and remove it
To restore the most recently created stash, use:
Command:
git stash popThis applies the latest stash to your working directory and, if the operation succeeds, removes that stash from the stash list.
If you want to apply a stash without removing it from the stash list, you can use:
git stash applyYou can view your available stashes using:
git stash list22. Delete a specific stash
If you no longer need a stash, you can remove it using:
Command:
git stash dropBy default, this removes the latest stash entry.
To remove a specific stash, specify its stash reference:
git stash drop stash@{2}Be careful when dropping a stash because the changes stored in that stash may no longer be available through the normal stash commands.
23. View commit history
To view the commit history of your repository, use:
Command:
git logThis displays information such as commit IDs, authors, dates, and commit messages.
For a more compact history, you can use:
git log --onelineThe shorter format is useful when you want to quickly understand the recent history of a project.
24. View remote repositories
To see the remote repositories associated with your local repository, use:
Command:
git remote -vThe output shows the fetch and push URLs associated with each configured remote.
This command is useful when you want to verify which remote repository your local project is connected to.
Final Thoughts
These Git commands cover many of the operations you'll use regularly while working on software projects. You don't need to memorize all of them immediately.
Start with the basics: git init, git clone, git status, git add, git commit, git push, and git pull.
Once you're comfortable with those commands, move on to branches, merging, fetching, reverting, resetting, and stashing.
The best way to understand Git is to use it regularly. Create a small practice repository, make some changes, create branches, commit your work, and experiment with different commands. Working with Git on an actual project will make the concepts much easier to understand.
Remember that Git has many more commands and features than those covered in this article. These 24 commands are simply a practical starting point for developers who are beginning to work with Git and GitHub.