GitHub has become an essential tool for developers to manage, store, and collaborate with other developers on software projects. With its simple powerful git-based version control system, GitHub allows users to track changes in their code and keep history of the entire project timeline.
In this article, we will learn about the most commonly used git commands with their examples. You can learn what each git command does and how it is used in projects or repositories with examples.
So, first let’s start with the git commands and its descriptions. It will give you a quick overview of what each command does.
Git Command with descriptions
Command | Description |
---|---|
git init | Initialize a local Git repository |
git clone repo_url | Clone public repository |
git clone ssh://[email protected]/[username]/[repository-name].git | Clone private repository |
git status | Check status |
git add [file-name] | Add a file to the staging area |
git add -A | Add all new and changed files to the staging area |
git commit -m "[commit message]" | Commit changes |
git rm -r [file-name.txt] | Remove a file (or folder) |
git branch | List of branches (the asterisk denotes the current branch) |
git branch -a | List all branches (local and remote) |
git branch [branch name] | Create a new branch |
git branch -d [branch name] | Delete a branch |
git branch -D [branch name] | Delete a branch forcefully |
git push origin --delete [branch name] | Delete a remote branch |
git checkout -b [branch name] | Create a new branch and switch to it |
git checkout -b [branch name] origin/[branch name] | Clone a remote branch and switch to it |
git branch -m [old branch name] [new branch name] | Rename a local branch |
git checkout [branch name] | Switch to a branch |
git checkout - | Switch to the branch last checked out |
git checkout -- [file-name.txt] | Discard changes to a file |
git merge [branch name] | Merge a branch into the active branch |
git merge [source branch] [target branch] | Merge a branch into a target branch |
git stash | Stash changes in a dirty working directory |
git stash clear | Remove all stashed entries |
git push origin [branch name] | Push a branch to your remote repository |
git push -u origin [branch name] | Push changes to remote repository (and remember the branch) |
git push | Push changes to remote repository (remembered branch) |
git push origin --delete [branch name] | Delete a remote branch |
git pull | Update local repository to the newest commit |
git pull origin [branch name] | Pull changes from remote repository |
git remote add origin ssh://[email protected]/[username]/[repository-name].git | Add a remote repository |
git remote set-url origin ssh://[email protected]/[username]/[repository-name].git | Set a repository’s origin branch to SSH |
git log | View changes |
git log --summary | View changes (detailed) |
git log --oneline | View changes (briefly) |
git diff [source branch] [target branch] | Preview changes before merging |
git revert commitid | Revert commit changes |
git config --global user.name "your_username" | Set globally Username |
git config --global user.email "[email protected]" | Set globally Email id |
git config --global --list | Get global config |
Git Commands with examples
Here are examples of all the Git commands that we use regularly in our software development process. Learn the commands with examples and explanations below.
Initialize a repository
git init
Initializes a new local Git repository. This sets up all the necessary files and directories needed for version control.
Clone a repository
git clone https://github.com/user/repo.git
Makes a copy of a remote Git repository to your local machine. This downloads the project’s files, history, and branches.
Add files to staging
git add file.txt
git add .
Stages files to be committed. This puts the files into the staging area to be included in the next commit.
Commit changes
git commit -m "My commit message"
Records a snapshot of the staged changes in the project history. This requires a commit message describing the updates.
Push commits to remote
git push origin main
Pushes local commits to the remote repository. This updates the remote branch with your local changes.
Pull latest changes
git pull origin main
Fetches the latest updates from the remote repository and merges them into your local branch.
Create a branch
git branch new-feature
It creates a new branch in the repository. In the above example, we create a branch named “new-feature”.
Switch to a branch
git checkout new-feature
It switches the user from the current branch (let’s say the master branch) to the new branch that is created i.e. “new-feature” branch.
List branches
git branch
Lists all local branches in the repo. The current branch is highlighted with an asterisk.
Merge a branch
git merge new-feature
Merges changes from a different branch into the current branch. This combines divergent branches and resolves any conflicts.
View commit history
git log
Shows the commit history and changes. Useful for auditing the project history.
Undo a commit
git revert {commitID}
Reverts a previous commit, effectively undoing those changes in a new commit.
Set up user info
git config --global user.name "John Doe"
git config --global user.email "[email protected]"
This sets the user name and email address that will be associated with your Git commits. Whenever we commit a change in Git, it is recorded under a user profile. This command sets the user name and email globally for your account in your machine.
Status
git status
Shows the state of your working directory and staging area. It lets you see which files are modified, staged, etc.
Remove file
git rm file.txt
Removes files from the working directory and stages the removal for commit. This begins tracking the file removal.
Delete branch
git branch -d branch-to-delete
git branch -D branch-to-delete
This command will delete a branch from your repository. Change “branch-to-delete” with the name of the branch you want to delete.
Rename branch
git branch -m old-name new-name
This commands helps you to rename a branch name in your git repository.
Stash
git stash
git stash clear
Temporarily stores uncommitted changes for later use. This cleans your working directory to switch branches.
Set remote URL
git remote set-url origin ssh://[email protected]/user/repo.git
The git remote set-url
command allows you to change the URL that a remote Git repository is pointing to.
Preview merge
git diff branch-1 branch-2
The git diff
command in Git allows you to view differences between commits, branches, files and more.
Undo commit
git revert {commitID}
The git revert
command in Git is used to undo a committed snapshot by creating a new commit.
The {commitID}
refers to the unique SHA-1 hash identifier of the commit that you want to undo.
Conclusion
In summary, we can say that, while Git and Github have a lot of power and flexibility for managing code projects, the core concepts boil down to a handful of simple but powerful commands. Cloning, committing, pushing, pulling, branching and merging form the basis for most Git workflows. And mastering these commands helps to work efficiently on our development projects.
Download GitHub Commands Cheat Sheets
Download the github Cheatsheet that contains more commonly used github commands.