Show pageBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== git ====== Git is a code [[vcs|Version Control System]]. Useful resources: * [[https://ohshitgit.com/|"Oh shit, git!"]] * [[https://github.com/git-tips/tips|Git Tips]] on Github * [[tools|Tools for Developers]] * [[https://www.reddit.com/r/git/|/r/git]] ===== common tasks ===== ^ reset changes of specific file | ''%%git checkout -- filename%%'' | ^ undo all changes since last commit | ''%%git reset --hard%%'' | ^ merge multiple commits\\ ([[http://de.gitready.com/advanced/2009/02/10/squashing-commits-with-rebase.html|tutorial]]) | ''git rebase -i'' | ^ show remote repositories | ''git remote'' | ==== add file after commit ==== Sometimes you remember that you have to add a file to the commit you just made. If you want to add a file to your commit after you already committed the changes: <code bash> git add <files> git commit --amend </code> This lets you edit the commit description after you committed the changes. If you don't want to change the commit message: ''%%git commit --amend --no-edit%%'' ===== Learn Git ===== * [[https://learngitbranching.js.org|Learn Git Branching]] – interactive visual Git tutorial. * [[https://git-scm.com/book/en/v2|Pro Git book]] (S. Chacon, B. Straub 2014. Apress) ===== set author ===== <code bash> git config user.name "John Doe" git config user.email "john@doe.org" </code> ===== foreign repositories ===== ==== just merge ==== <code bash># I want to make sure my master is in sync with the upstream master git checkout -b merge-patches master # first pull request git pull --no-ff https://github.com/user1/repo.git master # second pull request git pull --no-ff https://github.com/user2/repo.git branch1</code> ==== add as remote ==== <code bash> git remote add upstream path/to/repository.git # get all refs and objects so your git knows what it can do git fetch --all </code> You can merge foreign branches: <code bash> git merge upstream/master </code> ==== foreign Pull Requests from Github as refs ==== <file - .git/config> [remote "origin"] # add this line fetch = +refs/pull/*/head:refs/remotes/origin/pr/* </file> then execute: <code bash> # get refs git fetch --all # merge pull request with ID 999 git checkout pr/999 </code> ([[https://gist.github.com/piscisaureus/3342247|Source]]) ===== push new branch ===== <code bash># change to branch to be added git checkout master # push it to the repo git push -u origin master</code> ===== push into non-empty origin/master ===== //Error: [remote rejected] master -> master (branch is currently checked out)// <code bash> # push to new branch on remote git push origin master:refs/heads/upload # on remote machine git merge upload git branch -d upload </code> === Alternative: allow update of local branch === git config --local receive.denyCurrentBranch updateInstead ([[http://stackoverflow.com/questions/2816369/git-push-error-remote-rejected-master-master-branch-is-currently-checked|source]]) ==== merge two unrelated repositories ==== If you have two repositories which don't have a common origin, you can still merge them. If you try to do so via simple ''git pull'', you may get the error ''fatal: refusing to merge unrelated histories''. This is because git can't find a commit to base its merge on. <code bash> git remote add some-old-repo https://github.com/user/some-old-repo.git git pull --allow-unrelated-histories some-old-repo master git status -s </code> ([[https://www.alexkras.com/git-merg-two-or-more-repositories-and-keeping-history/|source]]) ===== manage dotfiles with git ===== <code> echo "*" >> ~/.gitignore git add -f .gitignore </code> ^ dotfile hinzufügen | ''git add -f .dotfile'' | === on the target peer === <code> echo "*" >> ~/.gitignore </code> ^ initialise | git init | ^ add remote | git remote add hostname:~ | ^ fetch files | git fetch --all | ^ replace existing | git reset --hard origin/master | ([[https://lrvick.net/blog/syncing_dotfiles_with_git/|Source]]) ===== Submodules ===== ^ get submodules when switched to branch with submodules | ''git submodule update'' | ===== Troubleshooting ===== ==== I copied a git repository from Linux/macOS to Windows and now it shows all files as changed! ==== Do a "git diff" on any file. What do you see? === The permissions have all changed! === <code bash>git config core.filemode false</code> === The whole file changed with weird ^ characters! === These are the line endings. Automatically adjust it to CRLF line endings with: <code bash>git config --global core.autocrlf true</code> ==== unable to migrate objects to permanent storage ==== Your permissions may be wrong. Check if any of the remote files in `.git` are not owned by the user you're connecting with. ([[https://stackoverflow.com/questions/65033831/unable-to-migrate-objects-to-permanent-storage|source]]) ==== git@github.com: Permission denied (publickey) ==== If you just want to clone a public repo with history from GitHub, don't use the ''git'' username like with ''%%git clone git@github.com:radicle-dev/radicle-upstream.git%%'', but rather the ''%%git://%%'' __protocol__: <code bash> git clone git://github.com/radicle-dev/radicle-upstream.git </code> ===== Secrets ===== Store passwords or other information you don't want to accidentally leak somewhere else than in your git repository. Use a secrets storage for this. You can use [[https://git-secret.io/|git-secret]], [[https://github.com/StackExchange/blackbox|blackbox]] or [[https://www.vaultproject.io/|Hashicorp Vault]] for this. ===== Articles ===== Some random articles about how to use Git in a regular or innovative way. * [[https://thenewstack.io/beyond-code-control-git-for-everything/|Beyond Code Control: Git for Everything]] (A. Schmunk, 2019) * [[https://medium.com/@dis_is_patrick/git-for-non-programmers-49c85d3f647d|Git for non-programmers]] (2014) * [[https://opensource.com/article/19/4/write-git|How writers can get work done better with Git]] (S. Kenlon. Opensource.com, 2019) ===== Videos ===== * [[https://www.youtube.com/watch?v=wpISo9TNjfU|Git vs. GitHub: What's the difference?]] (2020, Nathan Hekman, IBM Technology) ===== Tools ===== Useful programs to make life with Git easier or adding functionality to Git. * [[https://github.com/costerwi/rezip|ReZip]] – commit ZIP files to Git. * Last modified: 2024-07-05 14:31