Skip to content

Instantly share code, notes, and snippets.

@tbotalla
Last active November 24, 2020 18:44
Show Gist options
  • Save tbotalla/be7d10afa682b5c659bd7d10b7ba8751 to your computer and use it in GitHub Desktop.
Save tbotalla/be7d10afa682b5c659bd7d10b7ba8751 to your computer and use it in GitHub Desktop.

See git help of a specyfic command

git help stash

Delete old already pushed commits

git reset --hard HEAD~x // Elimina los ultimos x+1 commits
git push -f

Revert a commit

git revert <SHA_COMMIT> // Creates a revert commit for the specyfied commit
git push // Push the revert commit to the remote

Init Git repository from existing sources

  • First create it on Github or other provider the repository
git init
git add *
git commit -m "<DESCRIPCION>"
git remote add origin git@github.com:tbotalla/<NOMBRE_DEL_REPO>
git push -u origin master

Delete local and remote branch

git push --delete <remote_name> <branch_name>
git branch -d <branch_name>
git branch -D <branch_name> # Force delete of branch

Rename branch

1. Rename your local branch.
If you are on the branch you want to rename:
git branch -m new-name
If you are on a different branch:
git branch -m old-name new-name
2. Delete the old-name remote branch and push the new-name local branch.
git push origin :old-name new-name
3. Reset the upstream branch for the new-name local branch.
Switch to the branch and then:
git push origin -u new-name

Modify a commit message

git commit --amend

Working with tags

git tag [tag_name]
git tag -a v-1.0 # Create an annotated tag
git tag v-1.2 -m "Release 1.2" # Create an annotated tag with description
git push origin v-1.0 # Push specyfic tag
git push origin master --tags # Push local tags to remote origin
git tag --list # See tags
git show [tag_name] # Show tag
git tag --delete [tag_name] # Delete tag
git push origin :v-1.0 # Delete an already pushed tag on remote (push nothing to that tag)
git difftool v-1.0 v-1.1 # Comparte two tags
git tag -a v-0.9-beta 96ef2812 # Create a tag for specyfic commit
git tag -a v-0.9-beta 9689516a # Associate an existing tag to different commit

Working with stashes to save work in progress

git stash save "my_stash"
git stash list
# To apply a stash and remove it from the stash stack, type:
git stash pop stash@{n}
# To apply a stash and keep it in the stash stack, type:
git stash apply stash@{n}
git stash drop # Drops last stash
git stash show stash@{0} # See files modified on a specyfic stash
git stash -u # Stashes including the untracked files on working directory, not only the files tracked by git
git stash clear # Remove all saved stashes
git stash branch newbranch # Creates a new branch 'newbranch', switches to it, and applies last stash to it

Open .gitconfig file to see global git configurations

git config --edit --global

Configure diff and merge tool

git config --global -e
[merge]
        tool = p4merge
[mergetool "p4merge"]
        cmd = p4merge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
        trustExitCode = true
[mergetool]
        prompt = false
[diff]
        tool = p4merge
[difftool "p4merge"]
        cmd = p4merge "$LOCAL" "$REMOTE"
[difftool]
        prompt = false

Compare working directory vs staging area vs repository

  • Working directory vs Staging Area
git diff
git difftool
  • Working directory + Staging Area vs Repository (.git)
git diff HEAD
git difftool HEAD
  • Staging Area vs Repository (.git)
git diff --staged HEAD
git difftool --staged HEAD

Compare the HEAD and the commit before that one

git diff HEAD HEAD^
git difftool HEAD HEAD^

Compare local and remote branches

git diff master origin/master
git difftool master origin/master

Configure Git name & email

git config --global user.name "Juan Roman"
git config --global user.email "jrr10@gmail.com"
git config --global --list

Configure text editor (Visual Studio Code)

git config --global core.editor "code --wait"

See files tracked by git on repository

git ls-files

Unstage/revert local changes added to staging area

git reset HEAD file.txt

Revert to clean working directory

git checkout -- file.txt

Rename a file tracked by git

git mv file1.txt file.txt
# If renamed without git mv:
git add -A # this will make Git detect the renaming and deletions

Rename a file tracked by git

git rm file.txt

Show log history nicely

git log --oneline --graph --decorate
git log --all --graph --decorate --oneline
git log --since="3 days ago" # Show commits of last 3 days

Show details of a commit

git show 5c05047

Create alias for other git commands

git log --all --graph --decorate --oneline # Want an alias for this command
git config --global alias.hist "log --all --graph --decorate --oneline # Create an alias 'git hist' for previous command
git hist # Use it like this
git config --global -e # To see the created alias
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment