Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
rb:git-cheatsheet [19/09/2018 16:33]
andrew [git push]
rb:git-cheatsheet [18/09/2019 16:41] (current)
andrew [git config]
Line 1: Line 1:
 ====== Git Cheatsheet ====== ====== 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"​....+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. 
 + 
 + 
 ===== Starting a new repo ===== ===== Starting a new repo =====
  
Line 10: Line 21:
   $ git config --global user.email me@mycomp.co.uk   $ git config --global user.email me@mycomp.co.uk
   $ git config --global user.name Real Name   $ git config --global user.name Real Name
 +
 +You can see the current settings in force with:-
 +
 +<​code>​
 +$ 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
 +</​code>​
 +
 +Note this also shows aliases which are useful shorthand:-
 +
 +<​code>​
 +$ 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)
 +</​code>​
  
  
Line 18: Line 57:
   $ git init   $ git init
   Initialised empty Git repository in /​home/​andrew/​ansible/​testplays/​cis-awsLinux2/​.git/​   Initialised empty Git repository in /​home/​andrew/​ansible/​testplays/​cis-awsLinux2/​.git/​
 +
 +
 +
 +==== git branch (create and delete) ====
 +
 +=== Adding and listing branches ===
 +
 +
 +| 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 |
 +
 +
 +=== Deleting local and remote branches ===
 +
 +
 +| ''​git branch -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 ====
 +
 +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'​.
 +
  
  
Line 36: Line 112:
   $   $
   $ git rm testfile2   $ git rm testfile2
 +
  
 ==== git commit ==== ==== git commit ====
- 
  
 <code git> <code git>
Line 51: Line 127:
  
 Don't forget that changed files need to be added (''​git add''​) before the commit will work! 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 ==== ==== 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.+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 [-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+  $ git remote add origin https://​github.com/​user/​RepoName.git
   or   or
-  $ git remote add codeCommit https://​git-codecommit.eu-west-1.amazonaws.com/​v1/​repos/​CIS-Hardening-AWS_Linux2+  $ git remote add codeCommit https://​git-codecommit.eu-west-1.amazonaws.com/​v1/​repos/​RepoName
  
  
Line 89: Line 169:
 </​code>​ </​code>​
  
-Use the credential cacher to avoid repeated requests for username and passwords.+ 
 + 
 +AWS CodeCommit *REQUIRES* a "​helper",​ see [[rb:​aws-cli#​roles_with_codecommit|Amazon Web Services CLI]] 
  
   $ git config --global credential.helper cache   $ git config --global credential.helper cache
Line 100: Line 183:
 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. 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 ====
 +
 +<​code>​
 +$ 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(-)
 +</​code>​
 +
 +
 +
 +==== git log ====
 +
 +Use ''​git log''​ to show the last commits:-
 +
 +<​code>​
 +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:​~/​$
 +</​code>​
  
 ==== git status ==== ==== git status ====
Line 107: Line 225:
 $ git status $ git status
 On branch master On branch master
-Your branch is up-to-date with 'origin/​master'​.+Your branch is up-to-date with 'gitHub/​master'​.
 nothing to commit, working directory clean nothing to commit, working directory clean
  
Line 117: Line 235:
 $ git status $ git status
 On branch master On branch master
-Your branch is up-to-date with 'origin/​master'​.+Your branch is up-to-date with 'gitHub/​master'​.
 Changes not staged for commit: Changes not staged for commit:
   (use "git add <​file>​..."​ to update what will be committed)   (use "git add <​file>​..."​ to update what will be committed)
Line 128: Line 246:
 </​code>​ </​code>​
  
 +
 +==== git tag ====
 +
 +Create a release and add a tag to it, this creates a new HEAD from master which cannot be modified further.
 +
 +<​code>​
 +$ 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
 +</​code>​
  
 ===== Adding some new files to existing repo ===== ===== Adding some new files to existing repo =====
Line 144: Line 281:
 $ git add * $ git add *
  
-$ git push -u origin ​master+$ git push -u gitHub ​master
 Username for '​https://​github.com':​ UserName Username for '​https://​github.com':​ UserName
 Password for '​https://​UserName@github.com': ​ Password for '​https://​UserName@github.com': ​
-Branch master set up to track remote branch master from origin.+Branch master set up to track remote branch master from gitHub.
 Everything up-to-date Everything up-to-date
 $ $
Line 163: Line 300:
  
  
-$ git push -u origin ​master+$ git push -u gitHub ​master
 Username for '​https://​github.com':​ UserName Username for '​https://​github.com':​ UserName
 Password for '​https://​UserName@github.com': ​ Password for '​https://​UserName@github.com': ​
Line 173: Line 310:
 To https://​github.com/​UserName/​repo1.git To https://​github.com/​UserName/​repo1.git
    ​5730eea..a0ed44c ​ master -> master    ​5730eea..a0ed44c ​ master -> master
-Branch master set up to track remote branch master from origin.+Branch master set up to track remote branch master from gitHub.
 $ $
  
 $ git status $ git status
 On branch master On branch master
-Your branch is up-to-date with 'origin/​master'​.+Your branch is up-to-date with 'gitHub/​master'​.
 nothing to commit, working directory clean nothing to commit, working directory clean
  
Line 194: Line 331:
  
  
 +==== List all branches available ====
  
 +
 +<​code>​
 +$ git branch -a
 +* master
 +  remotes/​codeCommit/​Devel
 +  remotes/​codeCommit/​add-integration
 +  remotes/​codeCommit/​add-scan
 +</​code>​
 ==== git clone ==== ==== git clone ====
  
Line 260: Line 406:
 config@server1:​~/​repo1$ config@server1:​~/​repo1$
 </​code>​ </​code>​
 +
 +
 +==== git rebase ====
 +
 +Use this to merge updates/​changes in Master to local branch.
 +
 +<code git>
 +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
 +</​code>​
 +
  
  

rb/git-cheatsheet.1537371203.txt.gz · Last modified: 19/09/2018 16:33 by andrew