From Git init To Git merge, We have listed all the essential Git Commands you should know.
Hey, Developer! I have concluded 50 basic Git commands in this blog. I have covered all the git commands which are required for any functionality in GitHub. In this Blog, you will find out what is Git commands, its features, and 50 basic commands. Version control is an integral part of modern software development, and Git stands out as the go-to tool for managing source code. Git provides a powerful set of commands that enable developers to track changes, collaborate seamlessly, and maintain code integrity. In this blog post, we’ll explore the top 12 most used Git commands that every developer should master.
Git is both a distributed version control system and open-source software. It allows developers to maintain many versions of source code with ease. You can use it to determine who did what, when, and why. Git has become a must-have tool for any developer, and developers must be familiar with Git commands in order to properly utilize it. There are hundreds of Git commands, but just a few are used frequently.
What Is Git Command
Git is a distributed version control system (VCS) that is widely used for tracking changes in source code during software development. It was created by Linus Torvalds in 2005 and has since become the de facto standard for version control in the software development industry.
Here are some key concepts of Git Commands:
- Version Control System (VCS): Git allows multiple developers to work on a project simultaneously without interfering with each other. It keeps track of changes made to the source code, allowing developers to collaborate, roll back to previous versions, and merge changes seamlessly.
- Distributed System: Unlike centralized version control systems, Git is distributed. Each developer has a complete copy of the entire project with its history. This makes Git more robust and flexible, as developers can work offline and commit changes locally before pushing them to a central repository.
- Repositories: A Git repository is a storage location for a project, containing all the files, folders, and the history of changes. There can be a central repository, often hosted on services like GitHub, GitLab, or Bitbucket, and each developer can have their local repository.
- Branching: Git makes it easy to create branches, which are separate lines of development. This allows developers to work on new features or bug fixes without affecting the main codebase. Branches can be merged back into the main code when the changes are ready.
key Features of Git Commands:
- Commit: A commit in Git represents a snapshot of the project at a specific point in time. Each commit has a unique identifier (hash) and contains information about the changes made, the author, and a timestamp.
- Merge: Git provides tools to merge changes from one branch into another. This is particularly useful when working on separate features or bug fixes that need to be combined into a single codebase.
- Pull Request (PR): In Git-based platforms like GitHub and GitLab, a pull request is a way to propose changes to a repository. It allows developers to review, discuss, and collaborate on code before merging it into the main codebase.
- Remote: A remote in Git refers to a copy of the repository stored on a server, such as on GitHub. Developers can push changes to a remote repository or fetch changes from it.
1. Git clone
Git clone is a command that downloads existing source code from a remote repository (such as Github). In other words, Git clone creates an identical copy of the most recent version of a project from a repository and saves it to your computer.
There are a couple of ways to download the source code, but I like the clone with https method.
git clone <https://name-of-the-repository-link>
For example, if we want to download a project from Github, all we have to do is click the green button (clone or download), copy the URL from the box, and paste it after the git clone command that I demonstrated earlier.
This will create a copy of the project in your local workspace so you can begin working with it.
2. Git init
The git init command allows us to build a new Git repository. This is the initial command that initiates a new project in a GitHub repository. Run the git init command from the directory containing your project files. It will be expanded to include a hidden.git subdirectory.
$ git init
You can also specify a repository name using the git init command.
$ git init <your repository name>
3. Git branch
Branches are quite significant in the Git ecosystem. Branches allow many developers to work on the same project in parallel. We can use the git branch command to create, list, and delete branches.
Create New Branch
git branch <branch-name>
This command will generate a branch locally. To push the new branch into the remote repository, run the following command:git push -u <remote> <branch-name>
View branch:
git branch or git branch --list
Delete branch:
git branch -d <branch-name>
Also read About: How to make custom keyboard for gaming at home
4. Git checkout
The git checkout command allows us to swap between current branches or create and switch to new ones. To accomplish this, the branch you wish to switch to must be present in your local system, and any modifications in your current branch must be committed or stashed before making the transition. You can also use this command to verify the files.
Usage
Switch to an existing branch:
git checkout <branch-name>
Create and switch to a new branch:
git checkout -b <new-branch-name>
5. Git status
The Git status command provides all relevant information about the current branch.
git status
We can collect information such as:
- Is the current branch up to date?
- Is there anything to commit, push, or pull?
- Whether the files are staged, unstaged, or untracked.
- Whether any files are created, changed, or removed.
6. Git add
When we create, modify or delete a file, these changes will happen in our local and won’t be included in the next commit (unless we change the configurations).
We need to use the git add command to include the changes of a file(s) into our next commit.
To Add a single file:
git add <file>
To Add everything at once:
git add -A
When you look at the screenshot above in the fourth area, you’ll notice that there are red file names, which signifies they’re unstaged files. Unstaged files will not be included in your commits.
7. Git push
After you’ve committed your modifications, you’ll want to transmit them to the remote server. Git push transfers your commits to a remote repository.
git push <remote> <branch-name>
However, if your branch is just formed, you must upload it using the following command:
git push --set-upstream <remote> <name-of-your-branch>
8. Git pull
The git pull command is used to retrieve updates from a remote repository. This command is a combination of git fetch and git merge, which means that when we use git pull, it retrieves updates from the remote repository (git fetch) and immediately applies the most recent changes to your local repository (git merge).
git pull <remote>
9. Git revert
Sometimes we have to undo the changes we’ve made. There are several ways to undo our modifications, both locally and remotely (depending on our needs), but we must use these commands with caution to avoid accidental deletion.
The safer approach to undo our commits is to use git revert. To view our commit history, execute the following command: git log–oneline.
Then we just specify the hash code next to our commit that we want to undo:
git revert 3321844
After that, you will see the screen shown below;
simply hit shift + q to exit:
The Git revert command will undo the supplied commit, however, it will produce a new one without deleting the previous one:
The advantage of using git revert is that it does not change the commit history. This means you can still see all of your commits, including those that have been reverted.
Another safeguard is that everything happens on our local system unless we push it to the remote repository. That is why git reverse is the safer and preferred method for undoing our commits.
10. Git merge
When you’ve finished developing your branch and everything is working properly, merge it with the parent branch (dev or master). This is accomplished with the git merge command.
Git merge basically merges your feature branch and all of its commits back into the development (or master) branch. It’s vital to remember that you must first be on the branch you want to combine into your feature branch.
For example, if you wish to combine your feature branch with the development branch:
switch to the dev branch:
git checkout dev
Update local branch of dev:
git fetch
Merge feature branch to dev:
git merge <branch-name>
Before merging your branches, ensure that your development branch has the most recent version; otherwise, you may encounter conflicts or other issues.
List Of 50 Basic Git Commands You Should Know
Command | Description |
---|---|
git init | Initializes a new Git repository. |
git clone <repo> | Clones a repository into a new directory. |
git add <file> | Adds changes in the working directory to the staging area. |
git commit -m “message” | Records changes in the staging area with a descriptive message. |
git status | Shows the status of changes as untracked, modified, or staged. |
git log | Displays a log of commits with their details. |
git pull | Fetches changes from a remote repository and merges them into the current branch. |
git push | Pushes committed changes to a remote repository. |
git branch | Lists all branches in the repository. |
git checkout <branch> | Switches to the specified branch. |
git merge <branch> | Merges changes from one branch into the current branch. |
git remote -v | Lists all remote repositories associated with the current repository. |
git fetch | Downloads objects and refs from a remote repository. |
git diff | Shows changes between commits, commit and working tree, etc. |
git reset <file> | Unstages the file, but preserves its contents. |
git revert <commit> | Creates a new commit that undoes the changes made in the specified commit. |
git rm <file> | Removes a file from both the working directory and the staging area. |
git mv <old> <new> | Renames a file or directory. |
git tag <tagname> | Creates a lightweight tag pointing to the current commit. |
git fetch –tags | Fetches tags from the remote repository. |
git remote add <name> <url> | Adds a remote repository. |
git remote rm <name> | Removes a remote repository. |
git push origin –delete <branch> | Deletes a remote branch. |
git log –graph –oneline –all | Visualizes the branch and merge history in a compact format. |
git stash | Temporarily saves changes that haven’t been committed yet. |
git stash pop | Applies the changes stashed by the last git stash command. |
git cherry-pick <commit> | Applies changes introduced by a specific commit. |
git rebase <base> | Applies changes of the current branch onto another branch. |
git log –author=<author> | Shows only commits by the specified author. |
git blame <file> | Shows who last modified each line in a file. |
git clean -n | Shows a list of untracked files to be removed with git clean -f . |
git grep <pattern> | Searches for a pattern in the working directory. |
git bisect start | Starts a binary search to find the commit that introduced a bug. |
git reflog | Records when the tips of branches and other references were updated in the local repository. |
git log –pretty=format:”%h %ad- %s [%an]” –date=short | Customized log format with commit hash, date, subject, and author. |
git remote show <name> | Displays information about a remote repository. |
git log –since=2.weeks | Shows commits made in the last two weeks. |
git grep –cached <pattern> | Searches for a pattern in the staging area. |
git submodule add <repo> | Adds a Git submodule to your repository. |
git help | Displays help information about Git commands. |
git show <commit> | Shows the changes made in the specified commit. |
git instaweb | Sets up a simple web interface to browse your repository. |
git rev-parse HEAD | Shows the commit ID of the current HEAD. |
git fsck | Verifies the connectivity and validity of the objects in the database. |
git remote prune origin | Removes remote-tracking references that no longer exist on the remote. |
git log –oneline –graph –all | Displays a simplified graph of the commit history. |
git show-branch | Shows the branches and their commits. |
git update-index –assume-unchanged <file> | Temporarily ignores changes to a tracked file. |
git rev-list HEAD –count | Counts the number of commits in the current branch. |
git cherry -v <branch> | Shows commits in the current branch but not in the specified branch. |
git archive <branch> | Creates a tar or zip file of the specified branch. |
git blame -L 10,20 <file> | Shows who modified the specified lines in a file. |
git rev-parse –abbrev-ref HEAD | Displays the current branch name. |
git log –no-merges | Excludes merge commits from the log. |
git ls-files | Lists all files in the index. |
git bisect bad | Marks the current commit as bad during a git bisect search. |
git bisect good | Marks the current commit as good during a git bisect search. |
Wrap Up
Finally, knowing the 50 basic Git commands presented in this tutorial is a must-do for any developer looking to improve their version control and collaboration skills. Git’s flexibility and power allow developers to easily manage projects, track changes, collaborate seamlessly, and traverse the intricacies of modern software development workflows.
By implementing these commands into your daily development routine, you not only improve your ability to track and manage changes, but you also help to create a smoother and more structured collaborative coding environment. Whether you’re working on a single project or part of a huge team effort, understanding and applying these Git commands will surely improve your development abilities and contribute to the success of your projects.
Continue Read
Leave a Reply