Using Git - rajivkanaujia/alphaworks GitHub Wiki

TLDR

$ git status
$ git checkout -b rajiv_001
$ git pull origin rajiv_001
$ git add .
$ git commit -m "Images for the wiki"

Set the upstream

$ git push --set-upstream origin rajiv_001

Push the new feature branch to the remote repo

$ git push origin rajiv_001

Create a pull request using the URL in push command
Merge the Pull Request
Confirm Merge
Delete the Private Branch if you are not going to use it in future

$ git status
$ git fetch
$ git merge origin/master
$ git branch
$ git checkout master
$ git pull

# Background # Everyone time someone tries to use source control (like github etd), there is unfortunate fear of using git commands to get the work done. Lets try to simplify the situation. Also, I don't want to keep commit (change) histroy of certain files. There is easy path forward.

Objective

  1. Create private branch
  2. Make changes
  3. Checkin and commit to private branch
  4. Do a pull request for others to (re)view and approve
  5. Merge the pull request to the main branch

How will it be done?

Locally

  1. We are in Master Branch by default, checkout a new branch
    $ git checkout -b <New Branch Name>
    
  2. Make the changes in the code
  3. Stage files for commit
    • To add single file for commit
      $ git add <filename>
      
    • To add All Files for commit
      $ git add .
      
    • Commit as
      $ git commit -m “<Give Message>”
      
  4. Now we want to push the local branch to the remote. If Branch is not present remotely, new one will be created
    $ git push origin <New Branch Name or same name>
    

Remote :

  1. New branch gets created remotely.
  2. Create a PR to compare the newly created branch and the Master.
  3. Share the PR for Approval and Merge the PR.
  4. You can see the new code changes on the Master Branch.
  5. Delete the newly created branch on remote, or continue using it as your private branch (do not forget to fetch and merge private branches)

Deleting wrong commit :

Delete last commit and push to master

$ git reset --hard HEAD~1
$ git push origin master --force

Example

Add an image file. Note that this example uses newly imaged laptop so some steps will fail for the first time.

Setting up new private branch and working in it

Checking Status

One file under folder "images" has been locally added.

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	images/apache_001.png

nothing added to commit but untracked files present (use "git add" to track)

Creating Private Branch

Now, you can make many changes and keep adding them to private branch and merge to main when ready

$ git checkout -b rajiv_001
Switched to a new branch 'rajiv_001'
$ git status
On branch rajiv_001
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	images/apache_001.png

nothing added to commit but untracked files present (use "git add" to track)

Adding files to the Private Branch

$ cd images/
$ git status
On branch rajiv_001
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	apache_001.png

nothing added to commit but untracked files present (use "git add" to track)

Adding all files under folder images. You can name the file individually, if needed.

$ git add .
$ git status
On branch rajiv_001
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   apache_001.png

Moving to the main folder and checking status again

$ cd ..
$ git status
On branch rajiv_001
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   images/apache_001.png

Commit your changes to the Private Branch

If this is the first time you are going this, and your git config is not properly set, you will get errors with instructions on how to fix it. You can perform internet search to find a regulation for the errors.

$ git commit -m "Images for the wiki"

*** Please tell me who you are.

Run

  git config --global user.email "[email protected]"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: empty ident name (for <>) not allowed
$ git config --global user.email raj**********.com
$ git config --global user.name "Rajiv Kanaujia"

Try commit one more time

$ git commit -m "Images for the wiki"
[rajiv_001 270b833] Images for the wiki
 1 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 images/apache_001.png

At this stage, your changes are in your private branch locally. You have to push the changes to the origin

Push to Origin and get Pull-Request URL

$ git push --set-upstream origin rajiv_001
Enumerating objects: 15, done.
Counting objects: 100% (15/15), done.
Delta compression using up to 12 threads
Compressing objects: 100% (13/13), done.
Writing objects: 100% (13/13), 2.79 MiB | 753.00 KiB/s, done.
Total 13 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'rajiv_001' on GitHub by visiting:
remote:      https://github.com/rajivkanaujia/alphaworks/pull/new/rajiv_001
remote:
To https://github.com/rajivkanaujia/alphaworks
 * [new branch]      rajiv_001 -> rajiv_001
Branch 'rajiv_001' set up to track remote branch 'rajiv_001' from 'origin'.

Create a pull request

Visit the mentioned URL in the earlier step and create a pull request. Depending on the git setup, you may have one or more reviewer/approver needed for the pull request before you can merge it to master. I am assuming you get the picture on how it will work.

Use https://github.com/rajivkanaujia/alphaworks/pull/new/rajiv_001 to create a pull request

Create a pull request

Merge the Pull Request

Confirm Merge

Delete the Private Branch if you are not going to use it in future. I intent to keep using it, so I will skip the delete

Check the status

$ git status
On branch rajiv_001
Your branch is up to date with 'origin/rajiv_001'.

nothing to commit, working tree clean

Visual of what happened using Sourcetree

As you work, keep updating the private branch from the master

$ git fetch
$ git merge origin/master
Updating 270b833..e7cb8c3
Fast-forward

Switch to main branch and update

$ git status
On branch rajiv_001
Your branch is up to date with 'origin/rajiv_001'.

nothing to commit, working tree clean
$ git branch
  master
* rajiv_001
$ git checkout master
Switched to branch 'master'
Your branch is behind 'origin/master' by 4 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)
$ git pull
Updating ea5534c..c68ebe1
Fast-forward
 images/apache_001.png    | Bin 0 -> 37518 bytes
$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
$ git branch
* master
  rajiv_001

Switch to private branch

$ git checkout rajiv_001
Switched to branch 'rajiv_001'
Your branch is up to date with 'origin/rajiv_001'.

$ git branch
  master

Working with Wiki

$ git pull
$ git checkout -b wiki_001
$ git branch
  master
* wiki_001
$ git status
On branch wiki_001
nothing to commit, working tree clean

Make changes Check what changed

$ git status

Commit changes

$ git commit -m "Modified images"
$ git push --set-upstream origin wiki_001
$ git checkout master
$ git merge wiki_001
$ git push
$ git status

Deleting Change/Commit History TL/DR

git checkout --orphan temp_branch
git add -A
git commit -am "git history purge commit"
git branch -D master
git branch -m master
git push -f origin master

Note: If you like the instructions here, please refer it on your posts/documentation. Contact me if there are corrections needed.

⚠️ **GitHub.com Fallback** ⚠️