A collection of Git command line usage and configurations. This is not a tutorial, but a reference mostly for myself when forgetting something.
Set the name and email to be used with your commits.
git config --global user.name "Your Name" git config --global user.email "email@example.com" git config --global core.editor vim
Also sets VIM as the editor for when Git require input.
To set the configuration for a specific repository, use
Initialize Git repository
Only needed if you are starting without a remote repository and only intend to work locally.
cd src/mycode git init
Another variant is using the
-C option, which tells Git to change
work directory to the parameter given.
git -C src/mycode init
This works with any other command requiring a local Git repository.
Add file/s to Git repository
Add all tracked files to the index (staging area), but not untracked ones.
git add -u
Mark an untracked file part of the repository, but do not add it to the index.
git add -N newfile.c
If a file was added by mistake to the staging area.
git restore --staged -- newfile.c
To discard changes made to a file already under version management.
git restore -- changedfile.c
Commit all deleted-, added- and modified files, but not untracked.
git commit -a -m "Commit message"
Amend (replace) last commit with additions/deletions and no commit message.
git commit --amend --no-edit
If a commit is made with a mistake, a simple command can bring it back.
Note: Not a good idea on already pushed changes.
git reset --soft HEAD^
Remote Git repositories
Clone a local repository.
git clone /path/to/repository localrepository
If the remote repository is huge, a shallow clone can be done to save time.
git clone --depth 3 username@host:/path/to/repository localrepository
Create a dedicated Git repositories on a remote server
This is something I used before to self-host my repositories outside the home network, but it’s not really necessary since Gitea is a much better option for self-hosting your repositories.
Login to your server, create a
sudo adduser git sudo -u git mkdir /home/git/.ssh sudo -u git chmod 700 /home/git/.ssh
On your workstation, copy your public key to the git account.
(Now is a good time to restrict the git account to only allow public key authentication.)
Create a repository
test on the remote server.
ssh firstname.lastname@example.org 'git --git-dir=test.git --bare init'
Add the remote repository to your local repository.
cd test git remote add origin email@example.com:test.git git push origin master
Pulling changes from remote repository
If you perform a pull and want to go back to the state before.
git reset --hard ORIG_HEAD
The same goes for a unsatisfactory merge, the command below will go back to the state before the merge.
git reset --merge ORIG_HEAD
Forking a repository is technically not much different from cloning, the idea is to have the original repository on a different remote origin and use it to synchronize your own copy with the updates.
Start by cloning a repository, then rename the remote from origin to
git clone https://github.com/xfce-mirror/libxfce4util.git libxfce4util cd libxfce4util git remote rename origin upstream
Add your own locations and push.
git remote add origin firstname.lastname@example.org:apalsson/libxfce4util.git git push -u origin --all git push -u origin --tags
Accidental pushes to the upstream can be prevented by setting the url for push to something invalid, e.g.
git remote set-url --push upstream push_not_allowed
Add your own remote location.
git remote add origin https://gitlab.com/apalsson/libxfce4util.git
Synchronize with upstream
Use fetch to retrieve changes in the upstream repository.
git fetch upstream
If your repository is shared with anyone else, use the
merge command to
incorporate the changes into your branch, otherwise
rebase is a better option
that also allows for a nicer commit history.
git co master git rebase upstream/master
Fix conflicts, proceed rebase process with
git rebase --continue and in case
of regret use
git rebase --abort to cancel the rebasing process.
The merge command can be cancelled with
git merge --abort if needed.
To create a new branch of the current one and switch to it.
git checkout -b the-new-branch
Show contents of a file in another branch.
git show newbranch:src/main.c
Show differences between two branches.
git diff master..newbranch
Compare a single file between two branches.
git diff master..feature -- src/main.c
Squashing / combining commits
To merge a number of commits into as single, use rebase on the commit before
the ones to be merged into one. E.g. to combine the 3 last commits, then the
b292795 should be specified for rebase.
a9fab14 (HEAD -> master) crowdin: Add macedonian 9f132eb (tag: v1.6.6) Release v1.6.6 0924929 [ci skip] Crowdin translation bot: New translations for Ukrainian (#209) b292795 Update translations (#203) f82d24d Update crowdin bot commit message template
-i option to start an interactive rebase.
git rebase -i b292795
Your editor will open asking which commits to be squashed and skipped.
pick 0924929 [ci skip] Crowdin translation bot: New translations for Ukrainian (#209) squash 9f132eb Release v1.6.6 squash a9fab14 crowdin: Add macedonian # Rebase b292795..a9fab14 onto b292795 (3 commands)
The end-result becomes as follows.
db8c95a (HEAD -> master) # This is a combination of 3 commits. # This is the 1st commit message: b292795 Update translations (#203)
This rewrites history of the branch, and must be force pushed if squashed commits already were pushed to repository.
Create a tag
git tag -am "3.1.2rc1"
List all tags
Find tag starting with ‘1.9’.
git tag -l "1.9.*" # List tags starting with "1.9."
Pushing the created tag to remote repository.
git push -u origin --tags
See the history of a specific file.
git log --follow --all -p -- ./src/main.c
Multiple remote locations
It is possible to have multiple remote repositories, so that a push will be done to multiple repositories at the same time.
git remote set-url --add --push origin https://gitlab.com/name/repo.git git remote set-url --add --push origin https://github.com/name/repo.git