Git
How to avoid merge commits
Download the latest commits
git remote update -p
Update the local branch
git merge --ff-only @{u}
If the above fails with a complaint that the local branch has diverged:
git rebase -p @{u}
git reset --hard HEAD~1
git reset HEAD~1
Add
Adds co-author to your commit message. Handy for pair programming.
Commit message
Co-authored-by: Joel Califa <602352+califa@users.noreply.github.com>
Push
In order to not override someone else commits(with force) in remote repository use:
git push --force-with-lease
Reset
git reset HEAD~1
where ~1
is a number of commits from HEAD
# hard reset (loosing files/changes)
git reset --hard HEAD~1
Rebase
# on a remote branch
git pull --rebase
# on a master
git pull --rebase origin master
Stash
# preview last stashed changes
git stash show -p stash@{0}
Create named stash with adding files with hunks preview
git stash push -p -m "my awesome config"
Checkout unwanted files
git checkout -- .
Checkout to previous branch
git checkout -
Edit last local commit message
git commit --amend
Git log
To view a git log of just one user's commits:
git log --author="Alex"
Commit
Empty commit
Is useful to re-trigger CI pipeline or any other cloud integrations.
git commit --allow-empty -m "Trigger something"
Branch
Rename a local Git branch
Rename a current branch
git branch -m <newname>
Rename a brach while pointed to any branch
git branch -m <oldname> <newname>
Git global configurations
in ~/.gitconfig
To change your Git username and email
git config --global user.name "Alexander Grischuk"
git config --global user.email [your email address here]
Enable git pull use rebase by default
git config --global pull.rebase true
is equivalent to running git pull --rebase
every time.
git config --global push.default current
~/.gitignore_global
Ignore IDEA files globally
echo ".idea" > ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global
Create a new .gitignore
file fetched from the GitHub repo for a "node" js app
.gitignore
file fetched from the GitHub repo for a "node" js appnpx gitignore node
Creating a SSH Public Key on OSX
ssh-keygen -t rsa -b 4096
Copy public SSH key and add it to the GitHub
pbcopy < ~/.ssh/id_rsa.pub
UseKeychain: do not re-enter password while commiting/clonning
Add following to the ~/.ssh/config
Host *
AddKeysToAgent yes
UseKeychain yes
Add private key to keychain
ssh-add --apple-use-keychain ~/.ssh/<private_key>
Make sure AddKeysToAgent
is set to yes
in ~/.ssh/config
for this to work
How to resolve conflicts in yarn.lock
https://github.com/yarnpkg/yarn/issues/1776#issuecomment-269539948
git rebase origin/master
When the first conflict arises, I checkout the yarn.lock then re-perform the installation
git checkout origin/master -- yarn.lock
yarn
This generates a new yarn.lock based on the origin/master version of yarn.lock, but including the changes I made to my package.json. Then it's just a matter of:
git add yarn.lock
git rebase --continue
Release
Create a release tag
git tag <tagname>
Push all tags
git push origin --tags
Push a single tag
git push origin <tag>
List all tags
git tag
Clone
Get up to speed with partial clone and shallow clone
Clean up
clean unreachable object (sometimes speeds up .git)
git prune --dry-run
clean outdated branches
git fetch --prune
Advanced git
git-reflog
- Manage reflog informationgit-bisect
- Use binary search to find the commit that introduced a bug
Further reading
Last updated
Was this helpful?