This is an old revision of the document!


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 diccover “stuff”….

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 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 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.

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

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

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.
$

Use the credential cacher to avoid repeated requests for username and passwords.

$ git config --global credential.helper cache

git status

Check files which need commiting:-

$ git status
On branch master
Your branch is up-to-date with 'origin/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 'origin/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 origin master
Username for 'https://github.com': UserName
Password for 'https://UserName@github.com': 
Branch master set up to track remote branch master from origin.
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 origin 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 origin.
$
 
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
$ 

Pulling existing repo to local machine

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.1537200004.txt.gz · Last modified: 17/09/2018 17:00 by andrew