Git FAQ¶
General¶
GitHub¶
How to link to a specific line number on GitHub¶
First get a permanent link to files you want to link to on on GitHub. You can do this by viewing the file and pressing the “y” key to update the URL to a permalink to the exact version of the file you see (see this link for more information).
Then click on the line number you want (like line 18), and the URL in your browser will get a #L18 tacked onto the end. You can now copy and use this url.
CLI (command line interface)¶
Backup untracked files¶
To copy untracked files into a separate directory outside the git repository before cleaning up, use
git ls-files --others --exclude-standard -z -m - d | cpio -pmd0 . ./untracked-backup/
Options used:
--otherslists untracked files--exclude-standarduses .gitignore and the default git excludes-znull-delimited output-ppass-through (=copy)-mpreserve modification times-dcreate missing directories-0null-delimited input
These commands might be useful when migrating repositories e.g. from bitbucket to github. This allows the untracked changes to be move to a separate folder which can then be copied over to the new location where the new repo is located.
Determine the URL that a local Git repository was originally cloned from?¶
- If referential integrity is intact, use:
git remote show origin - If referential integrity has been broken, use
git config --get remote .origin.url
Change an existing local git repository’s remote URL from HTTPS to SSH¶
Cloning/pushing to a repository using a HTTPS remote URL’s (e.g. https://github.com/PrasadBabarendaGamage/iron.git ) requires a password. This can be avoided by cloning any new repository using a SSH remote URL (e.g. git@github.com:PrasadBabarendaGamage/iron.git), or by changing the remote URL for an existing local repository to use the SSH remote URL.
To change a existing local git repository’s remote URL from HTTPS to SSH:
- Edit the .git/config file under the root directory of the repository.
- Find
url=entryunder section[remote "origin"] - Change it from
url=https://github.com/PrasadBabarendaGamage/iron .gittourl=ssh://git@github.com:PrasadBabarendaGamage/iron.git. that is, change all the texts before the@symbol tossh://git - Save config file and quit.
Now you can use git push origin your_branch to sync with your remote repo.
Note that before you can push to your remote repo, you will need to have setup a ssh key pair e.g. in on github.
Make a git submodule to track remote branch¶
# add submodule to track master branch git submodule add -b master [URL to Git repo]; # Make sure the parent repo knows that its submodule now tracks a branch: cd /path/to/your/parent/repo git config -f .gitmodules submodule.<path>.branch <branch> # Make sure your submodule is actually at the latest of that branch: cd path/to/your/submodule git checkout -b branch --track origin/branch # if the master branch already exist: git branch -u origin/master master # (with 'origin' being the name of the upstream remote repo the submodule has been cloned from. # A git remote -v inside that submodule will display it. Usually, it is 'origin') # Don't forget to record the new state of your submodule in your parent repo: cd /path/to/your/parent/repo git add path/to/your/submodule git commit -m "Make submodule tracking a branch" # Subsequent update for that submodule will have to use the --remote option: # update your submodule # --remote will also fetch and ensure that # the latest commit from the branch is used git submodule update --remote # to avoid fetching use git submodule update --remote --no-fetch
When cloning a repository with a submodule, use the following command to initialise and update the submodules.
git submodule update --init --remote
How do I make Git ignore file mode (chmod) changes?¶
git config core.fileMode falsegit config core.fileMode falsecore.fileMode If false, the executable bit differences between the index and the working copy are ignored; useful on broken filesystems like FAT. See git-update-index(1). True by default.