Git Cheat Sheet
A few of the Git commands I find myself using all the time.
Global User Settings
You can set the basic global user settings with the following commands. This will make sure any commits are attributed to you.
git config --global user.name "Jayne Doe" git config --global user.email firstname.lastname@example.org
New Local Repository
You will often create a new repository on a cloud service (GitHub, GitLab, BitBucket), then clone it on your local machine. If instead you are starting with a local repository, you will need to do the following.
Create a new directory for the repository and initialise it as a Git repository.
mkdir /u01/git/oraclebase/vagrant cd /u01/git/oraclebase/vagrant git init .
Create a ".gitignore" file, so Git can ignore specific files, file types or whole sub-directories.
cat > .gitignore <<EOF # Software Files # ################## *.gz *.zip *.jar .vagrant *.bin *.vdi EOF
Create a "README.md" file, so people can see what the repository is for.
cat > README.md <<EOF # Vagrant Builds Just some Vagrant builds I use. Feel free to copy or fork this stuff. It's unlikely I'll accept pull requests as it's just my playground. ## Required Software * [Vagrant](https://www.vagrantup.com/downloads.html) * [VirtualBox](https://www.virtualbox.org/wiki/Downloads) EOF
Stage and commit theses changes to the repository.
git add . git commit -m "First commit."
You can read all about the commands in Pro Git, or Google them, but these commands are what I use on a daily basis.
List tracked files, meaning files Git already knows about, that have been modified, but not yet staged.
git diff --name-only
List tracked files that have been modified and staged, but not yet committed.
git diff --name-only --staged
List all untracked files, meaning files you've not told Git about yet. This excludes those files, file types and directories listed in the ".gitignore" file.
git ls-files --other --exclude-standard
There are a large number of ways you can stage new files. Here are some I use regularly.
# Stage all untracked files. git add $(git ls-files -o --exclude-standard) # Stage all tracked files that have been modified. git add -u # Stage explicitly referenced files. git add banana.txt ./citrus/orange.txt
Commit all staged files with an appropriate commit message.
git commit -m "Minor formatting changes."
When working with a remote repository you will need to push your changes to the remote repository once you are happy with them. This example assumes we are going to push the changes to the "master" branch on the remote repository.
Pull the latest changed to your local repository.
Do some work, which may involve changing existing files, adding new files or directories, or deleting existing files or directories. You should stage and commit changes based on individual units of work. If you have three distinct changes to perform, regardless of how many file changes occur in each of the three changes, they should be committed to your local repository as three separate commits, with meaningful commit messages.
Once your work is complete and you are ready to push your changes to the remote repository, you will first pull all changes from the remote repository.
If none of the files you have worked on have been modified since your last pull, that's great. If they have you will get some merge warnings and may have to manually decide how to merge your changes into those that have already been committed to the remote repository. Git may be able to do this for you, but if it's not sure it will give you a lot of help, telling you the sections of code that need to be dealt with.
Once any issues are corrected, you can push your changes to the remote repository. In this case we will push the changes from our local repository (origin) to the remote master branch (master).
git push -u origin master
For more information see:
Hope this helps. Regards Tim...