gitsheet - feliyur/exercises GitHub Wiki
git rev-parse --show-toplevel |
Show repository root dir. |
git rev-parse --abbrev-ref HEAD |
Show current branch. |
git checkout <branch> |
verbatim |
git branch <new name> |
create branch |
git checkout -b <new name> |
Create and checkout branch |
git diff --cached [commit id] <file 1> ... <file n> |
--cached flag allows diff of a staged file. |
git push [-u origin branch] |
-u flag creates remote branch. Use after e.g. checkout -b. |
git -C <directory> <command> |
run as if current directory is <directory>. |
git show <revision>:<file> |
(e.g. revision=HEAD~4), outputs a previous version of file. |
git pull . <local branch name> |
pull from a local branch |
git show |
Show current commit info |
git mergetool |
Merge conflict (see config section on choosing mergetool) |
git difftool <commit> -- <path> |
Show diff and merge w.r.t. commit |
git reset --hard <commit id> |
Reset HEAD pointer to a previous commit, discarding changes (--soft keeps changes). push -f does the action remotely, e.g. undoes commits. |
git revert -m 1 <commit id> |
Reverts a commit, can specify a range with .. , e.g. <start id>..<end id> , more here
|
git branch --help |
|
git branch <new name> |
create new branch |
git branch -a |
List all local and remote branches (use -r , -l for remote/local only) |
git show-branch |
List branches with extended information |
git branch -d |
delete (-D - force delete). Use git push <remote> --delete <branch name> to delete remote branch. |
git branch <branch_name> <commit-hash> |
Create a new branch from a given commit |
git remote -v |
show all remote urls |
git remote add <alias> <url> |
add a remote (can be used with pull alias and other commands) |
git remote show <alias> |
show info about a remote |
git remote set-url <alias> <url> |
update url |
git config --get remote.<alias>.url |
Get just the url. |
git worktree add [--checkout] [-b <new-branch>] <path> [<commit-ish>] |
Add folder with tracked repo. |
git worktree list |
|
git worktree remove <directory> |
|
git symbolic-ref HEAD refs/heads/mybranch |
Set currently active branch in a bare repo (worktree root). find refs to show existing refs. |
To create a new branch within a worktree, do
git branch --track <new branch> # Optionally: <origin branch>
git worktree add <new branch> <remote branch>
Full docs here.
Create a bare-bones repository (required for using worktree):
git clone --bare /path/to/repo
Convert an existing repository to bare-bones: (taken from here).
cd repo
mv .git ../repo.git # renaming just for clarity
cd ..
rm -fr repo
cd repo.git
git config --bool core.bare true
TODO.
Docs here https://git-scm.com/book/en/v2/Git-Tools-Submodules
Keybindings: https://github.com/jesseduffield/lazygit/blob/master/docs/keybindings/Keybindings_en.md
Clone a local repository: Make sure to replicate remote origin url.
git clone <local dir> <new dir>
git -C <new dir> remote set-url origin `git -C <local dir> remote get-url origin`
Recover from a hard action / reset: Following this.
git reflog
# find the relevant commit id to recover to
git reset --hard <commit id> # Recovers the files
Mirror a repository:
While it isn't possible to fork from the public GitHub directly to your Enterprise installation, you can fork it on the public GitHub and then mirror that forked repository on your enterprise installation.
Steps
- Create an empty repository on your enterprise GitHub:
curl https://github.yourenterprise.com/api/v3/user/repos \ -u "yourUsername" \ -d "{\"name\": \"whatever-repository\", \"private\": true }" \ -H "Authorization: Bearer TOKEN"
(use api.github.com/user/repos
for public github).
- Create a bare clone of your fork
git clone --bare https://github.com/publicGitHubUser/forked-repository.git
- Change directories so you are inside the bare clones folder:
cd ./whatever-repository.git/
- Push the repository with the
--mirror
flag to your enterprise GitHubgit push --mirror https://github.yourenterprise.com/enterpriseGitHubUser/forked-repository.git
More Information
https://help.github.com/articles/duplicating-a-repository/
Set vim as text editor. | git config --global core.editor "vim" |
Store credentials. |
git config --global credential.helper store . Generate (github) access token through Settings/Developer Settings . On linux this stores credentials in ~/.git-credentials . |
Set vimdiff as diff or merge tool |
git config --global merge.tool vimdiff , git config --global diff.tool vimdiff . Can set a different tool for merge.guitool and diff.guitool (e.g. kdiff3) |
Add to ~/.gitconfig
:
[color]
diff = auto
status = auto
branch = auto
interactive = auto
ui = true
pager = true
Following https://unix.stackexchange.com/questions/44266/how-to-colorize-output-of-git
From https://stackoverflow.com/questions/14753603/shortcuts-for-git-commands.
Add to .gitconfig
:
[alias]
st = status
ci = commit
br = branch
co = checkout
Add to .bashrc (or a sourced script):
alias ga='git add'
alias gc='git commit -v'
alias gd='git diff'
alias gst='git status'
alias gco='git checkout'
alias gcm='git checkout master'
alias gb='git branch'
# view remote branches
alias gbr='git branch --remote'
alias gup='git pull --rebase'
alias gp='git push'
# push a newly created local branch to origin
alias gpsup='git push --set-upstream origin $(git_current_branch)'
Add to .bashrc
:
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="\u@\h \[\e[32m\]\w \[\e[91m\]\$(parse_git_branch)\[\e[00m\]$ "
Then restart shell. Taken from here
Symptom | Solution |
---|---|
git push -u origin branches outputs "src refspec ... does not match any" |
Use git push -u origin HEAD:branch . |
remote: error: refusing to update checked out branch: ???? <br />remote: error: By default, updating the current branch in a non-bare repository<br />remote: is denied, because it will make the index and work tree inconsistent<br />remote: with what you pushed, and will require 'git reset --hard' to match<br />remote: the work tree to HEAD. |
Cannot push to a branch that is checked out in push target. Switch target to another branch, or use a bare repo. |
git for windows does not work with powershell out of the box | Run $Env:GIT_SSH=$((Get-Command -Name ssh).Source) or set GIT_SSH env var accordingly for a permanent fix. |
Submodule in detached head state | Normal behavior - submodule by default does checkout of a particular commit. See excellent explanation and solution here. |
Authentication problems on Windows 10 and later | check Windows "Credential Manager", git might use it by default to store credentials. Can disable. |