Skip to content

Instantly share code, notes, and snippets.

@marcelombc
Last active February 7, 2024 10:46
Show Gist options
  • Save marcelombc/c65ab84459473a519a5d to your computer and use it in GitHub Desktop.
Save marcelombc/c65ab84459473a519a5d to your computer and use it in GitHub Desktop.

COMMIT

Remove last commit

git reset HEAD^  # remove commit locally
git push origin +HEAD  # force-push the new HEAD commit

Remove last commit but keep files

git reset <commit_hash>
git push -f

Merge stashed changes with current changes

git stash show -p|git apply
git stash drop

Revert changes to modified files

git reset --hard  or  git reset --hard <commit_hash>
git push -f

TAG

List tags

git tag -l

Fetch tags from repo

git fetch --tags or git fetch --tags --force to override everything in local

Create a tag

git tag <tag_name>

Tag a specific hash

git tag -a <tag_name> <hash> -m "<tag_message>"

Push all tags to remote repository

git push --tags

Push only one tag to remote repository

git push origin tag <tag_name>

Update a tag on local filesystem

git fetch --tags
git tag -f <tag_name>

Update a tag on remote repository

git push -f origin <tag_name>

Remove a tag

git tag -d <tag_name>
git push origin :refs/tags/<tag_name>

Get tag that points to a specific hash

git tag --points-at <commit_hash>

BRANCH

Create a branch

git checkout -b <branch_name>

Create a branch from tag

git checkout -b <branch_name> <tag_name> or git checkout -b <branch_name> tags/<tag_name> if you have a branch with the same name of a tag

Push a branch to the remote repository

git push origin <branch_name>

Change to a give branch

git checkout <branch_name>

Delete a branch on your local filesystem

git branch -d <branch_name>

Force to delete a branch on your local filesystem

git branch -D <branch_name>

Delete a branch on the remote repository

git push origin :<branch_name>

Deletes the whole history on the remote repository for your branch with the history of the orphan branch

git push origin +<orphan_branch_name>:<branch_name>

Create a orphan branch

cd repository
git checkout --orphan orphan_name
git rm -rf .
rm '.gitignore'
echo "#Title of Readme" > README.md
git add README.md
git commit -a -m "Initial Commit"
git push origin orphan_name

GITHUB PAGES

https://gist.github.com/cobyism/4730490

SUBMODULES

Remove submodule

git submodule deinit <asubmodule>    
git rm <asubmodule>
# Note: asubmodule (no trailing slash)
# or, if you want to leave it in your working tree
git rm --cached <asubmodule>
rm -rf .git/modules/<asubmodule>

Sync submodule

git submodule sync
git submodule update --init --recursive --remote

STASH

Save to stash

git stash

List stash

git stash list

Pop last stash

git stash pop

Pop specific stash

git stash apply stash@{n}

Diff last stash

git stash show -p

Diff a specific stash

git stash show -p stash@{n}

UPDATE

Update remote url

git remote set-url origin <new_url>

REBASE

git checkout <feature_branch>
... (commit and push your changes)
git pull origin master -r
(git checkout HEAD -- <files>) if any conflicts, resolve with HEAD
git push origin <feature_branch> --force-with-lease

https://dev.to/shostarsson/the-git-rebase-workflow-2g49


How do I create a new github repo from a branch in an existing repo?

  1. Create the new_repo in github
  2. cd to your local copy of the old repo you want to extract from, which is set up to track the old_branch that will become the new_repo's master
  3. git push git@github.com:accountname/new_repo +old_branch:master

How to clean history

https://gist.github.com/CrookedNumber/8964442

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment