In this tutorial, we will delve into essential and commonly used Git commands, making it easy to understand how to effectively manage your Git repositories on platforms like GitHub. These commands are crucial for daily use and will help you streamline your version control workflows.
In a collaborative setting involving multiple teams or team members, the essential arsenal of Git commands comes into play. These commands are indispensable for facilitating smooth and organized project management.
Git commands list with examples cheat sheet
Here we will demonstrate to you the following git commands like Git Clone, git add, git push, git pull, git revert, git status, git commit, git merge, git branch, git checkout, git remote, git fetch Command. Or git commands cheat sheet.
Here are top git commands list with examples:
- Git Clone
- Git branch
- Git checkout
- Git status
- Git add
- Git commit
- Git push
- Git pull
- Git revert
- Git merge
- Git remote
- Git fetch Command
1). Git Clone Command
This command is used to download the project from a remote server.
Some ways to download the project from remote server. But we recommend using a command line.
git clone https://name-of-the-repository-link
Another way is by visiting GitHub’s website. They can also download the project and any file by clicking on the download and clone repository in green color.
2). Git Branch Command
The git branch has a very important role in Git or GitHub Word.
Using the git branch, many developers and programmers can work on one project at a time.
We will show you below how to create, delete and access the git branch.
Creating a new branch:
git branch
This command will create a branch locally. To push the new branch into the remote repository, you need to use the following command:
git push -u
Viewing branches:
git branch or git branch --list
Deleting a branch:
git branch -d
3). Git Checkout Command
When you work in a project using git. You should know the git checkout command.
The git checkout mostly is used for switching from one branch to another branch. We can also use it for checking out files and commits.
git checkout
There are some steps you need to follow for successfully switching between branches:
The changes in your current branch must be committed or stashed before you switch. The branch you want to check out should exist in your local
There is also a shortcut command that you can create and switch to a branch at the same time:
git checkout -b
This command creates a new branch in your local (-b stand for branch) and checks out to new the branch right after it has been created.
4). Git Status Command
The git status command is used to get the current state of the branch or get information about the branch.
git status
We can collect this kind of information:
- Check the current branch is up to date
- Whether there is anything to commit, push or pull
- Check there are files staged, unstaged or untracked
- Check there are files created, modified or deleted.
5). Git Add Command
The git add command adds a change to the staging area in the working directory. This tells Git that you want to include updates of a particular file in the next commit. However, the git add does not really affect the repository in any significant way – changes are not actually recorded until you run the git commit.
We need to use the git add the command to include the changes of a file(s) into our next commit.
To add a single file:
git add
To add everything at once:
git add -A
6). Git Commit Command
Important: Git commit saves your changes only locally. Git commit is like setting a checkpoint in the development process which you can go back later if needed.
git commit command with comment
git commit -m "commit message
We also can write a short message to explain what we have developed or changed in the source code.
7). Git Push Command
The git push command, which is commonaly used to upload local repository content (After making your changes) to a remote repository. or Git push uploads your compact to a remote repository.
git push
However, if your branch is newly created, then you also need to upload the branch too with the following command:
git push command to a branch
git push --set-upstream
or
git push -u origin
8). Git Pull Command
The git pull command is a combination of git fetch that recently merges the local repository and git repository, which will merge the branch from remote to a local branch, ‘remote_name’ is the repository name and ‘Branch_name’ is the name of the specific branch.
git pull
– Fetch changes from a remote repository and merge them into the current branch.
git pull
This operation may cause conflicts that you need to solve manually.
9). Git Revert Command
Sometimes we have to undo the changes we made. There are different ways to undo our changes locally or remotely (depending on what we need). Instead of deleting or orphaning the commit history, an inversion will create a new commit that reverses the specified changes.
git command revert local changes
Then, we just need to specify the hash code next to our commit that we would like to undo:
git revert 4425678
After this, you will see a screen like this and press shift + q to exit:
Git revert command will undo the given commit but will create a new commit without
deleting the older one:
new "revert" commit
The advantage of using git revert is that it doesn’t touch the commit history so we can still see all of the commits in our history even the reverted ones.
10). Git Merge Cmmand
Git merge command, which is used to merge changes from one branch into another.
For example, when you want to merge your feature branch into dev branch:
Firstly, you should switch to dev branch:
git checkout dev
Before merging, you should update your local dev branch:
git fetch
Finally, you can merge your feature branch into dev:
git merge
11). Git Remote Command
Git Remote Command
Git remote is like a backup that is stored on someone else’s computer. To create Git remote, you can use one of the popular services like Github, Bitbucket, and Gitlab. Create a remote repository, then link your local repository to the remote repository.
Viewing git remote configurations
git remote
List the remote connections you have to other repositories.
git remote -v
12). Git fetch command with options
The git fetch command downloads commits, files, and refs from a remote repository into your local repo. Fetching is what you do when you want to see what everybody else has been working on.
git fetch
Fetch all of the branches from the repository. This also downloads all of the required commits and files from the other repository.
Same as the above command, but only fetch the specified branch.
git fetch --all
A power move which fetches all registered remotes and their branches:
git fetch --dry-run