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”….
It's recommended that two factor authentication is set up for Github accounts. This does introduce a few subtle gotchas:-
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/You can save the password or PAT manually:-
url = https://username:password@github.com/username/pythonapp.git
You can add a username and password at the time you do a clone with:-
$ git clone https://username:pers-acc-tok@repo-url.com/pathto/your-repo.git
An alternative way is to use a credential store:-
$ git config credential.helper store or $ git config --global credential.helper store
Your creds will be stored in your home directory (~/.git-credentials)
Thanks Dharmesh for the info! You know who you are!
In your .bash_profile
in your home directory, add:-
git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/' } export PS1="\u@\h \W\[\033[32m\]\$(git_branch)\[\033[00m\]$ "
This gives:-
laptop economy-7 <fc #00ff00>(abc-123)</fc> $
$ git config --global user.email me@mycomp.co.uk $ git config --global user.name Real Name
You can see the current settings in force with:-
$ git config --list core.excludesfile=~/.gitignore core.legacyheaders=false ...edited... color.ui=auto color.interactive=auto alias.s=status alias.a=!git add . && git status
Note this also shows aliases which are useful shorthand:-
$ git s On branch master Your branch is up to date with 'github/master'. Untracked files: (use "git add <file>..." to include in what will be committed) fullstack-expt/.terraform/ nothing added to commit but untracked files present (use "git add" to track)
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/
Pulls a repo down from <URL>
git clone <url>
git branch <name> | create new branch |
git checkout -b <name> | create new branch and checkout |
git branch | List branches |
git branch -a | List all branches, local and remote |
git branch -d <branch> | safe delete branch id merged ok |
git branch -D <branch> | delete *without* warnings |
git push origin –delete <branch> | deletes remote branch only |
Create a new branch and checkout
$ git checkout -b ajs/newBranch
Switch to different branch
$ git checkout ajs/emptyDirWarning Switched to branch 'ajs/emptyDirWarning'
And back to master branch
$ git checkout master Switched to branch 'master' Your branch is up-to-date with 'origin/master'.
Also, to create a local branch from a remote one try this:-
git checkout -t remotes/repo/branch
This will create a new branch not associated with any existing branches. Useful for entirely new code where you don't need to base your branch on any existing code.
$ git switch --orphan master-tmp
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 -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.
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.
Setting two remotes seems a way to move a repo from one repository host to another (eg. Github to AWS Code Commit).
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
Deletes a remote URL
$ git remote rm CodeCommit
$ git remote -v
$ 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. $
If you have a local branch and a remote which do not share a common history, Git will refuse to merge them. Generally, this is a good idea, but if you know you are right and Git is wrong, you can use –allow-unrelated-histories
to force this push. This will merge the upstream branch with your changes. It may not be what you expect, so this is a “not used often” command.
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 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(-)
Note, git fetch
will get updates from the origin, but will not merge with any local changes. git pull
is the same as git fetch
followed by a git merge
. git fetch
will never change your local copy or branch, but git pull
will update your local branch to it's remote version, which you may not want.
git fetch
will get updates from the origin, but will not merge with any local changes.
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:~/$
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") $
Create a release and add a tag to it, this creates a new HEAD from master which cannot be modified further.
$ git tag -a 0.1.1 -m "Updated Tags" $ git push --tags ...work... $ git tag -a 1.0.0 -m "Updated Tags, ready for production." $ git push --tags $ git tag -n 1.0.0 Updated Tags ready for production. v0.1.0 Merge pull request #9 from JSainsburyPLC/ajs/TagAddition v0.1.1 Updated Tags
This works even for files outside any repo.
git diff --no-index -- file.a file.b
In summary:-
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 $
$ git branch -a * master remotes/codeCommit/Devel remotes/codeCommit/add-integration remotes/codeCommit/add-scan
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:~$
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$
Use this to merge updates/changes in Master to local branch.
git status git checkout master git pull git checkout ajs/branchName git rebase origin/master git status Resolve any conflicts here. Add changed files again. git depermission.py git status git rebase --continue
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$
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.
Use git environment variable GIT_SSH_COMMAND:-
$ GIT_SSH_COMMAND='ssh -i ~/.ssh/bitbucket' git push --set-upstream origin ajs/mybranch
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
Problem:- On local master branch by mistake and attempt to merge local branch to it, meant to merge one local branch with another.
Solution:- running git merge <branchname>
whilst master is checked out merged changes to the wrong place, to revert master back to as it was, I ran git reset –hard origin/master
NOTE –hard
can delete local files you may wish to keep!! Excercise caution!!
Very simiar fix to above, somehow local master was ahead of local, a reset –hard
was needed:-
$ git status On branch master Your branch is ahead of 'origin/master' by 5 commits. (use "git push" to publish your local commits) $ git reset --hard origin/master HEAD is now at xxxx Pull request #24: Minor fixes plus renaming $ git status On branch master Your branch is up to date with 'origin/master'.
from ~/.gitconfig
:-
[alias] lga = log --graph --oneline --all --decorate co = checkout lg1 = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar) %G? %C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
Thanks to John Buxton for the lg1 alias
Viewing aliases in effect:-
$ git alias ... edited ... cm => commit -m co => checkout d => diff ... s => status
See page on AWS Codecommit with a credential helper:- AWS CodeCommit cli
AWS cli code to create a repository in 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 } }
This page has been accessed for:-
Today: 1
Yesterday: 0
Until now: 778