====== 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:- - 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/]] - 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. 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) ===== Add Git Branch to PS1 prompt ===== 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 (abc-123) $ ===== Starting a new repo ===== ==== git config ==== $ 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 ..." to include in what will be committed) fullstack-expt/.terraform/ nothing added to commit but untracked files present (use "git add" to track) ==== 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 clone ==== Pulls a repo down from '''' git clone ==== git branch (create and delete) ==== === Adding and listing branches === | git branch | create new branch | | git checkout -b | create new branch and checkout | | git branch | List branches | | git branch -a | List all branches, local and remote | === Deleting local and remote branches === | ''git branch -d '' | safe delete branch id merged ok | | ''git branch -D '' | delete *without* warnings | | ''git push origin --delete '' | deletes remote branch only | ==== git checkout ==== 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 ==== Create new, empty 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 ==== 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 -m "comment"'' to add just one file. ==== git remote add ==== Adds a remote named for the repository at . 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 ] [-m ] [-f] [--[no-]tags] [--mirror=] '' $ 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': (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 [[aws:aws-cli#roles_with_codecommit|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(-) 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 ==== ''git fetch'' will get updates from the origin, but will not merge with any local changes. ==== git log ==== Use ''git log'' to show the last commits:- me@puppet:~/$ git log commit 15fb2ac3ec05c423465334526c38fd7705fb13033 Author: A* 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 ..." to update what will be committed) (use "git checkout -- ..." 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") $ ==== git tag ==== 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 ==== git diff ==== This works even for files outside any repo. git diff --no-index -- file.a file.b ===== Adding some new files to existing repo ===== In summary:- - git add * - git push -u origin master - git commit -m "File commit" - git push -u origin master - 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 rebase ==== 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 ==== git status ==== config@server1:~/repo1/scripts$ git status # On branch develop # Changes to be committed: # (use "git reset HEAD ..." to unstage) # # new file: fpamenu.sh # # Changes not staged for commit: # (use "git add ..." to update what will be committed) # (use "git checkout -- ..." to discard changes in working directory) # # modified: menu.sh # # Untracked files: # (use "git add ..." 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 [[rb:git-cheatsheet#git_add|git add]], add new files to the local repo with ''add'', commit to the local repo and then push to the remote repo. ==== git actions with specific ssh key ==== Use git environment variable GIT_SSH_COMMAND:- $ GIT_SSH_COMMAND='ssh -i ~/.ssh/bitbucket' git push --set-upstream origin ajs/mybranch ===== 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 ===== Backing out changes ===== 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 '' 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!! ===== Local master is ahead of remote master ===== 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'. ====== Git Aliases ====== 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 ====== AWS CodeCommit ====== See page on AWS Codecommit with a credential helper:- [[aws:aws-cli#aws_codecommit|AWS CodeCommit cli]] AWS [[aws:aws-cli|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: {{counter|today}} \\ Yesterday: {{counter|yesterday}} \\ Until now: {{counter|total}} \\