One Stop Workshop for Git
Git to GitHub, AWS CodeCommit, S3 Bucket and more
Recently I worked a number of projects, which heavily rely on git to push our files from local environment to remote repo such as GitHub, AWS CodeCommit, S3 Bucket. I thought it would be helpful if I collect and put them together so that we won’t be lost in research in regards to git command lines.
Let us start from Git itself.
Commonly used
git status
— This command lists all the files that have to be committed.
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
newfile
nothing added to commit but untracked files present (use "git add" to track)
git config
— This command records or snapshots the file permanently in the version history.
This command sets the author name and email address respectively to be used with your commits.
$ git config --global user.name "lightinglife"
$ git config --global user.name "zhaofeng87@gmail.com"
$ git config --list
credential.helper=osxkeychain
user.name=zhaofeng87@gmail.com
user.email=zhaofeng8711@gmail.com
.
.
.
git init
— This command is used to start a new repository.
$ git init
Initialized empty Git repository in /Users/paulzhao/gitdemo/.git/
git add .
— This command files in current folder to the staging area.
$ git add .
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: newfile
git commit -am “<comment as you like>”
— This command records or snapshots the file permanently in the version history and add files to staging area simultaneously.
$ git commit -am "comment demo"
[master (root-commit) 89792ba] comment demo
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 newfile
git push
— This command sends the committed changes of master branch to your remote repository.
Examples will be shown in different remote repos after pushing.
git pull
— This command fetches and merges changes on the remote server to your working directory.
We will dive deep when speaking about remote repos
git clone
— This command is used to obtain a repository from an existing URL.
$ git clone https://github.com/lightninglife/sls-notes-backend.git
Cloning into 'sls-notes-backend'...
remote: Enumerating objects: 19, done.
remote: Counting objects: 100% (19/19), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 19 (delta 7), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (19/19), 15.03 KiB | 15.03 MiB/s, done.
Resolving deltas: 100% (7/7), done.
$ ls
newfile sls-notes-backend
git branch
— This command lists all the local branches in the current repository.
$ git branch
* master
git checkout master/ dev/ prod
— This command is used to switch from one branch to another.
How you create a workstation and switch to it
$ git checkout -b dev
Switched to a new branch 'dev'
$ git checkout -b prod
Switched to a new branch 'prod'
switch in between different workstations
$ git checkout master
Switched to branch 'master'
$ git checkout dev
Switched to branch 'dev'
$ git checkout prod
Switched to branch 'prod'
git merge
— This command merges the specified branch’s history into the current branch.
We will deal with this command when discussing about remote repos
git remote
— This command is used to connect your local repository to the remote server.
We will further discuss this command when touching upon remote repos
Good to know ones
git diff
Usage: git diff
This command shows the file differences which are not yet staged.
Usage: git diff –staged
This command shows the differences between the files in the staging area and the latest version present.
Usage: git diff [first branch] [second branch]
This command shows the differences between the two branches mentioned.
git reset
Usage: git reset [file]
This command unstages the file, but it preserves the file contents.
Usage: git reset [commit]
This command undoes all the commits after the specified commit and preserves the changes locally.
Usage: git reset –hard [commit]
This command discards all history and goes back to the specified commit.
git rm
Usage: git rm [file]
This command deletes the file from your working directory and stages the deletion.
git log
Usage: git log
This command is used to list the version history for the current branch.
Usage: git log –follow[file]
This command lists version history for a file, including the renaming of files also.
git show
Usage: git show [commit]
This command shows the metadata and content changes of the specified commit.
git tag
Usage: git tag [commitID]
This command is used to give tags to the specified commit.
Remote Repos
To push files to S3 Bucket and CodeCommit, you need to meet prerequisites
1 AWS Account
Creating a non-root user
Based on AWS best practice, root user is not recommended to perform everyday tasks, even the administrative ones. The root user, rather is used to to create your first IAM user, groups and roles. Then you need to securely lock away the root user credentials and use them to perform only a few account and service management tasks.
Notes: If you would like to learn more about why we should not use root user for operations and more about AWS account, please find more here.
2 To more effectively manipulate AWS resources, we may consider to install awscli
For mac user, please install Homebrew
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
For Windows user, please visit here.
As we installed Homebrew, let us install awscli
$ brew install awscli
$ aws --version
aws-cli/2.0.22 Python/3.7.4 Darwin/19.6.0 botocore/2.0.0dev26
Notes: We need to have providers setup for AWS
$ export AWS_ACCESS_KEY_ID=AKIAWYH7TZJJ7HMPWX4W
$ export AWS_SECRET_ACCESS_KEY=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
Create a S3 Bucket using awscli
$ aws s3api create-bucket --bucket gits3bucketdemo --region us-east-1
{
"Location": "/gits3bucketdemo"
}
Verify S3 bucket using cli
$ aws s3 ls
2020-04-24 23:42:17 app-build-refmygomzayu
2020-05-01 17:25:56 bitbucket-codedeploy-bucket
2020-06-17 02:54:12 cf-templates-nuwohw9d3wgw-us-east-1
2020-06-14 21:13:32 cf-templates-nuwohw9d3wgw-us-west-2
2020-06-14 21:18:30 cloudformation-for-cloudtrail-config-guardduty
2020-06-14 21:25:10 cloudformationforcloudtrailconfigandguardduty
2020-07-18 15:11:25 codepipeline-us-west-2-667252476270
2020-04-24 23:42:17 eg-staging-app
2020-04-24 23:42:17 eg-staging-app-build-gqgllfrhzolh
2020-05-14 00:50:56 elasticbeanstalk-ca-central-1-464392538707
2020-05-14 16:34:27 elasticbeanstalk-us-east-1-464392538707
2020-07-23 21:29:17 gits3bucketdemo
2020-06-15 16:18:37 my-kubernetes-project-hash-kops-kube-bucket
2020-04-24 23:42:17 my-repo-for-aws-terraform-project
2020-04-28 15:08:47 my-terraform-bucket-with-unique-name
2020-04-30 16:21:14 mycicdbucketwithbitbucket
2020-07-17 20:38:06 sls-notes-backend-prod-serverlessdeploymentbucket-1f61fc2059hzz
Verify S3 bucket in AWS console
To copy file from local environment to S3 bucket created
$ aws s3 cp myfile s3://gits3bucketdemo/ --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers full=emailaddress=user@example.com
upload: ./myfile to s3://gits3bucketdemo/myfile
Verify myfile in gits3bucketdemo
For more, please visit here.
Now let us focus on CodeCommit
Since CodeCommit is region specific, we need to setup providers shown below
$ export AWS_ACCESS_KEY_ID=AKIAWYH7TZJJ7HMPWX4W
$ export AWS_SECRET_ACCESS_KEY=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
$ export AWS_DEFAULT_REGION=us-west-2
Verify CodeCommit in AWS console
First time to set up a remote repo in this case CodeCommit
$ git push --set-upstream origin master
The config profile (CodeCommitProfile) could not be found
Username for 'https://git-codecommit.us-west-2.amazonaws.com/v1/repos/gitcodecommitrepo': adminuser-at-464392538707
Password for 'https://adminuser-at-464392538707@git-codecommit.us-west-2.amazonaws.com/v1/repos/gitcodecommitrepo':
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 214 bytes | 214.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://git-codecommit.us-west-2.amazonaws.com/v1/repos/gitcodecommitrepo
* [new branch] master -> master
Verify in AWS console
We created a file named oldfile in CodeCommit and pull from remote repo to local repo
$ git pull
remote: Counting objects: 3, done.
Unpacking objects: 100% (3/3), 262 bytes | 131.00 KiB/s, done.
From https://git-codecommit.us-west-2.amazonaws.com/v1/repos/gitcodecommitrepo
52b7a8b..6d859b7 master -> origin/master
Merge made by the 'recursive' strategy.
oldfile | 1 +
1 file changed, 1 insertion(+)
create mode 100644 oldfile
$ ls
oldfile
We now will revisit git merge
We checkout which branch we are in now, then switch to dev
$ git branch
dev
* master
prod
$ git checkout dev
Switched to branch 'dev'
As we merge master in dev, there will be files added to our local workstation dev
$ git merge master
Updating 89792ba..3b50a56
Fast-forward
newfile | 0
oldfile | 1 +
2 files changed, 1 insertion(+)
delete mode 100644 newfile
create mode 100644 oldfile
Now we are able to push it to CodeCommit as a workstation called dev
$ git push origin dev
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 470 bytes | 470.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
To https://git-codecommit.us-west-2.amazonaws.com/v1/repos/gitcodecommitrepo
* [new branch] dev -> dev
Verify in our CodeCommit, dev workstation is established
Touch upon the last command git remote
To add git remote repo we apply
$ git remote add origin master https://git-codecommit.us-west-2.amazonaws.com/v1/repos/gitcodecommitrepo
Verify your remote repo
$ git remote -v
origin https://git-codecommit.us-west-2.amazonaws.com/v1/repos/gitcodecommitrepo (fetch)
origin https://git-codecommit.us-west-2.amazonaws.com/v1/repos/gitcodecommitrepo (push)
Git to GitHub
The only difference in between AWS Repos and GitHub is the url we need to provide
e.g.
For git remote
$ git remote add tester https://github.com/lightninglife/reactapp.git ### lightninglife is your account name and reactapp is your repo name
Conclusion:
We touched upon almost every aspect of git functions throughout this project. Hopefully it will serve as a tool kit for folks who may use git on a daily basis.