Git Cheatsheet

Don't believe anything here…. :-} At the moment this is just for my benefit and is being added to and edited as I discover “stuff”….

Usernames and passwords

It's recommended that two factor authentication is set up for Github accounts. This does introduce a few subtle gotchas:-

  1. If 2fa is enabled, your github username and password used to log in to the web portal will not work for git clone https://github.com/YOUR_REPO, this will return remote: Invalid username or password. The solution is to use a Personal Access Token, this is set up in the security section of the Github website config pages. It is described here:- https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
  2. If you have two repos on a machine (OsX) which need two different logins, modify the repo URL to include the username to log in as, this is set in .git/config (eg. url = https://username@github.com/username/pythonapp.git), otherwise the github.com keychain access logins will be used. This may be true for linux too.

Starting a new repo

git config

$ git config --global user.email me@mycomp.co.uk
$ git config --global user.name Real Name

git init

Creates an empty or reinitialise an existing repo, this writes the .git directory and adds appropriate subdirectories.

$ git init
Initialised empty Git repository in /home/andrew/ansible/testplays/cis-awsLinux2/.git/

git branch

git branch <name> create new branch
git branch -b <name> create new branch and checkout
git branch List branches
git branch -a List all branches, local and remote
git btanch -d <branch> safe delete branch id merged ok
git branch -D <branch> delete *without* warnings
git push origin –delete <branch> push deletes to remote master

git checkout

$ git checkout ajs/emptyDirWarning
Switched to branch 'ajs/emptyDirWarning'

$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

Switch to different branch

git add

Add file to local repo, either single file:-

$ git add README.md

Or files which match an expression:-

$ git add *

Also rm removes files from the working tree before commit

$ rm testfile2 
$
$ git rm testfile2

git commit

$ git commit -m "initial commit"
[master (root-commit) 5730eea] initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
$

-m is a message stored to illustrate the changes being committed. If you forget the -m, git will open up an editor for you to create one. If you cancel, the commit is cancelled.

Don't forget that changed files need to be added (git add) before the commit will work!

git commit -a will add all files changed, or git commit <file> -m “comment” to add just one file.

git remote add

Adds a remote named <name> for the repository at <url>. Convention seems to be using origin, but you could use a more descriptive name, eg. codeCommit or gitHub. I think this would allow you to set two remotes and push to either or.

git remote add [-t <branch>] [-m <master>] [-f] [–[no-]tags] [–mirror=<fetch|push>] <name> <url>

$ git remote add origin https://github.com/user/RepoName.git
or
$ git remote add codeCommit https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/RepoName

git remote delete

Deletes a remote URL

$ git remote rm CodeCommit

Verify remote URL

$ git remote -v

git push

$ git push -u origin master
Username for 'https://github.com': UserName
Password for 'https://UserName@github.com': <Hidden> (needs to be Personal Access Token for 2fa access)
Counting objects: 3, done.
Writing objects: 100% (3/3), 246 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/mycorp/repo1.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
$

AWS CodeCommit *REQUIRES* a “helper”, see Amazon Web Services CLI

$ git config --global credential.helper cache

If this doesn't work it may fail with a message:-

error: src refspec master does not match any.
error: failed to push some refs to 'origin'

The reason for this is that there is only a local master branch created when commits have been done, so create or touch a file, add it and then repush to remote.

Push local branch to remote repo:-

git push --set-upstream origin name-of-localBranch

git pull

$ git pull codeCommit
remote: Counting objects: 3, done.
Unpacking objects: 100% (3/3), done.
From https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/REPO_NAME
   4cec434..32ac5a2  master     -> codeCommit/master
Updating 4cec434..32ac5a2
Fast-forward
 FileName | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

git log

Use git log to show the last commits:-

me@puppet:~/$ git log
commit 15fb2ac3ec05c423465334526c38fd7705fb13033
Author: A* <me@mydomain.co.uk>
Date:   Mon Oct 8 17:26:48 2018 +0100

    Initial commit
me@puppet:~/$

git status

Check files which need commiting:-

$ git status
On branch master
Your branch is up-to-date with 'gitHub/master'.
nothing to commit, working directory clean
$ 

Following any local changes, this will show what needs to be committed:-

$ git status
On branch master
Your branch is up-to-date with 'gitHub/master'.
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:   roles/cisHardening/tasks/section_06_level1.yml
 
no changes added to commit (use "git add" and/or "git commit -a")
$ 

Adding some new files to existing repo

In summary:-

  1. git add *
  2. git push -u origin master
  3. git commit -m “File commit”
  4. git push -u origin master
  5. git status

Transcript:-

$ git add *
 
$ git push -u gitHub master
Username for 'https://github.com': UserName
Password for 'https://UserName@github.com': 
Branch master set up to track remote branch master from gitHub.
Everything up-to-date
$
 
$ git commit -m "File commit"
[master a0ed44c] File commit
 43 files changed, 2580 insertions(+)
 create mode 100644 ansible_id_rsa
 create mode 100644 examples.txt
 create mode 100644 hosts-ansible.ans
 create mode 100644 main.yml
...edited....
 create mode 100644 roles/updateSystem/vars/main.yml
 create mode 100644 test.yaml
$ 
$ 
$ git push -u gitHub master
Username for 'https://github.com': UserName
Password for 'https://UserName@github.com': 
Counting objects: 56, done.
Compressing objects: 100% (48/48), done.
Writing objects: 100% (56/56), 18.07 KiB | 0 bytes/s, done.
Total 56 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), done.
To https://github.com/UserName/repo1.git
   5730eea..a0ed44c  master -> master
Branch master set up to track remote branch master from gitHub.
$
 
$ git status
On branch master
Your branch is up-to-date with 'gitHub/master'.
nothing to commit, working directory clean
$ 

Pulling existing repo to local machine

List all branches available

$ git branch -a
* master
  remotes/codeCommit/Devel
  remotes/codeCommit/add-integration
  remotes/codeCommit/add-scan

git clone

This will pull all the branches from the remote repo to the local repo.

config@app-test:~$ git clone https://github.com/mycorp/repo1.git
Cloning into 'repo1'...
Username for 'https://github.com': user
Password for 'https://user@github.com': 
remote: Counting objects: 5337, done.
remote: Compressing objects: 100% (88/88), done.
Receiving objects: 100% (5337/5337), 145.80 MiB | 638 KiB/s, done.
Resolving deltas: 100% (3445/3445), done.
config@app-test:~$ 
config@app-test:~$ ls -l
total 4
drwxr-xr-x 12 config config 4096 Nov 16 15:13 repo1
config@app-test:~$ 

git pull

This will update an existing local repo with changes.

config@server1:~/repo1$ git pull
Username for 'https://github.com': user
Password for 'https://user@example.com': 
remote: Counting objects: 185, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 185 (delta 73), reused 71 (delta 71), pack-reused 105
Receiving objects: 100% (185/185), 2.91 MiB | 1.76 MiB/s, done.
Resolving deltas: 100% (129/129), completed with 43 local objects.
From https://github.com/mycorp/repo1
   b18c497..31d0b7f  develop    -> origin/develop
 * [new branch]      feature/debug-thumbnail -> origin/feature/debug-thumbnail
   a414060..9ef89a8  master     -> origin/master
 * [new tag]         v3.11.5    -> v3.11.5
Auto-merging src/tests/Generator.h
Merge made by the 'recursive' strategy.
 scripts/status.sh                           |    2 +-
 src/Generator.cpp                  |   89 +++++---------

....edited....

 src/tests/Request.h                  |   35 ++++++
 55 files changed, 873 insertions(+), 466 deletions(-)
 create mode 100644 src/tests/FileUtilTest.cpp
 create mode 100644 src/tests/FingerprintProcessorTest.cpp
 create mode 100644 src/tests/IntegrationTests/RtfpdTestBase.py
 mode change 100644 => 100755 src/tests/IntegrationTests/StartIntegrationTests.py
 create mode 100755 src/tests/IntegrationTests/testBlackMegicCardLogging.py
 create mode 100755 src/tests/build_scripts/grepLSOF.sh
 create mode 100644 src/tests/s/AudioFingerprintGenerator.h
 rename src/tests/{DataGeneratorTest.h => s/DataGenerator.h} (83%)
 create mode 100644 src/tests/s/FingerprintProcessor.h
 create mode 100644 src/tests/s/GeneratorCollection.h
 create mode 100644 src/tests/s/Request.h
config@server1:~/repo1$

git status

  
config@server1:~/repo1/scripts$ git status
# On branch develop
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   fpamenu.sh
#
# 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:   menu.sh
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test-printf.sh
#       test-setip.sh
config@server1:~/repo1/scripts$ 

git add, commit and push

Same as the above content git add, add new files to the local repo with add, commit to the local repo and then push to the remote repo.

Restoring files which are deleted

use this to find the commit id of the last commit where the file was deleted:-

$ git rev-list -n 1 HEAD -- ansible_id_rsa
3d83c24b89d7b27aecf99fecef54982d3ecf0620
$

Then use this commit id to checkout the file from a specific point in time:-

$ git checkout 3d83c24b89d7b27aecf99fecef54982d3ecf0620^ -- ansible_id_rsa 

AWS CodeCommit

$ aws codecommit create-repository --repository-name CIS-Hardening-AWS_Linux2 --repository-description "Repo for ansible code to harden aws Linux2 image."
{
    "repositoryMetadata": {
        "repositoryDescription": "Repo for ansible code to harden aws Linux2 image.",
        "cloneUrlSsh": "ssh://git-codecommit.eu-west-2.amazonaws.com/v1/repos/CIS-Hardening-AWS_Linux2",
        "repositoryId": "91fb59e3-833e-4705-8e1c-xxxxx",
        "lastModifiedDate": 1537199788.236,
        "accountId": "1234567789",
        "repositoryName": "CIS-Hardening-AWS_Linux2",
        "Arn": "arn:aws:codecommit:eu-west-2:581230658448:CIS-Hardening-AWS_Linux2",
        "cloneUrlHttp": "https://git-codecommit.eu-west-2.amazonaws.com/v1/repos/CIS-Hardening-AWS_Linux2",
        "creationDate": 1537199788.236
    }
}

rb/git-cheatsheet.txt · Last modified: 20/05/2019 09:42 by andrew