Have a look to the Pro Git book [1], what is the main source of information about Git. Keep in mind this reference if you are in trouble with Git.
If you don’t have an GitHub account, please, do the the Hello World guide at GitHub and create one. 1 Please, create a README.md file for the Hello World repo, as the guide suggests.
Now, we are going to do the same that we would have done using the GitHub web interface (except the Step 1: Create a Repo), but now using the terminal which will be the most used interface for dealing with Git. First, if Git is not installed in your host (try to run git in a terminal), install it with:
(Optional) Clone (download) the Hello World repo. You need to click on the “Code” button (select “https”, not “download a zip file”). Then run:
Notice that a new directory named as the repo’s name at GitHub has been created, and that inside you can find the README.md file written in Markdown.
(Optional) Create (and switch to) a feature branch called improving_readme. In your terminal write:
(Optional) Modify the file README.md. Append to it, for example, a link to the Hello World guide. Use an ASCII editor (nano, for example):
And write:
(Optional) Commit your modification(s):
In your first commit you will be prompted with:
Please, input such information.
After the commit, your local repo is ahead of your origin (copy at GitHub of the) remote repo. This means that your local has modifications that the origin doesn’t have yet.
(Optional) Synchronize your local and the origin using push:
Notice that if you have not uploaded a public SSH key (or the corresponding private key is not properly installed in your computer), the GitHub server requests your username and password, and this is something that is going to happen with every push. To avoid this repetitive input of your GitHub login information, you need to login at GitHub using public-key criptography. For that, you must own a pair of keys, one public and other private, and upload the public one to GitHub.
The first step is to check whether you already have a pair of keys (if your are using the just installed Xubuntu distribution, obviously you don’t need to check anything and can go directly to the next step). Simply revise your $HOME/.ssh directory with:
and if you find a pair of files with almost the same name, and one of they finising in .pub, you probably own a pair of SSH keys.
If you don’t have one or if you prefeer to create a new one, you can create a pair of keys. Open a terminal and write:
using the email address you provided when you created your GitHub account. Then, when you are prompted with:
just press the Enter-key, to select such output prefix. Otherwise, write a different one, but don’t change the path to the .ssh directory.
Now SSH should request you for a passphrase. If you write one, you will be asked for it each time you push your commits to GitHub. There are two options to avoid this:
Input no passphrase (just by pressing the Enter-key again in the previous step). This has the drawback that if somebody steals your keys, he could access to GitHub as he were you.
Input a passphrase and configure ssh-agent to send it to GitHub by you. This option is the preferable one because you will be asked for the passphrase only when the ssh-agent is started (Xfce does that for you).
Now it’s time to check whether the ssh-agent is already running in your computer. This can be done with:
and in the case of Xubuntu, you should get something similar to:
989 ? Ss 0:00 /usr/bin/ssh-agent /usr/bin/im-launch startxfce4 1433 pts/0 S+ 0:00 grep --color=auto ssh-agent
This means that there are two processes in whose description there exists the string ssh-agent. The first entry is the agent process. The second one is the grep running at the same time that the ps.
If the ssh-agent were not running, it can be launched to run in the background with:
eval "$(ssh-agent -s)"
but you don’t need to do that in your Xubuntu installation, because (remember) the ssh-agent the Xfce desktop environment lauches it.
With your keys, run:
and the passphrase will be prompted.
Go now to GitHub -> Settings -> SSH and GPG keys -> New SSH key. Open a terminal and write:
and copy and paste the content of such file (which ends with your email address) inside of the space where you can read “Begins with ’ssh-rsa’, ...”. Don’t forget to give a title (something such as “tm” (tecnologías multimedia)) to the key pair if you created a dedicated one.
When you use the key for the first time (clonning a repo or pushing a commit), the SSH client will warn you that the autenticity of github.com cannot be checked. This is normal and should happen only in the first interaction. Type yes. Notice that if this problem persists, then you could be suffering a man-in-the-middle attack.
Revise The Fork and Branch Git Workflow. Basically, this “protocol” explains that to contribute to an open-source repo hosted by GitHub without belonging to the developping organization, you must do some git-steps (and some of them are below).
Make a fork of the <repo_name> project. We will call to this repo the upstream, whose URL is
git@github.com:<orgainization_name>/<repo_name>.git
This info can be found when you clone the <repo_name>. Notice, that the action of clonning (not forking) the <repo_name> is a waste of time because you cannot contribute directly to it (remember, you must clone your own repo).
Add the remote2 upstream with:
Check that everything has worked with:
where you should see two remotes: origin and upstream. Something similar to:
origin git@github.com:you_at_GitHub/<repo\_name>.git (fetch) origin git@github.com:you_at_GitHub/<repo\_name>.git (push) upstream git@github.com:<orgainization_name>/<repo\_name>.git (fetch) upstream git@github.com:<orgainization_name>/<repo\_name>.git (push)
Finally, to update your local repo write:
git pull upstream master
cat >> ~/.profile << EOF # Add branch name to the Bash prompt: parse_git_branch() { git branch 2> /dev/null | sed -e ’/^[^*]/d’ -e ’s/* \(.*\)/ (\1)/’ } export PS1="\[\033[01;32m\][\u@\h\[\033[01;37m\] \W\[\033[01;32m\]]\$(parse_git_branch)\[\033[00m\]\$ " EOF
[1] Scott Chacon and Ben Straub. Pro Git. Apress, 2020.
1Notice that in the Hello World guide, the master branch is named the main branch.
2Git is a decentralized control system for source code. Decentralization means that every developer has a copy of the origin, and that, the developers can synchronize their locals with any of the remotes.