Github
Git Cheat-Sheet
Git bash basic command
1. cd is used to change directory.
2. pwd helps u to show your present working directory.
3. q is used to quit or exit from bash terminal.
4. touch filename is used to create a new file
5. ls help us to list all the files in that directory.
6. clear is used to clear the bash terminal
git init
Helps us to initialize a git in the current directory.
git clone
The “clone” command downloads an existing Git repo to your local computer.
<repository>
Specify the URL of the remote repository.
git add
The “add” command marks changes to be included in the next commit.
<file-path>
Specify the files you want to add to the Staging Area.
— a
recursively added all files of the current directory.
. (dot)
recursively added all files of the current directory.
-u
stage (modified and deleted) files.
* (asterisk)
add all files in current dir, except for files whose name begins with a dot.
git status
The “status” command helps you understand the current state of your repo.
git commit
The “commit” command is used to save your changes to the local repo.
-m <message>
Sets the commit’s message.
-a
Includes all currently changed files in this commit.
--amend
Update the last commit message.
-a -m " message "
direct commit to tracked file.
git push
The “push” command is used to publish new local commits on a remote server.
— all
Pushes all local branches.
— tags
Pushes all local tags.
— delete
Deletes the specified remote branch.
-u origin master
is useful when publishing a local branch on a remote for the first time.
git pull
The “pull” command is used to download and integrate remote changes.
git rm
The “rm” command helps you to remove files from a Git repository.
<filename>
The name of a file (or multiple files) you want to remove.
— cached
Removes the file only from the Git repo, but not from the filesystem.
git restore
The “restore” command helps to unstage or even discard uncommitted local changes.
<filename>
The name of a file (or multiple files) you want to restore.
-- stage <filename>
Removes the file from the Staging Area, but leaves its actual modifications untouched.
— source <ref>
Restores a specific revision of the file.
— patch
Allows you to select individual chunks to restore.
git switch
The “switch” command allows you to switch your current HEAD branch. It’s relatively new (added in Git v2.23) and provides a simpler alternative to the classic “checkout” command.
<branch-name>
The name of a local or remote branch that you want to switch to.
-c <new-name>
The name of a new local branch you want to create.
<branch-name> — discard-changes
Switch to the specified branch and discard any local changes to obtain a clean working copy.
- (dash)
Switch back to the previous branch. When specifying just the “-” character instead of a branch name, Git will switch back to the last checked-out branch.
git remote
The “remote” command helps you to manage connections to remote repositories.
-v
Shows URLs of remote repositories when listing your current remote connections.
add <shortname> <url>
Creates a new connection to a remote repository.
remove <name>
Disconnects the remote from the local repository.
rename <old-name> <new-name>
Renames the remote connection.
git revert
The “revert” command helps you undo an existing commit.
<commit-hash>
Specify the commit that you want to undo.
— no-commit
Do not directly commit the created changes
— no-edit
Use the default commit message that Git suggests.
git branch
The “branch” command helps you create, delete, and list branches.
<branch>
Create a new branch called a branch.
-d <branch>
Delete thrspecified branch.
-D <branch>
Force delete the specified branch, even if it has unmerged changes.
-m <branch>
Rename the current branch to branch
-a
List all remote branches
git checkout
The “checkout” command can switch the currently active branch — but it can also be used to restore files.
<branch-name>
The name of a local branch that you want to switch to.
-b <new-branch>
Creates a new local branch and directly switches to it.
master
To go back to the master branch
<file-path> <commit-hash>
Restores a historic revision of a given file.
git merge
The “merge” command lets you take the independent lines of development created by the git branch and integrate them into a single branch
— no-ff
Creates a merge commit even when a fast-forward would be possible.
— squash
Combines all integrated changes into a single commit, instead of preserving them as individual commits.
— abort
When a conflict occurs, this option can be used to abort the merge and restore the project’s state as it was before starting the merge.
git log
The “git log” tool allows you to view info about previous commits that have occurred in a project.
-p
This command helps us to get info about which file is added or removed from the current directory
— stat
will give all the stat of all add or delete done in the directory.
--pretty=oneline
all commits will show in online only.
--since=<duration>
u will get the commit as per your mentioned duration eg 2days/1week/1year.
git config
The “config” command is a convenience function that is used to set Git configuration values on a global or local project level.
git config levels and files
Before we further discuss “git config” usage, let's take a moment to cover configuration levels. The “git config” command can accept arguments to specify which configuration level to operate on. The following configuration levels are available:
- --local
By default, “git config” will write to a local level if no configuration option is passed. Local-level configuration is applied to the context repository “git config” gets invoked in.
- --global
Global level configuration is user-specific, meaning it is applied to an operating system user.
- --system
System-level configuration is applied across an entire machine. This covers all users on an operating system and all repos. The system-level configuration file lives in a gitconfig file off the system root path.
--list
list of all configuration
<--global/--local/--system> user.name
displays the username
<--global/ — local/ — system> user.email
displays the user mail id
<-- global> alias.<aliasname> <originalgitcmd>
To set an alias name globally.
rm -rf .git
This “rm -rf .git” command helps us to remove the initialized git repo.