Skip to content

Instantly share code, notes, and snippets.

@NearHuscarl
Last active August 1, 2018 09:30
Show Gist options
  • Save NearHuscarl/40321955e177ba675fd8b68f45b0d6ad to your computer and use it in GitHub Desktop.
Save NearHuscarl/40321955e177ba675fd8b68f45b0d6ad to your computer and use it in GitHub Desktop.
some useful git commands

Change from https to ssh

git remote -v
# origin  https://github.com/NearHuscarl/dotfiles (fetch)
# origin  https://github.com/NearHuscarl/dotfiles (push)

git remote set-url origin [email protected]:NearHuscarl/dotfiles.git

git remote -v
# origin  [email protected]:NearHuscarl/dotfiles.git (fetch)
# origin  [email protected]:NearHuscarl/dotfiles.git (push)

Revert

how to revert disastrous commit

git revert HEAD~2 # revert last 2 commit

Tag

Add tag

git tag -a <tag-name> -m <long-description>

Add tag point to an old commit

# Set the HEAD to the old commit that we want to tag
git checkout 9fceb02

# temporarily set the date to the date of the HEAD commit, and add the tag
GIT_COMMITTER_DATE="$(git show --format=%aD | head -1)" \
git tag -a v1.2 -m"v1.2"

# set HEAD back to whatever you want it to be
git checkout master

Delete tag

git tag -d <tag-name> # local
git push origin -d <tag-name> # remote

Push tag

git push --tags

Hooks

Setup

git config --global core.hooksPath ".hooks"

In /path/to/repo/.hooks/ Create hook file. Example name for hook: .hooks/pre-commit, .hooks/commit-msg

Submodule

Create submodule

git submodule add <url> <name>
# e.g. git submodule add  https://github.com/NearHuscarl/dictionary-data.git Data
git commit -m 'add submodule' # commit immediately after cloning

Update submodule (first time)

git submodule update --init --recursive

Update submodule

git submodule update --recursive --remote

In submodule

git pull

In main repo

git commit -m 'update submodule'
git push

Branch

Create & move to new-branch

git checkout -b <new-branch>

Move to master branch

git checkout master

Delete branch

git branch -d <branch-name>

Merge

Pull content

git stash
git pull
git stash apply --index
  • Store your changes tempoparily in the stash and remove them from current working directory
  • Pull change from other party
  • Restore changes from the stash. (the --index option is useful to make sure that staged files are still staged)

Merge using vim

If auto merge failed. Use vim to resolve merge conflicts

vim path/to/conflict/file

Vim will open 3 buffers:

  • the left one is target branch
  • the midde one is current working copy
  • the right one is merge branch

Some useful commands when merging in vim:

  • ]c and [c to move to next/previous hunk
  • :diffget //2 to merge from target branch
  • :diffget //3 to merge from merge branch
  • :diffupdate to update out-of-sync diff

After solving merge conflict. Close all the buffer except the middle one (working copy) by typing :only in the middle buffer

Note:

  • Target branch is the current active branch (if you do git checkout feature then it's feature)
  • Merge branch is the branch in the git merge argument (if you do git merge master from feature branch, then it's master)

References

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