Skip to content

Instantly share code, notes, and snippets.

@jaimemrjm
Last active November 29, 2023 14:54
Show Gist options
  • Save jaimemrjm/2638461d2ed9ed9524413765ef17a95c to your computer and use it in GitHub Desktop.
Save jaimemrjm/2638461d2ed9ed9524413765ef17a95c to your computer and use it in GitHub Desktop.
my git tips

My git tips

Update repo and submodules

git pull && git submodule update --init

Update repo if error fatal: Cannot rebase onto multiple branches.

git pull origin <branch> --rebase 

Show remote repo

git remote show origin

Repo reset at all

Throw away all my staged and unstaged changes, forget everything on my current local branch and make it exactly the same as origin/master:

git reset --hard origin/master

Push my changes

git push origin master

Create branch from PR and/or checkout PR

See the header message "user wants to merge x commits into master from branch_name": git checkout <branch_name>

Create my branch from existing branch (by default: master branch)

git checkout -b <my_branch_name>

Restore file from HEAD

git checkout @ -- <file>

Discard (undo) git add file or directory

git reset HEAD -- <file_or_directory>

Discard local commit and keep changes

git reset HEAD^

Discard all local changes

git reset --hard

Discard all local commits

git reset --hard origin/master

Discard untracked changes

git stash save --keep-index --include-untracked

Check differences between my branch and master

Only for one file: git diff <my_branch> master -- <file>

For all files: git diff --stat --color master..my_branch

Replace file from another branch

git checkout <branch_name> -- <file>

Find commits about a deleted file

git log --full-history -- "<path/file>"

Add --all to search in all branches.

Ignore some files without add to .gitignore

Add filenames in file:

.git/info/exclude

Merge (rebase) from branch, for example, master

git merge <branch>

Note: check the button Update branch at the end of the PR at github to do easily.

Force End of Line to UNIX-format

git config --global --edit

-> set autocrlf = false

Show End of Line of files

git ls-files --eol

In the output, i/lf means that the file uses lf (UNIX format) in the index, and w/crlf (Windows format) means it uses crlf in the working directory.

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