What is GIT?
GIT is an open-source repository where users can place their files, including code, zips, text, or the entire project. It has several features, like collaboration, version control, process control, etc. GIT stores file more efficiently and provide better integrity than other version control systems. Git is a command-line tool.
What is GitHub?
With its neat user interface, GitHub, a hub for developers, has become the most widely used version control system and can be accessed by anyone, anywhere.
Git Workflow
Git has different workflows, and every project should have one based on its requirements. Git workflow is nothing but a lightweight, branch-based workflow that helps teams that deploy projects regularly. For example, a forking Git workflow creates a local copy of the repository. So, each developer has a local workspace and a central repository. This kind of workflow is good when multiple developers are involved and allows developers to collaborate on the forked copy before making changes to the main repository.
Why do we need Git?
As Git is a version control system, developers constantly work on it to save their changes, make them viewable for others, merge their changes with others, and much more. For any changes to the Git repository, developers need to know the right commands for the right action. Below is a list of the important and commonly used Git commands:
Common Git Commands
git add
To add the specified file(s) to the staging
git archive
Creates archives of commits, branches, and trees and combines multiple files into one. You can retrieve the contents by extracting the files when needed git archive HEAD — create an archive from the HEAD ref of the repo git archive output = ‘.tar’ — store the archived files in the given location git archive –format=tar.gz — specifies the format of the archived file, like tar, zip, tar.gz
git branch
Lists all the branches in the repository. A new branch with the name will be created if a branch is specified. To delete a branch, use the -d option with the branch name. To rename a current branch, use -m.
git branch — lists all branchesgit branch <branch_name> — creates a new branch named ‘branch_name’ but doesn’t check out the new branch.git branch -d <branch_name> — deletes the branch, prevents the delete if any unmerged changes are theregit branch -D <branch_name> — force delete the branch even if unmerged changes are theregit branch -m
git cat-file
Display content or size/type information of repository objects git cat-file
git checkout
To switch between different branches of a repository, use it carefully, as there is no ‘undo’ for it. git checkout <branch_name> — checks out the specified branchgit checkout -b <branch_name> — create a new branch and switch to this branch For example:
git clean
Clean up the working director. Files that are committed will not be removed git clean -n — lists the files that will be removedgit clean -f — removes the files
git clone
Create a copy of the existing repo into a new directory. Useful to get a development copy of a central repo.
git clone <central_repo> <new_dir> — create a copy of the central repo into a new directory
For example:
git clone -branch
git commit
Saves the changes into a staging environment so that others can see it. git commit — commit the changes to the staginggit commit -m <useful_msg> — gives a message upon commit to highlight the changes madegit commit -a — commit the changes directly, without staging Let’s say you added a file named samplefile.txt to your working directory and want to commit the file. On giving the above command, you will get the output: Once you give the message, the changes are committed:
git config
Specifies the configuration level to write a property value into. The ‘Local’ level is the default (when nothing is specified). git config –local — saves the configuration in the .git directory of the repogit config –global — saves the configuration in the user’s home directorygit config –system — contains the configuration of all users and repositories and is located in the git config file of the root.
git diff
Compare the changes in the git repo, which can be done before staging, at staging, and after staging (commit). git diff — track repo changes that are not yet stagedgit diff –staged — track changes of staged files (which are not committed)git diff HEAD — track file changes after the commitgit diff <commit1_id> <commit2_id> — track changes between two commits; you can find out the commit_ids using ‘git log -p –follow –filename‘
git fetch
Fetch a branch or entire remote repository
git fetch
git fsck
File System ChecK command checks for validity and connectivity of database objects. It checks the SHA-1ID of the objects and the connections they make. Fsck is useful for recovering lost commits and files. git fsck –full
git gc
Runs garbage collection on the current repository and cleans up unused files. git gc
git grep
Searches for specific content in the repository. Git provides many options to search in different ways git grep -i ‘search_term’ — search by ignoring case [Man and man will be the same]git grep -f <file_name> — show matching patterns of a particular filegit grep -e ‘search-term’ — use -e for pattern matchinggit grep -E ‘regexp|multiple_exp’ –Search for regular expressions, we can search multiple by using the pipe (OR) operatorgit grep -n ‘expr’ — prefix line number of the matching linegit grep -c ‘expr’ — show the count of lines that matched instead of each line
git ls-tree
To list the contents of a tree object from the current directory.
git ls -tree -d
git init
Create a new blank repository. It is the first command you execute to make a git project. git init — creates a .git repository in the working directory, For example, to create a new repo called ‘geekflare’, give the command as:
git instaweb
User Interface to browse the git repository through a browser. Uses the CGI script GitWeb for the same. git instaweb –httpd=webrick — starts the server (httpd) and opens the web browser on the page. To stop the server, use the above command with the –stop option.
git log
Records every activity in the git repository.
git log — displays the last few commitsgit log –oneline — shows the output as the first 7 characters of SHA and the commit message with one commit per linegit log stat — displays more information about the modified files, like the number of lines added/removed, a summary of total records changed, lines added/removedgit log –patch (or -p) — shows the files modified, the specific changes, and their locationgit log –graph — View log results in graph formgit log -
git merge
Integrates all the development files into a single branch, combines two branches, and merges multiple commits into a single history. The merge stops if there is a conflict, and git presents the conflict files. Once the conflicts are resolved, the merge continues.
git checkout -b — first, checkout the branch to merge
git add
git prune
Deletes (Prunes) the files that are unreachable from the current branch. It is a cleaning process to remove unused files from the branch git prune -n — don’t prune, just show what all can be prunedgit prune -v — shows the output of actions done by the prunegit prune –progress — shows the progress of the prunegit fetch –prune — prunes all the outdated branches
git pull
It receives data from a remote server to a working repository. It updates the local (working) branch with all the latest files from the remote repo.
git pull — pull the remote repogit pull
git push
Pushes all the local changes into the remote repository. It is an upload process exactly opposite to the pull and fetches commands
git checkout master — checkout the branch that has the latest changes
git push origin master — push the changes to the remote repo
Example:
We can also use push to delete a remote branch using the command git push –delete
git rebase
Combines multiple commits from different branches to create a new final base commit. Useful before merging all the changes, to commit changes from different branches one by one (linearly).
git rebase
git remote
Checks the configuration of the remote server and allows connection access between remote and local.
git remote — by default, it returns ‘origin’, the default name of the remote server given by Gitgit remote -v — lists short names and URLs of all the available remote connectionsgit remote add
git reset
Go back to a previous commit, and discard changes made after that commit
git reset
git rm
Removes the specific file from git. You can undo the effect of rm using the reset or checkout command git rm <file_ name> — remove the specific file To remove files from git but keep them in your local (staging), use: git rm –cached
git show
View any object such as blob, tree, commit, or tag by specifying its SHA1
git show — not specifying the
git stash
To switch branches without committing in the current branch, store the non-committed data safely
git stash — saves the work and index stategit stash save
git status
View the status of the repository and staging, i.e., the status before the commit stage. You can use this command after any other git command, like adding, updating, or removing a file git status — shows the changes that are to be committed, or untracked (no staged) changes For example, if you add a file in your working directory named samplefile.txt and check if it’s added, you can give the above command. It will result in the output as follows:
git tag
Friendly references are used to indicate milestones or reference points in the code git tag <tag_name> — create a tag with the given namegit tag — list all the available tagsgit tag show <tag_name> — show details of the specified taggit tag -l “.*” — show tags that match the specified pattern or characters
gitk
Launches the git user interface that displays the contents, commits, full diff, and other details in a window gitk — open the git repo in a window for visualization
git version
Use the git version command to check the version of the git you are using.
Final Words
This post has listed the most commonly used git commands with their options. Next, you can check out the GitHub credentials scanner.