This tutorial is about setting up a Github account to commit (via SSH) to an existing/new repository and common commands to handle that repository.
- globally (recommended for one github account / OS user)
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
- locally (recommended for multiple github accounts / OS user)
git config --local user.email "[email protected]"
git config --local user.name "Your Name"
- system (across the entire machine)
git config --system user.email "[email protected]"
git config --system user.name "Your Name"
git config --list
Generate the keys
ssh-keygen -t ed25519 -C "[email protected]"
The path I setup was /home/user/.ssh/id_rsa_github
. It generated 2 files: /home/user/.ssh/id_rsa_github
and /home/user/.ssh/id_rsa_github.pub
Setting up the keys - method 1
a. Adding the key to ssh command in a local git repo
git config --local core.sshCommand "ssh -i /var/www/html/website/github.key"
Setting up the keys - method 2
a. Start the ssh-agent
eval $(ssh-agent -s)
b. Add the private key to ssh-agent
ssh-add ~/.ssh/id_rsa_github
Note: add the content of your public key /home/user/.ssh/id_rsa_github.pub
(without email) to Github SSH keys page
git clone https://github.com/binarweb/repository-name.git destination-folder-name
with a specific identity
git clone --config core.sshCommand="ssh -i c:/Users/MyUser/.ssh/github_binarweb" [email protected]:binarweb/repository-name.git .
git clone --depth=1 https://github.com/binarweb/repository-name.git
git init
git config --local user.email "[email protected]"
git config --local user.name "Your Name"
git config --local core.sshCommand "ssh -i /var/www/html/website/github.key"
git remote add origin [email protected]:binarweb/repository-name.git
git branch -M master
git add .
git commit -m 'initial commit'
git push -u origin master
If you work with SSH keys, you need to setup (once) the origin:
git remote set-url origin [email protected]:binarweb/repository-name.git
Then
git add .
stages the file
git commit -m 'additional commit'
commits the changes
git commit -m 'commit title' -m 'commit description'
commits with title and description (it doesn't support multiple description lines - see command below)
git commit
will open an editor where you can add title and description (see this)
git push -u origin master
push the commits to master branch
This command will merge the current change into the last commit (no other commit will be made) with an optional message change
git commit -a --amend
While this will merge the 2 commits, the commit hash changes.
Make sure the squash strategy is merge, by running:
git config pull.rebase false
Squash until a specific commit with:
git reset --soft ceb5ab28ca && git commit
After this, push the changes with:
git push --force
https://stackoverflow.com/a/77386103/9618184
After we have staged the files with git add .
, we can unstage the files with
git rm --cached README
for a single file, or
git reset HEAD .
git reset HEAD README
for multiple/single files
git checkout README
this will revert the file from current revision, or
git checkout master~2 README
to revert the file from two revisions back from master branch
git pull origin master
or with overwrite
git reset --hard HEAD
git pull origin master
git diff
simple (author, date, commit title, commit hash):
git log -1
with some details (simple + diff of files):
git show
git checkout dev
where dev
is the branch you want to switch to
git reset --hard HEAD~2
git push -f
HEAD~2 means the last 2 commits
Note: make sure you don't have remote commits before running git push -f
command or else the remote changes will be permanently lost
git log --reflog
git status
Get a list of files you want to commit
git status
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: file1
modified: file2
modified: file3
modified: file4
Add the files to staging
git add file1 file2
Check to see what you are committing
git status
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: file1
modified: file2
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: file3
modified: file4
Commit the files with a commit message
git commit -m "Fixed files 1 and 2"
If you accidentally commit the wrong files
git reset --soft HEAD~1
will delete the last commit, but you will not lose any file changes
If you want to unstage the files and start over
git reset
Unstaged changes after reset:
M file1
M file2
M file3
M file4