Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

GergesHany/git-commands-cheat-sheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 

Repository files navigation

A Cheat Sheet for Git Commands

sessions to learn git


Content


Install Git


Linux users

Open your terminal and run these commands

sudo apt update
sudo apt upgrade
sudo apt install git

Windows and Mac users


Git Terminologies

Git Command Description
Bare Repository Repository without working directory
Branch A branch is an active area of development in Git. The most recent commit shows the tip of the branch.
Blame Describes the last modification to every line in the file. Shows Revision, Author & Time.
Checkout This refers to the process in which any given commit is selected from the repository and the state of the associated file and the directory tree is recreated in the working directory.
Commit This is a single point in Git history which holds the information about a changeset.
diff A diff is the difference in changes between two commits, or saved changes
Detached Head This exact state - when a specific commit is checked out instead of a branch - is what's called a "detached HEAD"
Fetch Fetching means to get latest changes in the branch and the local/remote repos.
Fork By forking the repository you will be able to add commits and create Pull Requests
Hash A unique SHA1 code for every commit
Head A named reference to the Commit at the tip of a Branch
Index A collection of files with state information.
Merge To bring out the content of another Branch in the current Branch.
Master The default development Branch in Git
Origin The default Upstream Repository
Pull Request Suggests changes into the Master Branch
Push Pushes new changes after Committing them
Repository A collection of Commits, Branches and Tags to identify Commits.
Working Tree The tree of actual checked out files

Git Configuration

Git Command Description
git config --global user.name "Your Name" Set the username to be used for all actions
git config --global user.email "Your email" Set the email to be used for all the actions.
git config --global alias.< alias-name >< git-command > Create a shortcut for the Git command.
git config --system core.editor < editor > Set the text editor for all the command actions.
git config –global –edit Open global configuration file in the text editor for manual editing.
git config –global color.ui auto Enable helpful colourization of command line outputs.

Set Up a Git Repository

Git Command Description
git init Initialize an empty Git repo in the current project.
git clone (Repo URL) Clone the repository from GitHub to the project folder.
git clone (Repo URL) (Folder ) Clone the repository into a specific folder.
git remote add origin (https://github.com/username/(repo_name).git) Create a remote repo pointing on your existing GitHub repository.
git remote Shows the name of remote repositories.
git remote -v Shows the name and the URL of the remote repositories.
git remote rm (remote repo name) Removes the remote repository.
git remote set-url origin (git URL) Changes the URL of the repository.
git fetch Get the latest changes from the origin but not merge.
git pull Get the latest changes from the origin and merge them.

Local File Changes

Git Command Description
git add (file name) Add the current changes to the file to staging.
git add . Add the whole directory changes to staging (no delete files).
git add -A Add all new, modified and deleted files to staging.
git rm (file_name) Removes the file and untracks (stop tracking) it.
git mv (file_name) (new_file_name) Changes the filename and prepare it for Commit.
git checkout < deleted file name > Recovers the deleted file and prepares it for Commit
git status Shows the status of the modified files.
git ls-files –other –ignored –exclude-standard Shows the list of all ignored files.
git diff Shows unstaged changes in the index and the working directory.
git diff –staged Shows file differences between staging and the last file version.
git diff (file_name) Shows changes in a single file compared to the last Commit.

Declare Commits

Git Command Description
git commit -m “(message)” Commit the changes with a message
git commit -am “(message)” Add all changes to staging and commit them with message
git checkout < commit hash> Switch to the provided commit
git show < commit hash> Outputs metadata and content changes of the specified commit
git reset < commit hash> Undo all commits after this commit. Preserving changes locally
git reset --hard < commit hash> Discard all history and changes back to the given commit
git reset --hard Head Discard all local changes in working directory
git log Show history of changes
git log -p Shows the full display of each commit
git log -oneline Shows the list of commits with only message
git log --follow (file_name) List all the history for the current file
git blame (file_name) Shows all changes with name of user
git stash Temporarily saves all modified tracked files
git stash pop Restores the most recently stashed files
git stash list List all stash changedsets
git stash apply Apply the latest stashed contents
git stash drop Discard the most recently stashed files
git stash apply (stash id) Re-Apply a specific stash content by ID
git stash drop (stash_id) Drop a specific stash content by ID
git push Push changes to the Origin
git push origin (branch_name) Push branch to the Origin
Git push -f origin (branch_name) Force push the changes to the origin
git tag (tag_name) Define tag for a version

Branching

Git Command Description
git branch Shows the list of all branches
git branch <branch_name> Create a new branch
git branch -a List all branches local and remote
git branch -m <old_name> <new_name> Rename the branch
git checkout -b <branch_name> Create a branch and switch to it
git checkout -b <new_branch_name> origin/<branch_name> Get a remote branch from origin to the local directory
git branch -D <branch_name> Delete a specified branch
git merge <branch_name> Merge the current branch into master(first checkout to master)
git rebase <branch_name> Take all the changes of the branch and restate on other.
git rebase Rebase the current branch onto base. Base can be a commit ID or branch name.
git fetch remote <branch_name> Fetches the specific branch from the repository
git diff <branch_name>..<branch_name> Shows the differences of two branches
git pull --rebase Fetch the remote’s copy of current branch and rebases it into the local copy
git push --all Push all of your local branches to the specified remote



Stash

Stash the changes

  1. git stash
  2. git stash save

Stash the untracked files

  1. git stash --include-untracked
  2. git stash -u

List the stashes

  1. git stash list

show the latest stash

  1. git stash show
  2. git stash show -p
  3. git stash show -u
  4. git stash show --include-untracked
  5. git stash show -p -u
  6. git stash show -p --only-untracked

Show the individual stash

  1. git stash show stash@{1}
  2. git stash show stash@{1} -p
  3. git stash show stash@{1} -u
  4. git stash show stash@{1} --include-untracked
  5. git stash show stash@{1} --only-untracked

Apply the stash

  1. git stash pop
  2. git stash apply
  3. git stash apply stash@{1}

Delete the stash

  1. git stash clear
  2. git stash drop stash@{2}

Create branch from stash

  1. git stash branch <branch_name>
  2. git stash branch <branch_name> stash@{revision}

About

A Cheat Sheet for Git Commands

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published