Difference between revisions of "Git/notes"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< Git
Jump to navigation Jump to search
(Created page with "==Questions== * When <code>git pull github</code> returns this: You asked to pull from the remote 'github', but did not specify a branch. Because this is not the default con...")
 
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
==How To==
 +
===status in bash===
 +
To make {{l/linux/cmd|bash}} git-aware, add this line to <code>~/.bashrc</code> (not sure if it matters where in the file):
 +
<syntaxhighlight lang=bash>
 +
parse_git_branch() {
 +
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
 +
}
 +
export PS1="\u@\h \[\033[01;32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
 +
</syntaxhighlight>
 +
This causes bash to show the current branch as part of the command prompt. You will need to [https://stackoverflow.com/a/2518150 reload bash's config] for it to take effect.
 +
 +
===project renaming===
 +
To rename a project (including the folder-slug):
 +
* In your origin repository manager (e.g. GitLab), clone the project into a new one (with the folder-slug you want).
 +
* Clone the project from there to your local workspace.
 +
* Modify any URLs in <code>{{arg|project}}/.git/config</code> that point to the old folder.
 +
* Stage, commit, and push back to the origin.
 
==Questions==
 
==Questions==
 +
''questions that have not yet been answered''
 +
 +
* Given that the four possible conditions for a file are "untracked", "unmodified", "modified" and "staged" (according to [http://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository this]):
 +
** '''Q:''' If a staged file becomes "unchanged" when it is "committed", does that mean that it is no longer "staged"? Or should we speak of those four conditions as applying to ''changes'' rather than ''files''?
 +
** '''Q:''' If the four conditions apply to changes (not files), then does it make sense to talk about "staging files"?
 +
** '''Q:''' How do you get a list of all the files that have been staged, whether or not they are "modified"?
 +
* '''Q:''' Is there any way to set things up so you don't have to keep entering username and password when pushing to a password-protected remote repository (such as GitHub)?
 +
 +
==Answers==
 +
* '''Q:''' Where are git settings stored?
 +
** '''A:''' User settings are stored in <code>~/.gitconfig</code>; not sure where global settings are stored.
 +
* '''Q:''' How do I store my username & password so I don't have to enter it each time I "git push origin"?
 +
** '''A:''' <syntaxhighlight lang=bash inline>git config --global credential.helper 'cache --timeout=10000000'</syntaxhighlight>
 +
*** This doesn't store it permanently, but will "cache" it for --timeout seconds.
 
* When <code>git pull github</code> returns this:
 
* When <code>git pull github</code> returns this:
 
  You asked to pull from the remote 'github', but did not specify
 
  You asked to pull from the remote 'github', but did not specify
 
  a branch. Because this is not the default configured remote
 
  a branch. Because this is not the default configured remote
 
  for your current branch, you must specify a branch on the command line.
 
  for your current branch, you must specify a branch on the command line.
:* Q: How do I figure out what branch I want to use?
+
:* '''Q:''' How do I figure out what branch I want to use?
 
+
:** '''A:''' See [[Git/branch|git branch]].
* Given that the four possible conditions for a file are "untracked", "unmodified", "modified" and "staged" (according to [http://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository this]):
+
* When <code>git fetch github master</code> returns this:
** Q: If a staged file becomes "unchanged" when it is "committed", does that mean that it is no longer "staged"? Or should we speak of those four conditions as applying to ''changes'' rather than ''files''?
+
From https://github.com/woozalia/w3tpl
** Q: If the four conditions apply to changes (not files), then does it make sense to talk about "staging files"?
+
  * branch            master    -> FETCH_HEAD
** Q: How do you get a list of all the files that have been staged, whether or not they are "modified"?
+
:* '''Q:''' Why don't any new files appear in the local folder?
 +
:** '''A:''' Because "fetch" doesn't retrieve files; it just creates a local pointer to the remote branch. (See answer to next question for more details.)
 +
:* '''Q:''' What does this mean? (Did anything actually happen, and if so, what?)
 +
:** '''A:''' This just creates a sort of "pointer" to the remote branch we now need to merge; the pointer is stored in <code>.git/FETCH_HEAD</code>. To actually retrieve the files, <code>git merge FETCH_HEAD</code> tells Git to actually retrieve the files and merge them into the local repository &ndash; the files will now appear in the local folder, with status {{l/same|unmodified}} (i.e. they have been {{l/same|commit}}ted to the local repository).

Latest revision as of 16:10, 31 July 2022

How To

status in bash

To make bash git-aware, add this line to ~/.bashrc (not sure if it matters where in the file):

parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \[\033[01;32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

This causes bash to show the current branch as part of the command prompt. You will need to reload bash's config for it to take effect.

project renaming

To rename a project (including the folder-slug):

  • In your origin repository manager (e.g. GitLab), clone the project into a new one (with the folder-slug you want).
  • Clone the project from there to your local workspace.
  • Modify any URLs in <project>/.git/config that point to the old folder.
  • Stage, commit, and push back to the origin.

Questions

questions that have not yet been answered

  • Given that the four possible conditions for a file are "untracked", "unmodified", "modified" and "staged" (according to this):
    • Q: If a staged file becomes "unchanged" when it is "committed", does that mean that it is no longer "staged"? Or should we speak of those four conditions as applying to changes rather than files?
    • Q: If the four conditions apply to changes (not files), then does it make sense to talk about "staging files"?
    • Q: How do you get a list of all the files that have been staged, whether or not they are "modified"?
  • Q: Is there any way to set things up so you don't have to keep entering username and password when pushing to a password-protected remote repository (such as GitHub)?

Answers

  • Q: Where are git settings stored?
    • A: User settings are stored in ~/.gitconfig; not sure where global settings are stored.
  • Q: How do I store my username & password so I don't have to enter it each time I "git push origin"?
    • A: git config --global credential.helper 'cache --timeout=10000000'
      • This doesn't store it permanently, but will "cache" it for --timeout seconds.
  • When git pull github returns this:
You asked to pull from the remote 'github', but did not specify
a branch. Because this is not the default configured remote
for your current branch, you must specify a branch on the command line.
  • Q: How do I figure out what branch I want to use?
  • When git fetch github master returns this:
From https://github.com/woozalia/w3tpl
 * branch            master     -> FETCH_HEAD
  • Q: Why don't any new files appear in the local folder?
    • A: Because "fetch" doesn't retrieve files; it just creates a local pointer to the remote branch. (See answer to next question for more details.)
  • Q: What does this mean? (Did anything actually happen, and if so, what?)
    • A: This just creates a sort of "pointer" to the remote branch we now need to merge; the pointer is stored in .git/FETCH_HEAD. To actually retrieve the files, git merge FETCH_HEAD tells Git to actually retrieve the files and merge them into the local repository – the files will now appear in the local folder, with status unmodified (i.e. they have been committed to the local repository).