The regular action to install git is sudo apt install git. However, with this command, you will retrieve the git version that is valid at that time for the given Linux OS. Chances are that there's a newer version available already.
To get the latest version on a Linux distro, type the following commands:
sudo add-apt-repository ppa:git-core/ppa -y
sudo apt-get update
sudo apt-get install git -y
If you then run
git --version
you might have a more recent version of git compared to the regular installation process
You should at least have your name and e-mail set up for Git.
To do so, run the following two commands
git config --global user.name <your_name>
git config --global user.email <your_email>
This way your name and e-mail will be connected to all commits you do in a repository and you will not be bugged with it the first time you do a commit and your username and/or e-mail is not known yet.
To upload a local copy to GitHub, follow the below steps:
Log in into your GitHub account
Select the tab Repositories
Press the New button (button has a green background)
Fill in the repository name (doesn't have to be the same name as your local repository, but I feel it's logical to keep both names the same)
Select if you want it to be a private or public repository
You can select other things too (add readme, .gitignore, license type...) if you want
Click the Create repository button at the bottom of the page
In the new screen you will see the HTTPS (or SSH) link to make a remote connection to the master on GitHub from your local git repository. Copy the HTTP (or SSH) link
Go back to your local git repository and execute the following command:
git remote add origin <https_link_you_copied>
The remote repo will be added to the config file of your local git repository. Go to the .git directory in your repo and open the config file. You should see something like this in the file (among other things):
[remote "origin"]
url = https://github.com/GeVanCo/RelayBoard.git
fetch = +refs/heads/*:refs/remotes/origin/*
This means the remote origin location is now connected to your local git repository.
To push your local git repository content to the remote origin repository, execute the following command:
git push origin master --force
This will push your local repository (master in this case) to the remote repository (origin in this case)
It might be that GitHub is asking for your credentials if it's the first time you're pushing something from that computer. If so, do the following:
if asked for your username, fill in your GitHub user name. In my case, this is gevanco
if asked for your password, use the PAT password, NOT the user password we used to use. This method is not valid anymore since August 13, 2021!
I recently had issues using my existing PAT and it turned out I had to create a new one, although the old one was set to "never expire"...
To avoid GitHub asking your password (PAT!!!) over and over again each time you want to do an action (push, pull, fetch,...) you can ask the help of the credential helper like so:
git config --global credential.helper store
After entering the above command, next time you access the GitHub repo it will ask you one more time for your PAT but then it won't ask it to you anymore all future times.
To undo that mechanism, run the command git config --global --unset credential.helper
Once this is done, execute the following command:
git pull
The first time you will see the following message:
$ git pull
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 920 bytes | 3.00 KiB/s, done.
From https://github.com/GeVanCo/RelayBoard
33812a9..dc949b1 master -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> master
This means you still have to set the tracking information to the branch. Execute the following command to do this:
git branch --set-upstream-to=origin/master master
Now you have made an upstream connection between your master repository and the remote origin/master repository.
Performing a
git pull
now will pull all the changes from the remote repository to the local one.
If all went fine in the previous step, you should see that next to your master branch there's also the origin/master branch, referring to the branch on the remote server (GitHub in this case). At least, if you're using GitExtensions or another Git capable graphical desktop application.
If you want to check/see all the branches (including the remote ones) you have in your current Git repo, run the command git branch -a.
If you now make changes locally and you execute the command
git push
your changes will be uploaded to the remote git repository.
Once the push command is finished, don't forget to do a
git pull
again to bring your local repository in sync with the remote repository!
Make it global:
git config --global status.showUntrackedFiles no
Make it local:
git config status.showUntrackedFiles no
To undo the change:
git config status.showUntrackedFiles normal
git rm --cached <file> to remove the given file from the cache
git commit to commit the deleted file
add the file to .gitignore, check with git status
commit again
git reset --soft HEAD~1
git restore --staged .
When you have committed files locally and you do a git diff, you won't see any differences anymore. However, it's still possible to see the differences in the committed files, using the following commands:
git diff HEAD HEAD~1: this will show the newest changes in read and the old situation in green when using the default Git colours.
This is less intuitive: red normally means removal of lines, green means addition of lines.
git diff HEAD~1 HEAD: same as above, but added lines will be shown in green, removed lines in red. To me, that's much more "logical".
If you only want to see the names of the files that are changed but not their content that has been changed, add --name-only to the command like so:
git diff --name-only HEAD~1 HEAD
If you want to only include files instead of exclude files in your Git repository your .gitignore file should be like this:
*
!*.ic
!*.ih
!*setup*
How this works:
First, you tell Git you don't want any file to be added to the Git repository. Only then you ask Git to make some exceptions for certain file types.
So, in the example above, all files will be ignored except the files ending with .ic or .ih or files where setup is part of the file name.
This way, you include files instead of excluding them.
If you want to change the default Git editor and use, for instance, vim then run the following command:
git config --global core.editor "vim"
I've installed gitk on my RPi to visualise the Git repositories. After installation I just have to run gitk in the Git repository I want to see and a graphical UI will pop up after a short period of time.
Remember you have to have an ssh -X secure connection to the RPi before graphical stuff is sent over to the client!