Managing git
Posted: 2023-02-28 Filed under: system | Tags: git, github 3 CommentsI appreciate git’s power, but I still find it unnecessarily complicated. I am not going to whine or complain here about that. Instead, I decided to outline a few steps about setting up keys and some basic commands. I am using GitHub as an example.
Setting up keys
Click on your profile picture, then go: Settings → SSH and GPG keys → New SSH key. For detailed instructions click on “Check our guide to generating SSH keys.” Open a terminal, then:
Generate an ed25519 key:
ssh-keygen -t ed25519 -C "myemail@email.com"
you will be prompted the following, and normally you go with the default:
Enter file in which to save the key (/home/<user>/.ssh/id_ed25519): Your identification has been saved in /home/<user>/.ssh/id_ed25519 Your public key has been saved in /home/<user>/.ssh/id_ed25519.pub
Analogously, for the legacy method (that’s what I have used):
ssh-keygen -t rsa -b 4096 -C "myemail@email.com"
Enter file in which to save the key (/home/<user>/.ssh/id_rsa): Your identification has been saved in /home/<user>/.ssh/id_rsa Your public key has been saved in /home/<user>/.ssh/id_rsa.pub
Tell ssh about the key:
ssh-add /home/<user>/.ssh/id_ed25519
-or-
ssh-add /home/<user>/.ssh/id_rsa
Make sure you have a ~/.ssh/config
file with the following contents:
# Default account Host github.com HostName github.com User git # Depending on what you used: IdentityFile ~/.ssh/id_ed25519 # IdentityFile ~/.ssh/id_rsa
Open id_ed25519.pub
or id_rsa.pub
and paste the contents (key) at GitHub in SSH keys / Add new field. Set a title to your liking.
You can have several users on the same machine e.g a “work” user and a “hobby” user, each managing a different git account. When generating the key, specify a custom file name:
/home/<user>/.ssh/id_ed25519_custom
Tell ssh about the key:
ssh-add /home/<user>/.ssh/id_ed25519_custom
Add the following to ~/.ssh/config
# Custom account Host github.com-custom HostName github.com User git IdentityFile ~/.ssh/id_ed25519_custom
Using git
Set up details (globally)
git config --global user.name "Firstname Secondname" git config --global user.email myemail@email.com git config --global core.editor nano git config --global init.defaultBranch main
To start a new repo locally, initiate in an empty folder, create the empty repo online in the browser (just create it, without a README), and specify origin:
git init git remote add origin git@github.com:UserName/newRepo
To clone an online repo locally:
git clone https://github.com/UserName/repoName
Commits and push
# tell git to track all new files (if any): git add * # or, tell git to add only one changed file (or folder): git add README.md # commit all changes (you'll be promped for a message): git commit -a # or, commit just one changed file/folder (-m: message): git commit README.md -m "README.md: Added" # push commit online: git push -u origin main
Working with branches
# make a branch (“devel”) and switch to it: git branch devel git checkout devel # push changes to branch git push -u origin devel # switch back to main and merge devel to main git checkout main git merge devel # check what branches you have and where you are git branch # delete a branch "devel" (careful!!): git branch -d devel # push the delete action: git push -d origin devel
If you have several users managing different repos, things may get complicated, because you need to specify which user does the commits. You need to remove this, because the default is git@github.com
:
# remove origin git remote rm origin # then add the custom one git remote add origin git@github.com-custom:UserName/newRepo
What about git rebase?
Never used it. Can you give an example situation for its use?
Of course! For example, you cloned SlackBuilds.org git repo and you want to keep it updated with your local changes. You want your local changes to be applied after public SBo updates, not to have a mix of public and your local updates.
This is what rebase does.