Skip to content

Latest commit

 

History

History
171 lines (138 loc) · 5.44 KB

File metadata and controls

171 lines (138 loc) · 5.44 KB

Git

A collection of Git commands and features that can be used as a quick reference.

Git Commands

Command Action
git clone https://github.com/user/repo.git clones the repo using https
git clone git@github.com:user/repo.git clones the repo using ssh
git add -u adds all modified files to the local git repo
git commit -m "Commit Message" Commit the changes
git push -u push the local branch and set it as upstream
git pull --rebase origin master rebases the local branch to the remote master
git rebase -i HEAD~3 start interactive rebase for the last 3 commits
git rebase -i <commit_hash> start interactive rebase starting after the stated commit
git rebase --onto main <branch> rebase a branch onto main
git log show the Git log and who made which commits
git diff show all unstaged changes
git update-index --assume-unchanged <file> tell git to ignore any changes to a file

Branches

Command Action
git switch -c branchName Create a new branch and switch to it
git push -u origin branchName Push the new branch to remote and set it as upstream
git fetch origin branchName Fetch a branch to local without merging/rebasing it
git branch -d branchName Delete a local branch (use -D to force delete)
git push origin --delete branchName Delete a branch on remote
git branch -r list all remote branches.
git branch -a shows the local list of all known branches (run git fetch before)
git remote update origin --prune update the local list of remote branches

Forks

Command Action
git remote -v shows all remote connections with their respective URL
git remote add <name> <URL> adds a remote URL with <name> (e.g. upstream) to the branch
git fetch upstream fetch upstream remote changes into your fork
git rebase upstream/main rebase you branch to upstream/main

Amending commits

Sometimes it is necessary to edit a commit, either beacuse the message is incomplete or the author is incorrect.
git commit --amend lets you edit the last commit message (this also works while rebasing).
git commit --amend --author="Author Name <email@address.com>" --no-edit changes the author of the last commit message.
It is also possible to squash or fixup commits with a rebase.

Fixup old commit

It is possible to fixup a specific commit using its commit ID/Hash.

git add <FILE>                              # Stage the fix
git commit --fixup=<COMMIT_ID>              # Commit a fix for a specific commit
git rebase -i --autosquash <COMMIT_ID>~1    # rebase the fixup

Remove file from old commit

You can remove a file from an older commit using its commit ID/hash.

git checkout <COMMIT_ID>
git reset --soft HEAD^
git restore --staged <path/to/file>
git commit -c ORIG_HEAD --no-edit
git rebase --onto HEAD <COMMIT_ID> <DESTINATION-BRANCH>

Undo a rebase

Check git reflog and search for the commit before the rebase and reset the branch to it. Suppose the old commit was HEAD@{2}:

git reset --hard HEAD@{2}

Move commits to a new branch

Can be useful when you forgot to create a new branch before making changes.
Make sure you stay on your current branch, e.g. main before doing this.

git branch featurebranch    # create the new feature branch
git reset --keep HEAD~X     # move the current branch back X commits
git switch featurebranch    # switch to the new branch

If your feature branch already exists, the steps are a bit different.

git switch featurebranch    # switch to your existing feature branch
git merge main              # add the new commits to the existing branch
git switch main             # switch to main
git reset --keep HEAD~X     # move main back X commits

Resolve all merge conflicts at once

In case you want to resolve all merge conflicts at once (with --theirs or --ours).

git status | grep both | awk '{print $3}' | xargs git checkout --[theirs|ours]

Apply multiple patches with Git

Can be useful when multiple patch files should be merged into one.
Checkout the commit or release tag of the Git repo to apply the patches to. Afterward, the whole diff between all patches and the chosen commit/release tag can be created.

git reset --hard 26.2.2             # reset the working tree
git am ~/Downloads/patch_*.diff     # apply all patches
git diff 26.2.2 > big_patch.patch   # get the diff between HEAD and the tag

.gitconfig

Use git ignored to list all files marked with --assume-unchanged.

[alias]
    ignored = !git ls-files -v | grep "^[[:lower:]]"
[commit]
    verbose = true
[core]
    editor = nvim
[diff]
    algorithm = histogram
    colorMoved = plain
    mnemonicPrefix = true
    renames = true
[fetch]
    all = true
    prune = true
    pruneTags = true
[gitreview]
    username = pgriffin
[http]
    postBuffer = 157286400
[init]
    defaultBranch = main
[log]
    decorate = short
[status]
    showStash = true
[user]
    email = peter@griffin.com
    name = Peter Griffin

Set up SSH with Git

Generate a keypair

Generate the the ssh keypair (if you don't already have one).

ssh-keygen -t rsa -b 4096

Copy your public key (~/.ssh/id_rsa.pub) to Github.

Working with repos

Change your directory to a local repo and run:

git remote set-url origin git@github.com:username/your-repository.git

Or clone a new repo with:

git clone git@github.com:username/your-repository.git