Last active
October 28, 2023 18:29
-
-
Save FilippoBovo/2dd19c515d1ce4bc9ad6cac6dd21cc14 to your computer and use it in GitHub Desktop.
Git Commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git reset --soft HEAD~1 # Use HEAD~2 to amend the last 2 commits | |
# Amend files | |
git add . | |
git commit -m "<commit_message>" | |
git push --force |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GENERAL | |
git init # Initialise Git repository | |
git status | |
git add <file> # Add file to staging index | |
git add . # Add all files in current directory to staging index | |
git commit -m <message> # Commit changes | |
git commit -am <message> # = git add . + git commit -m 'message here' | |
git fetch --prune # Synchronise the remote branches from the online repository | |
# LOGS | |
git log # See commit logs | |
git log -n 5 # Last 5 commits | |
git log --oneline # Compact format | |
git log --since=2016-08-09 | |
git log --until=2016-08-09 | |
git log --since="2 weeks ago" | |
git log --since=2.weeks | |
git log --until=3.days | |
git log --author="Name" | |
git log --grep="BlaBla" # Filter for word BlaBla | |
git log --graph --oneline --all --decorate # Nice view of branch history | |
git log -p # Show actual changes | |
git shortlog # Summarise logs and show authors | |
# UNDOING CHANGES | |
git checkout -- <file> # Undo changes | |
git reset HEAD <file> # Unstage files | |
git restore --staged <file> # Unstage files | |
git commit --amend -m 'Commit message' # Amend last commit | |
git checkout <SHA> -- <file> # Retrieve old version, eg SHA=c4b913ef | |
git revert <SHA> # Reverting to commit c4b913ef | |
git reset --soft <SHA> # Only move HEAD pointer to commit c4b913ef | |
git reset --mixed <SHA> # ^-- and change staging index to match repository | |
git reset --hard <SHA> # !!Caution!! ^-- and modify working directory to match repository | |
git clean -n # List untracked files that will be removed | |
git clean -f # Remove untracked files | |
# CHANGES TO FILES | |
git rm <file_to_delete> | |
git mv <original_file> <new_file> | |
git rm --cached <file> # Untrack files | |
# NAVIGATION | |
git ls-tree <tree-ish> # Explore tree listings - For <tree-ish>, see below | |
# • Tree-ish: something that references part of the tree | |
# • full SHA-1 hash | |
# • short SHA-1 hash | |
# • at least 4 characters for small projects | |
# • 8-10 characters to be unambiguous | |
# • HEAD pointer | |
# • branch reference (this would refer to the tip of the branch) | |
# • parent commit (HEAD^, acf87504^, master^, HEAD~1, HEAD~) | |
# • grandparent commit (HEAD^^, acf87504^^, master^^, HEAD~2) | |
# • great-grandparent commit (HEAD^^^, acf87504^^^, master^^^, HEAD~3) | |
# • HEAD: | |
# • pointer to "tip" of current branch in repository | |
# • last state of repository (what was last checked out) | |
# • points to parent of next commit | |
# • where writing commits takes place | |
# COMPARING COMMITS | |
git diff # Show changes | |
git diff --staged # Show only staged changes | |
git diff <SHA_from> # Show changes since commit 2907d12 | |
git diff <SHA_from>..<SHA_to> # Show changes between commits SHA_from and SHA_to | |
git diff <SHA_from>..<SHA_to> <file> # ^-- Only for selected file | |
git diff --stat --summary <SHA_from>..<SHA_to> # Snapshop view | |
git diff -b # = git diff --ignore-space-change | |
git diff -w # = git diff --ignore-all-space | |
# BRANCHING | |
git branch # Show branches in local repository | |
git branch <branch_name> # Create new branch | |
git checkout <branch_name> # Switch to new branch | |
git checkout -b <branch_name> # Create and switch to new branch | |
git diff <first_branch>..<second_branch> # Comparing branches | |
git diff --color-words <first_branch>..<second_branch> # ^-- colored words format | |
git branch --merged # Show all branches that are completely included in current branch | |
git branch --move <old_branch_name> <new_branch_name> # Rename branch | |
git branch -d <branch_to_delete> # !!Caution!! Delete merged branch | |
git branch -D <branch_to_delete> # !!Caution!! Delete unmerged branch | |
# MERGING | |
git merge <branch_to_get_changes_from> | |
git merge --no-ff <branch_to_get_changes_from> # Fast-worward merge | |
git merge --ff-only <branch_to_get_changes_from> # Only if fast-worward merge can be done | |
git merge --abort | |
git mergetool # View merge tools | |
# STASHING | |
git stash save <message> | |
git stash list | |
git stash show stash@{<stash_number>} | |
git stash show -p stash@{<stash_number>} # Show changes | |
git stash apply # Retrieve | |
git stash pop # Retrieve and remove from stash | |
git stash drop stash@{<stash_number>} # Remove from stash | |
git stash clear # !!Caution!! Delete all stashes | |
# REMOTES | |
# Here, <alias> is the remote name, like "origin". | |
git remote # Show all remotes | |
git remote -v # Show remote url | |
git remote show origin # Show remote origin branch | |
git remote set-url origin <url> # Change the remote url | |
git remote add <alias> <url> | |
git remote rm <alias> | |
git push -u <alias> <branch> | |
git branch -r # Show remote branches | |
git branch -a # Show local and remote branches | |
git clone <url> # Eg, repository = https://github.com/coolperson/coolproject.git | |
git push <alias> <branch> | |
git fetch <alias> # Update local data; no change to working dir | |
git merge <alias>/master # Merge local version of <alias>/master with current branch | |
git merge --squash <alias>/master # Like command above, but squashes commits into single one | |
git rebase -i HEAD~3 # Rebase the last three commits. This can be used to squash the last three commits into a single one. | |
git pull # = git fetch + git merge | |
git branch <branch_we_are_creating> <alias>/<branch_we_are_checking_out_from> | |
git checkout --track <alias>/<branch_we_are_checking_out_from> # this will copy a remote branch to a local branch | |
git push <alias> :<remote_branch_to_delete> # !!Caution!! Delete remote branch | |
git push <alias> --delete # !!Caution!! ^-- Same | |
git push --delete <alias> <remote_branch_to_delete> # !!Caution!! ^-- Same | |
git remote update origin --prune # Purge remote branches in the local machine | |
# REBASING | |
git branch <branch_name>_backup # Create a backup of the branch before rebasing in case there are issue when rebasing. | |
git rebase <branch_to_rebase_on> # Rebase the current branch on the branch_to_rebase_on. Rebasing requires force push. | |
git rebase -i <branch_to_rebase_on> # Interactive rebase the current branch on the branch_to_rebase_on. Rebasing requires force push. | |
git rebase --onto <last_commit_of_squashed_base> <last_commit_of_base> # Rebase the branch on a base branch after the base branch has been squashed or rebased. See: https://stackoverflow.com/questions/35901915/how-to-rebase-after-squashing-commits-in-the-original-branch | |
# SOURCES | |
# • https://www.lynda.com/Git-tutorials/Git-Essential-Training/100222-2.html | |
# • https://github.com/timothychen01/comprehensive-git-cheatsheet |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
git update-index --assume-unchanged path/to/file.txt | |
# Revert back | |
git update-index --no-assume-unchanged path/to/file.txt | |
# See https://help.github.com/articles/ignoring-files#ignoring-versioned-files |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# If you want to merge project-a into project-b: | |
cd path/to/project-b | |
git remote add project-a path/to/project-a | |
git fetch project-a | |
git merge --allow-unrelated-histories project-a/master # or whichever branch you want to merge | |
git remote remove project-a | |
# https://stackoverflow.com/a/10548919 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment