If you are here reading this post, there is a high probability that you have heard or interacted with Github, and you now want to learn Git. Before we continue with showing you some of the cool Git commands, let’s understand the difference between Git and GitHub.
Git and Github
Let’s first discuss Git. Putting it in the simplest way possible, Git is a distributed version control system used to manage a project in development source code history. Git is like a collaboration tool that enables several developers to contribute to a single project.
Suppose you have a team of developers all working on developing an Information Management System. Some will work on the GUI, others on the Database, and some on the system functionality. Managing such a project would need the use of USB drives to share code between developers. However, with Git, developers can add their code to the project without overwriting any part of the project.
Github, on the other hand, is an online platform used to host the Git repository. While Git is a command-line utility, Github has a Web interface that comes with even more additional features to manage a project.
Top 20 Git commands
In this post, I will show you the top 20 git commands that will come in handy when managing your projects.
1. Git Init
git init
This command initializes a git repository in your directory and creates a .git folder. As a [dot] file, the .git directory is hidden, and you might need to disable the feature in Windows or use the ls -a command in Linux to see it. However, it’s better to leave it as that and not tamper with this folder’s contents.
2. Git Config
git config –global user.name “Your Username Here” git config –global user.email “user@domain.com”
This command configures the username, email, file formats, etc., with those of Github or the online platform hosting your repository.
3. Git Clone
git clone "url.git"
This command downloads/clones a repository from a remote source to your local machine. For example, let’s clone the reaver repository to our local machine. The Reaver is an open-source tool used in Wireless penetration testing.
git clone https://github.com/t6x/reaver-wps-fork-t6x.git
By running the ls command, we see the Reaver folder cloned successfully.
4. Git Status
git status
The Git Status command shows you all information you need to know about your local repository. It includes the branch you re working on, files present in index and untracked files, and modified files.
5. Git Add
git add
The Git Add command adds untracked files present in your working directory to index ready for committing to our remote repository. In the previous example, “git status,” we see our index.html is an untracked file.
Let’s add it to the index for committing using the git add command. See the image below.
git add index.html
By running the ‘git status’ command again, we see the index.html file added to the index and ready for committing.
6. Git Remove
git rm --cached [file-name]
The Git Remove command removes files added from the working directory to index. Let’s remove the index.html that we said in the previous example.
git rm index.html
git rm --cached index.html
7. Git Commit
git commit
The Git Commit command saves your changes to the local repository, ready to be pushed to the remote repository. This command takes three arguments;
- -m (message)
Enables you to leave a note of your changes so that colleagues can understand what happened. See the example below.
git commit -m "Improved Design on Contact Page."
- -a
this argument Takes all modifications performed on the tracked files.
- –amend
This argument updates the most recent commit with any other staged changes or any last commit message.
8. Git Diff
git diff
The Git Diff command lists all untracked changes. For example, let’s add some Html code to our index.html file then run the git diff command. See the output in the image below.
9. Git Reset
git reset
The Git Reset command un-stages your file and put your working directory and index in the state of your last commit. It preserves any changes done to the file.
10. Git Log
git log
This command lists the version history of the branch you are currently working on.
You can use the ‘git log’ command with the ‘-follow’ parameter to list the version history of the file. For example;
git log -follow index.html
11. Git Show
git show
This git command lists the changes made on a file and metadata of the particular commit.
12. Git Tag
git tag
This command lists tags to a specified commit.
13. Git Branch
git branch
The git branch command lists all the branches present in your local repository.
To create a new branch, use the syntax below.
git branch [new-branch-name]
Note, you will only be able to create your first branch after making your first commit.
To delete a branch, use the syntax below.
git branch -d [branch-name]
14. Git Checkout
The Git checkout command enables you to switch between the branches present in your local repository.
git checkout [branch-name]
If the branch you are specifying is not there, add the -b parameter to create the branch.
git checkout -b [branch-name]
15. Git Merge
git branch [branch-name]
This command merges the history of the specified branch into the current working branch.
16. Git Remote
This command links your local repository on your computer to the remote repository.
git remote add [variable-name] [Remote-Repo-Serve-Link]
17. Git Push
git push [variable-name] [branch-name]
This command pushes/sends your committed changes to the remote repository on Github or Bitbucket.
To push all your branches on the local repository to the remote repository, use the syntax below.
git push -all [variable-name]
18. Git Pull
git pull [remote-repo-link]
This Git command fetches the changes of the remote repository and merges them with your local repository.
19. Git Stash
git stash save
The above Git Stash command stores all you tracked files temporarily.
git stash pop
This git command restores any recently stashed files.
20. Git fsck
git fsck
The Git File System Check (fsck) checks the integrity of the Git file system in your local repository by identifying corrupted objects.
Getting to know how different Git commands work is useful, especially if you will be using it to manage your projects. Luckily, most of these commands are easy to understand, and you might not need to use every single of them.
1 comment
15. Git Merge
needs to be updated