What is Git
Git is a distributed version control system that tracks changes in any set of computer files, usually used for coordinating work among programmers who are collaboratively developing source code during software development. Git’s goals include speed, data integrity, and support for distributed, non-linear workflows.
– Wikipedia
In simple words, it helps to manage any sort of changes to our code or files in a project. It breaks down our software development into multiple versions, allowing us to go back to previous versions if needed.
Git maintains repos(repositories). Repo contains all our code and its history. Git stores all the changes we make in the repo. If we make changes in files and we want to take a snapshot of those changes we have to commit those changes to the repo and git creates a history for us of those changes with a unique identifier.
Github is the web-based platform built on top of Git. It serves as a platform for hosting git repos and collaborating on projects with others. We can push our commited changes to Github so that others can work on tham too.
Git has 3 main states: Modified, Staged, and Committed.
The working directory contains a specific version or state of our file which is pulled out from the .gitdirectory. In the working directory, we make changes and add them to a staging area when we are satisfied with our changes. The staging area or staged state can be thought of as preparing our changes to be saved. We save our changes before committing them. At this state, we can choose which files we want to include or exclude from being committed. At the final state, we commit or save the changes to the .git directory with a unique identifier. Now we can push our final files to the remote repository.
Committing our changes is also referred to as taking snapshots of our changes. We are just taking snapshots of our files at a specifc point. This is useful for tracking our changes or creating a backup of our files.
Now we understand how Git works. Let’s move on to learn about the frequently used Git commands.
In case Git isn’t installed in your machine, please follow this article :
https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
Commands for setting up a Git repo
- git init
git initIt creates an empty git repo. If we write this command in the existing directory then it initializes the directory as a git repo. Basically, this is the way to create a .git directory.
- git clone
git clone [valid-url]Retrieve the entire existing repo from GitHub. git clone takes a valid repo URL and retrieves the repo to our local machine.
git clone https://github.com/husainasfak/dub.gitIf we want to clone the repo in a specific folder, we have to pass the folder name as the second argument.
git clone [valid-url] [folder-name]If the specified folder doesn’t exist, Git will create a new one and clone the repo into it. If the folder already exists, Git will use that folder to clone the repo into it. But there is a catch here, If the existing folder already consists of a .git directory, Git will not clone the repo into it, as it would result in conflicts.
Commands for setting up a remote git repo
We need these commands when we want to connect our local directory to a remote git repo which is hosted on GitHub. We need to connect our local repository to the remote repository because, after that, we can push and pull changes between our local and remote repositories.
- git remote add
git remote add [name] [valid-repo-url]Add remote repository to our local git repository
git remote add origin https://github.com/husainasfak/git-command-example.gitNow our local git repository pointing to github.com/husainasfak/git-command-example… with name origin. If we push any changes it will save in git-command-example remote repo.
- git remote
git remoteShows all remote repositories
If we pass the option -v (verbose), it will show the remote urls too.
git remote -v - git remote remove
git remote remove [name-of-remote-repo]git remote rm [name-of-remote-repo]
Remove the remote repository from our local git repository. We have to pass the existing remote repo name as an argument.
git remote remove origingit remote rm origin
It removes the remote repo named origin.
- git remote set-url
git remote set-url [name-of-remote-repo] [new-url]This command updates the URL that our local repo associates with a particular remote name
git remote set-url origin https://github.com/husainasfak/git-command-example1.gitBefore the origin pointed to github.com/husainasfak/git-command-example…, but after executing this command It changes the URL. Now the origin pointing to github.com/husainasfak/git-command-example1.. URL.
Commands for Staging and Snapshotting
- git add
This command adds our changes in the staging area for the next commit. This command has multiple variations and all the variations work differently. Let’s go through each one:
➤ If we want to add specific files in the staging area for commit.
git add [file-names]We have to specify the file names in the command and it will put the files in the staging area.
git add index.html contact.html➤ If we want to add the current directory in the staging area for commit.
git add .This command puts all the changes of the current directory and its subdirectory into the staging area. It stages all the modified files and new files, but it doesn’t stage deleted files because it only stages changes in tracked files. If a file has been deleted from the working directory, It’s no longer present to be tracked.
➤ If we want to add all new, modified, and deleted files in the staging for commit.
git add -Agit add –all
If we want to ensure that deleted files are also included in the commit, we use this command.
- git status
git statusThis command displays information about the current state of our working directory. It shows all modified, staged and deleted files. By default, it provides a detailed descriptive output.
we want to display information in shorter form, we have to pass -s or –short option
git status -sgit status –short
- git rm
This command removes files from the working directory and the staging area. We have to pass the valid options for working with it correctly.
➤ If we want to remove the file from the working directory, even if it has local modification.
git rm -f [file_name]git rm –force [file_name]
➤ If we want to remove the file from the staging area, but keep it in the working directory.
git rm –cached [file_name] - git check-ignore
git check-ignoreThis command is used to check if git tracks a file or folder or not. Sometimes we want to ignore some files and folders to be tracked and not to save them to the git repo. In that case, we create a .gitignore file in the root level in the working directory.
In the .gitignore file, we can mention files and folders which, we don’t want to be tracked.
- git commit
git commit -m “[message]”It commits the changes that we have added to the staging area. It takes the descriptive message of our commit. Before committing, we have to ensure that we have added our changes to the staging area.
git commit -m “initial commit”➤ If we want to add our changes to the staging area and commit them at the same time, we use -am option
git rm -am “[message]” - git log
git logIt displays the commit history of a git repository. By default, it shows all the history descriptively, but you can modify it by passing the options.
➤ Shows all the history in the short form
git log –oneline➤ Filters commits by author
git log –author=<pattern>Examples :
• Matching the exact author – git log –author=”David Miller”
• Matching any author starting with “David” – git log –author=”David.*”
• Matching any author containing the string “jef” – git log –author=”.*jef.*”
• Matching any author with a specific email address –
git log –author=”.*@example.com”
➤ Filters commits whose commits messages match with the specific pattern
git log –grep=<pattern>Examples :
• Matching the exact commit message – git log –grep=”bug fix”
• Matching commit messages starting with “bug” – git log –grep=”^bug”
• Matching commit messages ending with “bug” – git log –grep=”bug$”
• Matching commit messages containing the string “bug” – git log –grep=”bug”
• Matching commit messages containing either “fix” or “bug” –
git log –grep=”fix|bug”
➤ Filter commits made after the specified date
git log –since=<date>git log –after=<date>
Examples :
• Commits made after 01 Apr 2024 – git log –after=”2024-04-01″
• Commits made since 01 Apr 2024 until the present –
git log –since=”2024-04-01″
➤ Filter commits made before the specified date
git log –until=<date>git log –before=<date>
Examples :
• Commits made before 01 Apr 2024
git log –before=”2024-04-01″ or git log –until=”2024-04-01″ - git diff
This command is used in Git to see the differences between changes, we have made in our files since the last commit or between different branches. It helps us to understand exactly what changed in our woking directory.
➤ If we want to see the changes that haven’t been staged yet. git diff shows the changes we have made in our working directory compared to the staging area.
git diff➤ If we want to see the changes that have been staged. git diff –staged shows the changes we have staged but haven’t committed yet.
git diff –staged➤ If we want to see the changes between our current working directory and the last commit.
git diff HEAD➤ If we want to see the changes between our current working directory and the specific commit. For this, we need the commit hash, we want to compare against.
git diff <Commit_Hash>Example: git diff 98564dd0646acdcab080d84df6cbc819aee6a1d7
➤ If we want to see the changes between two commits.
git diff <Commit_Hash1> <Commit_Hash2>➤ If we want to see the changes between the two branches.
git diff <Branch_Name1> <Branch_Name2>
Commands for Branching and Merging
Branching and Merging allow us to work on an isolated environment in our working directory without affecting the main version of our code. After we finish working we can merge that branch into our main version.
- git branch
git branch <Branch_name>This command has two use cases:
➤ It shows all our local branches in the git repository.
git branch➤ For all branches local and remote.
git branch -a➤ If we pass the branch name then It creates a brand new branch.
git branch new-feature➤ If we want to delete the branch, pass the -d argument along with existing branch.
git branch -d <Branch_name> - git checkout
git checkout <Branch_name>It switches to the specified branch
we can also usegit switch <Branch_name>for switching to the specified branch
➤ If we pass the argument -b, It will create a new branch for us and switch to that branch
git checkout -b <Branch_name> - git merge
git merge <branch_name>It merges the specified branch into the current branch.
Consider, that we have created a new-feature branch, and now we want to merge all the changes of new-feature branch to the main branch. For that, in the main branch, we have to write the following command –
git merge new-featureSometimes git encounters merge conflicts during a merge, merge conflict occurs, If there are changes in the same line in the file and Git is unable to merge it automatically. We have to resolve them manually.
After resolving the conflict we have to add the conflicted file into staging area and continue the merging process.
git add <conflicted-file>
git merge –continue➤ If we want to abort the merge and restore the state to its previous version, pass the –abort attribute
git merge –abort
Commands for interacting with remote repositories
- git push
git pushIt pushes our local commits to a remote repo. It is used for publish our local changes to the remote repository, making them available to others to use.
- git fetch
git fetchIt fetches changes from a remote repository to our local repository without merging them into your current branch.
- git pull
git pullIt fetches changes from a remote repository to our local repository and merge them into your current branch. It’s a combination of two Git commands: git fetch and git merge. First, it fetches the changes from the remote repository using git fetch, and then it merges those changes into your local branch.
Commands for temporary save the changes
- git stash
git stashTemporarily save the modified and staged changes in the working directory, allowing us to switch to another branch or perform other tasks without committing the changes.
- git stash list
git stash listList all the saved changes.
- git stash apply
git stash applyIt applies the most recently saved changes to the working directory without removing them from the stash.
- git stash pop
git stash popIt applies the most recently saved changes to the working directory and removes them from the stash.
- git stash drop
git stash dropIt drops the most recently saved changes from the stash.
➤ If we want to drop a specific stashed item, pass the stash ID –
git stash drop <stash_id>
Commands for configuring a git workspace
- git config
➤ Set User Name and Email
git config –global user.name “<your name>”
git config –global user.email “<your email>”➤ Alias a command
git config –global alias.<alias_name> <command_name>It creates a shortcut or alias for frequently used Git commands. This is an extremely useful configuration for productivity.
Examples –
git config –global alias.co checkout
git config –global alias.ci commit
Conclusion
We can’t cover all git commands in a single article, but these are the most useful ones. I hope you found them helpful. Any suggestions or corrections are appreciated.
References
Source: hashnode.com