My Git Cheat Sheet
Jul. 9th, 2020 07:04 amI really like FOSSIL for version control:
https://www.fossil-scm.org/home/doc/trunk/www/index.wiki
You can easily build it from source and it's cross-platform portable. It was written by the developer of sqlite. That being said, most projects will be using git. So for ease of look-up, I'm creating a cheat sheet of the git commands I use most frequently.
Git is much harder to build from source. You can use it cross-platform though. Most Linux distributions have a git package and there's Git for Windows formerly Portable Git for use on Windows systems.
Some git libraries with source to build them include:
https://github.com/libgit2/libgit2
https://github.com/oridb/git9
You can view git code via the web using various methods. Gitweb is one such method. If you're using Windows and just using the code locally or on a secure local network with shared drives, you can use portable xampp. It comes with gitweb and and an Apache web server. You just need to configure both of them. You can use it to easily view a repository on a shared remote drive or a local drive via a web browser. If you're on a Linux or BSD system, use your package manager to download a web server and gitweb. Setting up a web interface using xampp and a shared drive was relatively easy. Setting up a web interface like gitweb with your own web server and accessing via URL is not so easy. I asked several Linux users groups and forums for recommendations. The typical answer was to use a third party product like github and let someone else do the work. Not very helpful if you want to know how to do things for yourself. Since I needed an intranet solution that was secure, I went with the method of setting up a shared drive and using portable xampp with gitweb. I use portable versions whenever possible because it isolates the files used. In this case, I'm referring to portable in terms of moving files to other locations or running completely from a flash drive. There's also portable as in cross-platform portable in terms of being able to build code from source on a variety of operating systems and with difference compilers. Portable apps (programs that are portable across file locations) make it easier to move or copy everything to another machine and to update versions. I've had to do that more than once.
I've been trying to update a project on Github for a few years now and it keeps failing. That's after receiving advice from several people in some of our local developer groups. They couldn't figure out why it was failing either. I finally tried Codeberg and it worked on the first try. Unfortunately, the original version of the project which I cloned is still on Github and I'm not sure how I'm going to get my changes over to it. At least I can get my changes into the Codeberg repository now.
Some basic commands for use with git:
master is the name of the version control branch. origin typically points to the remote source repository that the code is stored in. The default is to use origin but you can add another name for your project and use it instead. The source repository can be a remote URL or a shared drive. The location typically ends with .git
I typically assume the latest version is on my machine and I'm pushing it to the repository. If others are editing the same code or the latest version is in the repository, you'll need to do a pull before you do a push. If you get out of sync because you've forgotten to do the pull, but you know you have the latest and you just need to fix things, you can do a reset.
Substitute the name of the project for the word project in the commands below.
To add another source repository:
git remote add project //remotedrive/git/git.git
Once it's added, you can do a git push for that project.
If you don't want to add the source repository each time before working with git, you can add it to the configuration. There should be a config file under the hidden directory /git/git/.git. Edit the config file and add the repository there. For example:
[remote "project"]
url = //remotedrive/git/git.git
fetch = +refs/heads/*:refs/remotes/project/*
To push code from your machine:
You can use *.* to push all code or you can use other wildcards or a specific file name. The m parameter is for adding a message and you can use any text you want. If you're using gitweb to display the repository, the message information for each commit will appear under summary. You can then drill down to see information about the state of the code at the time of that commit.
Use cd to get to the directory where the source code to be saved with version control is located. If you're on Windows, do this in git-bash. Example:
cd /c/git/git
git add *.*
git commit -m 'data_YYYY_MM_DD'
git push project master
You can check the status during any time during the add and commit using:
git status
To get code to your machine using a pull:
First, cd to the location where you store the code locally. For example, on Windows using a command prompt:
cd /git
git pull project master
If you've tried to update a project and it's out of sync with other systems that also have the project, use:
git reset --hard project/master
To clone a project:
If you didn't create the project on your machine originally and you want to retrieve it from the source repository and work with it on your on machine for the first time, you can clone the source repository.
To clone a git repository, cd to the location where you want to put it. For example, on Windows using a command prompt:
cd /git
Clone the location of the source repository. It could be a shared drive or URL. The location typically ends with .git
git clone //remotedrive/git/git.git
To push code from a cloned repository on my local machine to an online repository:
I cloned the code to my local machine.
To change the branch from master to main:
git branch -m master main
I set the remote repository:
git remote add [repository] [url]
Fill in a name for the repository and the url you're pushing it to.
I had to force the changes to the repository since there was already a README.md file and a license and I wanted to override with the original project's information:
git push -f repository
Note that the files already there (README.md and LICENSE) were lost.
However, this moved the original project with it's history to a new online repository.
https://www.fossil-scm.org/home/doc/trunk/www/index.wiki
You can easily build it from source and it's cross-platform portable. It was written by the developer of sqlite. That being said, most projects will be using git. So for ease of look-up, I'm creating a cheat sheet of the git commands I use most frequently.
Git is much harder to build from source. You can use it cross-platform though. Most Linux distributions have a git package and there's Git for Windows formerly Portable Git for use on Windows systems.
Some git libraries with source to build them include:
https://github.com/libgit2/libgit2
https://github.com/oridb/git9
You can view git code via the web using various methods. Gitweb is one such method. If you're using Windows and just using the code locally or on a secure local network with shared drives, you can use portable xampp. It comes with gitweb and and an Apache web server. You just need to configure both of them. You can use it to easily view a repository on a shared remote drive or a local drive via a web browser. If you're on a Linux or BSD system, use your package manager to download a web server and gitweb. Setting up a web interface using xampp and a shared drive was relatively easy. Setting up a web interface like gitweb with your own web server and accessing via URL is not so easy. I asked several Linux users groups and forums for recommendations. The typical answer was to use a third party product like github and let someone else do the work. Not very helpful if you want to know how to do things for yourself. Since I needed an intranet solution that was secure, I went with the method of setting up a shared drive and using portable xampp with gitweb. I use portable versions whenever possible because it isolates the files used. In this case, I'm referring to portable in terms of moving files to other locations or running completely from a flash drive. There's also portable as in cross-platform portable in terms of being able to build code from source on a variety of operating systems and with difference compilers. Portable apps (programs that are portable across file locations) make it easier to move or copy everything to another machine and to update versions. I've had to do that more than once.
I've been trying to update a project on Github for a few years now and it keeps failing. That's after receiving advice from several people in some of our local developer groups. They couldn't figure out why it was failing either. I finally tried Codeberg and it worked on the first try. Unfortunately, the original version of the project which I cloned is still on Github and I'm not sure how I'm going to get my changes over to it. At least I can get my changes into the Codeberg repository now.
Some basic commands for use with git:
master is the name of the version control branch. origin typically points to the remote source repository that the code is stored in. The default is to use origin but you can add another name for your project and use it instead. The source repository can be a remote URL or a shared drive. The location typically ends with .git
I typically assume the latest version is on my machine and I'm pushing it to the repository. If others are editing the same code or the latest version is in the repository, you'll need to do a pull before you do a push. If you get out of sync because you've forgotten to do the pull, but you know you have the latest and you just need to fix things, you can do a reset.
Substitute the name of the project for the word project in the commands below.
To add another source repository:
git remote add project //remotedrive/git/git.git
Once it's added, you can do a git push for that project.
If you don't want to add the source repository each time before working with git, you can add it to the configuration. There should be a config file under the hidden directory /git/git/.git. Edit the config file and add the repository there. For example:
[remote "project"]
url = //remotedrive/git/git.git
fetch = +refs/heads/*:refs/remotes/project/*
To push code from your machine:
You can use *.* to push all code or you can use other wildcards or a specific file name. The m parameter is for adding a message and you can use any text you want. If you're using gitweb to display the repository, the message information for each commit will appear under summary. You can then drill down to see information about the state of the code at the time of that commit.
Use cd to get to the directory where the source code to be saved with version control is located. If you're on Windows, do this in git-bash. Example:
cd /c/git/git
git add *.*
git commit -m 'data_YYYY_MM_DD'
git push project master
You can check the status during any time during the add and commit using:
git status
To get code to your machine using a pull:
First, cd to the location where you store the code locally. For example, on Windows using a command prompt:
cd /git
git pull project master
If you've tried to update a project and it's out of sync with other systems that also have the project, use:
git reset --hard project/master
To clone a project:
If you didn't create the project on your machine originally and you want to retrieve it from the source repository and work with it on your on machine for the first time, you can clone the source repository.
To clone a git repository, cd to the location where you want to put it. For example, on Windows using a command prompt:
cd /git
Clone the location of the source repository. It could be a shared drive or URL. The location typically ends with .git
git clone //remotedrive/git/git.git
To push code from a cloned repository on my local machine to an online repository:
I cloned the code to my local machine.
To change the branch from master to main:
git branch -m master main
I set the remote repository:
git remote add [repository] [url]
Fill in a name for the repository and the url you're pushing it to.
I had to force the changes to the repository since there was already a README.md file and a license and I wanted to override with the original project's information:
git push -f repository
Note that the files already there (README.md and LICENSE) were lost.
However, this moved the original project with it's history to a new online repository.