Table of Contents
- Lots of Git Tips
- Repositories
- Stash
- Commits
- Tags
- Branches
- Stage
- Config
- Log
- Reset/Undo
- Submodules
- Other
- Git Workflows
- https://makandracards.com/makandra/5531-git-move-a-commit-between-repositories
- https://blog.prototypr.io/git-for-beginners-12-commands-you-need-to-know-e084cce9cc94
- https://flaviocopes.com/git-cheat-sheet/
- https://github.blog/2015-06-08-how-to-undo-almost-anything-with-git/
- https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests
git clone https://github.com/username/reponame.git --branch branchname --single-branch --depth 1
Revert shallow clone into full clone.
git fetch --unshallow
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch origin
git remote -v
git branch --set-upstream-to origin/my_branch
git branch -u origin/my_branch
git remote rename upstream origin
git remote add upstream https://github.com/username/reponame.git
git remote set-url origin new.git.url/reponame.git
git remote show origin
Stash specific files (as of Git 2.13)
git stash push -- [filepaths]
- or -
git stash push -m "message" [filepath]
Stash hunks of lines interactively
git stash -p
Stash with custom message
git stash save "Your custom message"
See recent stash:
git stash show -p
git stash show -p stash@{0}
git stash list
git stash show -p stash@{1}
git stash drop
https://gist.github.com/joseluisq/7f0f1402f05c45bac10814a9e38f81bf
git fsck --no-reflog | awk '/dangling commit/ {print $3}'
git stash apply SHA1_OF_STASH
OR
git branch recovered SHA1_OF_STASH
Create patch (mailbox) file (optionally for one specific file):
git checkout sourceBranch
git format-patch -1 SHA1_OF_COMMIT~..SHA1_OF_COMMIT [path/to/file.js]
Apply patch (mailbox) file:
cd toDifferentRepo
git checkout targetBranch
git am pathToPatchFile.patch
Note that the new commit will have a different hash than the original. If you want the hash to remain the same, use git cherry-pick instead.
git rebase --interactive HEAD~3
git commit --amend -m "New commit message"
git add .
git commit --amend --no-edit
Can alternatively use this to interactively pick-out files and hunks of lines to be added: git add -p
git show (shows diff)
git show commitID or git diff commitID^!
Specify a different author for last commit.
git commit -m "msg" --author "John Smith <[email protected]>"
git commit --amend --reset-author
Change date of last commit.
git commit --amend --date="Wed Feb 16 14:00 2011 +0100" --no-edit
git commit --amend --date=now --no-edit
git tag YOUR_TAG_NAME
git tag -f YOUR_TAG_NAME
git fetch origin --tags
git tag origin YOUR_TAG_NAME
git push --tags origin
git tag -d YOUR_TAG_NAME
git push --delete origin YOUR_TAG_NAME
Remote command to delete all tags is first, followed by the command for local tags.
In Linux:
git ls-remote --tags --refs origin | cut -f2 | xargs git push origin --delete
git tag | xargs git tag -d
In Windows PowerShell (remote tags must all exist locally:
git tag | foreach-object -process { git push origin --delete $_ }
git tag | foreach-object -process { git tag -d $_ }
In Windows Batch:
FOR /f "tokens=*" %a in ('git tag') DO git push origin --delete %a
FOR /f "tokens=*" %a in ('git ls-remote --tags --refs origin') DO git push origin --delete %a
git checkout -b <newbranch> origin/<branch>
git switch <branch>
or
git checkout --track origin/<branch>
git checkout -p HEAD~
git branch -u origin/<branch>
git branch -l
git branch -r
git branch -a
https://www.atlassian.com/git/tutorials/merging-vs-rebasing
git pull origin <branch>
git cherry-pick origin/<branch or hash or tag>
git merge origin <branch>
Incorporate upstream changes into branch, moving extra commits to the tip of the branch after synchronising it with the changes from the central repository.
git pull --rebase origin master
Integrating feature/patch/patch from a remote branch - 2 step process:
git fetch <remote-git-url> <branch or sha>
git cherry-pick FETCH_HEAD
git branch -m <NewBranchName>
git diff branch1..branch2
git branch -D <branch>
git push upstream --delete <branch>
git fetch origin
git push origin HEAD
git push origin HEAD:other_branch_name
git push -u origin <branch>
Edit files and then... git add git rebase --continue or get rebase --abort
git status
git status -uno # Don't show untracked files.
git diff
Show diff with staged files:
git diff --cache [filename]
You can also use git diff to compare the stash with any branch.
git diff stash@{0} master
git checkout – <file/folder>
(just delete the directory or file)
git config --global user.name "Full Name"
git config --global user.email "[email protected]"
git config --global log.date iso
git config --global color.ui true
git config --global credential.helper store
git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=600'
git config --local --list
git config --global --list
git config --system --list
git config --global core.fileMode false
git log --oneline
git log --author=partofnane
git log --grep=partofmessage
git log --graph --oneline --decorate
git log --pretty=format:"%h%x09%an%x09%ad%x09%s"
git log --oneline -1 (shows last log)
git cherry -v
git log origin/master..HEAD
git log --stat origin/master..HEAD
git log master --not --remotes --oneline
git log --name-status
git log --follow -- <filename>
git log --follow -p -- <filename>
git show-ref --heads
git shortlog -s -n
git log --graph --oneline --decorate --all
git reflog
git reset HEAD@{1}
git checkout --
git reset --
git checkout --
git reset HEAD <fileToUnstage>
git reset --hard HEAD~
git reset --hard HEAD~5
git reset --soft HEAD~
git reset --soft HEAD~5
Same as undo one or more commits but add a push (note: not a good idea if others are also working on same repo): git push origin HEAD -f
git rm -r --cached file.name
git reset 'HEAD@{1}'
git reset --hard YOUR_TAG_NAME
git revert <sha hash>
git submodules
git submodule add remoteURL destinationFolder
git submodule update --init --recursive .
git config --file .gitmodules --get-regexp path | awk '{ print $2 }'
git submodule update --remote
git submodule deinit submoduleFolder
git rm submoduleFoder
git config --global credential.github.com.useHttpPath true
git rm --cached <filename>
git rm -r --cached <filenpath>
git grep "regex"
- https://www.atlassian.com/git/tutorials/comparing-workflows
- https://nvie.com/posts/a-successful-git-branching-model/
- https://buddy.works/blog/5-types-of-git-workflows
- https://proandroiddev.com/how-to-set-up-an-efficient-development-workflow-with-git-and-ci-cd-5e8916f6bece
git checkout master
git pull
git checkout feature-branch
git rebase master
git mergetool
git rebase --continue
git rebase --abort
git push -f
git init
git remote add <remote> --track origin/<branch>
git add .
git commit -m "Initial commit"
git push origin HEAD
git clone https://github.com/username/repo.git <TargetFolder>
git clone <url>
cd (change directory)
git checkout -b <branch-name>
git status
Make your changes
git status
git diff
git add .
git status
git commit -m "Commit message"
git push
Merge changes from GitHub
git checkout master
git pull
git status