Thursday, May 27, 2021

Introduction to GitHub branching in 10 commands



Introduction to GitHub branching in 10 commands: 

1. git clone [repo_url]
2. git branch [-r] [-a]
3. git branch --set-upstream-to
4. git pull
5. git checkout [-b] [branch_name]
6. git push origin HEAD:[branch_name]
7. git switch -c [branch_name]
8. git log
9. git clone --branch [branch_name] [repo_url]
10. git push --set-upstream origin [branch_name]

Why concept of branching using an image:
Now the commands:

1. INITIAL SETUP USING "git clone"

CMD>git clone https://github.com/ashishjain1547/repo_for_testing.git Cloning into 'repo_for_testing'... Logon failed, use ctrl+c to cancel basic credential prompt. Username for 'https://github.com': aj@gmail.com Password for 'https://aj@gmail.com@github.com': remote: Enumerating objects: 8, done. remote: Counting objects: 100% (8/8), done. remote: Compressing objects: 100% (5/5), done. remote: Total 8 (delta 1), reused 4 (delta 1), pack-reused 0 Unpacking objects: 100% (8/8), 4.98 KiB | 10.00 KiB/s, done. CMD>dir /b repo_for_testing CMD>cd repo_for_testing CMD\repo_for_testing>dir /b .gitignore LICENSE newFile.txt README.md CMD\repo_for_testing> type newFile.txt NEWFILE

2. LIST ALL THE BRANCHES

git branch: shows the local branches git branch -r: shows the remote branches git branch -a: shows all the branches (both local and remote) ~\aj> git clone https://github.com/ashishjain1547/repo_for_testing.git Cloning into 'repo_for_testing'... remote: Enumerating objects: 14, done. remote: Counting objects: 100% (14/14), done. remote: Compressing objects: 100% (8/8), done. remote: Total 14 (delta 4), reused 8 (delta 2), pack-reused 0 Unpacking objects: 100% (14/14), 5.41 KiB | 5.00 KiB/s, done. ~\aj> cd repo_for_testing ~\aj\repo_for_testing> git branch * main ~\aj\repo_for_testing> git branch -r origin/HEAD -> origin/main origin/main origin/test_branch ~\aj\repo_for_testing> git branch -a * main remotes/origin/HEAD -> origin/main remotes/origin/main remotes/origin/test_branch ~\aj\repo_for_testing> git checkout -b test_branch Switched to a new branch 'test_branch' Note: If you already have a branch named "test_branch" in your local, then you would not pass the argument "-b". ~\aj\repo_for_testing> git branch main * test_branch ~\aj\repo_for_testing> git branch -r origin/HEAD -> origin/main origin/main origin/test_branch Note: we would not see any changes in the remote branches from "git checkout" command. ~\aj\repo_for_testing> git branch -a main * test_branch remotes/origin/HEAD -> origin/main remotes/origin/main remotes/origin/test_branch

3. SUPPOSE YOUR NEW REMOTE BRANCH IS NOT VISIBLE IN CMD USING "git branch -r", THEN TO LOAD NEW REMOTE BRANCH TO SHOW UP IN AN EXISTING LOCAL REPO, DO A 'GIT PULL'

CMD\repo_for_testing> git branch -r origin/HEAD -> origin/main origin/main CMD\repo_for_testing> git pull From https://github.com/ashishjain1547/repo_for_testing * [new branch] test_branch -> origin/test_branch Already up to date. CMD\repo_for_testing> git branch -r origin/HEAD -> origin/main origin/main origin/test_branch

4. CREATING A NEW LOCAL BRANCH 'test_branch', SETTING UPSTREAM BRANCH FOR IT AND ADDING A NEW FILE IN IT

~\aj> git clone https://github.com/ashishjain1547/repo_for_testing.git Cloning into 'repo_for_testing'... remote: Enumerating objects: 19, done. remote: Counting objects: 100% (19/19), done. remote: Compressing objects: 100% (11/11), done. remote: Total 19 (delta 6), reused 12 (delta 3), pack-reused 0 Unpacking objects: 100% (19/19), 5.92 KiB | 6.00 KiB/s, done. ~\aj> cd repo_for_testing ~\aj\repo_for_testing>git branch * main ~\aj\repo_for_testing> git branch -r origin/HEAD -> origin/main origin/main origin/test_branch ~\aj\repo_for_testing> git branch -a * main remotes/origin/HEAD -> origin/main remotes/origin/main remotes/origin/test_branch 'git checkout -b test_branch' CREATES A NEW LOCAL BRANCH NAMED 'test_branch' BECAUSE OF THE ARGUMENT '-b' ~\aj\repo_for_testing> git checkout -b test_branch Switched to a new branch 'test_branch' ~\aj\repo_for_testing>git branch main * test_branch ~\aj\repo_for_testing>git branch -r origin/HEAD -> origin/main origin/main origin/test_branch ~\aj\repo_for_testing> git branch -a main * test_branch remotes/origin/HEAD -> origin/main remotes/origin/main remotes/origin/test_branch ~\aj\repo_for_testing> dir Volume in drive C is Windows Volume Serial Number is 8139-90C0 Directory of ~\aj\repo_for_testing 07/14/2021 04:06 PM DIR> . 07/14/2021 04:06 PM DIR> .. 07/14/2021 04:06 PM 368 .gitignore 07/14/2021 04:06 PM 11,558 LICENSE 07/14/2021 04:06 PM 11 newFile.txt 07/14/2021 04:06 PM 38 README.md 07/14/2021 04:06 PM 23 test_file_20210528.txt 5 File(s) 11,998 bytes 2 Dir(s) 62,644,645,888 bytes free ~\aj\repo_for_testing> git branch --set-upstream-to=origin/test_branch test_branch Branch 'test_branch' set up to track remote branch 'test_branch' from 'origin'. ~\aj\repo_for_testing> git pull Updating daa4600..82c204f Fast-forward 20210528_test_branch.txt | 1 + 202107141543.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 20210528_test_branch.txt create mode 100644 202107141543.txt ~\aj\repo_for_testing> echo "202107141608" > 202107141608.txt ~\aj\repo_for_testing> dir Volume in drive C is Windows Volume Serial Number is 8139-90C0 Directory of ~\aj\repo_for_testing 07/14/2021 04:08 PM DIR> . 07/14/2021 04:08 PM DIR> .. 07/14/2021 04:06 PM 368 .gitignore 07/14/2021 04:08 PM 30 20210528_test_branch.txt 07/14/2021 04:08 PM 17 202107141543.txt 07/14/2021 04:08 PM 17 202107141608.txt 07/14/2021 04:06 PM 11,558 LICENSE 07/14/2021 04:06 PM 11 newFile.txt 07/14/2021 04:06 PM 38 README.md 07/14/2021 04:06 PM 23 test_file_20210528.txt 8 File(s) 12,062 bytes 2 Dir(s) 62,644,277,248 bytes free ~\aj\repo_for_testing>git status On branch test_branch Your branch is up to date with 'origin/test_branch'. Untracked files: (use "git add <file>..." to include in what will be committed) 202107141608.txt nothing added to commit but untracked files present (use "git add" to track) ~\aj\repo_for_testing> git add -A ~\aj\repo_for_testing> git commit -m "#" [test_branch 9017804] # 1 file changed, 1 insertion(+) create mode 100644 202107141608.txt ~\aj\repo_for_testing> git push Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 4 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 284 bytes | 142.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 remote: Resolving deltas: 100% (1/1), completed with 1 local object. To https://github.com/ashishjain1547/repo_for_testing.git 82c204f..9017804 test_branch -> test_branch ~\aj\repo_for_testing> Note: Now, you can go to GitHub.com and create a pull request, then merge "test_branch" with "main" branch.

5. SWITCHING TO THE NEW BRANCH

CMD\repo_for_testing> git checkout origin/test_branch Note: switching to 'origin/test_branch'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example: git switch -c [new-branch-name] Or undo this operation with: git switch - Turn off this advice by setting config variable advice.detachedHead to false HEAD is now at 5b934da cmt 1909 CMD\repo_for_testing>git branch * (HEAD detached at origin/test_branch) main ~ ~ ~ Use the '-b' argument to also attach the HEAD to new branch. CMD\repo_for_testing> git checkout -b origin/test_branch Switched to a new branch 'origin/test_branch' CMD\repo_for_testing> git branch main * origin/test_branch 6. ADDING A FILE TO THE NEW BRANCH CMD\repo_for_testing> echo "20210528 into test_branch" > "20210528_test_branch.txt" CMD\repo_for_testing> dir /b .gitignore 20210528_test_branch.txt LICENSE newFile.txt README.md CMD\repo_for_testing> git status HEAD detached at origin/test_branch Untracked files: (use "git add [file]..." to include in what will be committed) 20210528_test_branch.txt nothing added to commit but untracked files present (use "git add" to track) CMD\repo_for_testing> git add -A CMD\repo_for_testing> git commit -m "0303" [detached HEAD 03e190f] 0303 1 file changed, 1 insertion(+) create mode 100644 20210528_test_branch.txt CMD\repo_for_testing>git push fatal: You are not currently on a branch. To push the history leading to the current (detached HEAD) state now, use git push origin HEAD:[name-of-remote-branch] CMD\repo_for_testing> git push origin HEAD:test_branch Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 4 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 306 bytes | 306.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 remote: Resolving deltas: 100% (1/1), completed with 1 local object. To https://github.com/aj/repo_for_testing.git 5b934da..03e190f HEAD -> test_branch

7. CHECKING REPOSITORY STATUS USING 'GIT LOG'

CMD\repo_for_testing> git log In the logs below, "HEAD" is detached. commit 03e190f8810c9335613cc2c534914e666c04eeeb (HEAD, origin/test_branch) Author: unknown [abc@xyz.com] Date: Fri May 28 03:03:23 2021 +0530 0303 commit 5b934dac6c399f06275feab5b4a91fa8063e092b (origin/main, origin/HEAD, main) Author: Admin on Ubuntu [admin@master.com] Date: Wed Mar 17 19:09:57 2021 +0530 cmt 1909 commit 974a71b3df68140ebbc3b4775201b39c500aa589 Author: aj [abc@xyz.com] Date: Wed Mar 17 01:32:16 2021 +0530 Initial commit Fixing the detached HEAD: CMD\repo_for_testing>git switch -c origin/test_branch Switched to a new branch 'origin/test_branch' CMD\repo_for_testing>git branch main * origin/test_branch Now the HEAD is pointing to the current branch: CMD\repo_for_testing> git log commit 03e190f8810c9335613cc2c534914e666c04eeeb (HEAD -> origin/test_branch, origin/test_branch) Author: unknown [abc@xyz.com] Date: Fri May 28 03:03:23 2021 +0530 0303 commit 5b934dac6c399f06275feab5b4a91fa8063e092b (origin/main, origin/HEAD, main) Author: Admin on Ubuntu [admin@master.com] Date: Wed Mar 17 19:09:57 2021 +0530 cmt 1909 commit 974a71b3df68140ebbc3b4775201b39c500aa589 Author: aj [abc@xyz.com] Date: Wed Mar 17 01:32:16 2021 +0530 Initial commit

8. USING 'GIT CLONE' TO CLONE A BRANCH

CMD\ws_branches\(2)> git clone --branch test_branch https://github.com/aj/repo_for_testing.git Cloning into 'repo_for_testing'... remote: Enumerating objects: 11, done. remote: Counting objects: 100% (11/11), done. remote: Compressing objects: 100% (6/6), done. remote: Total 11 (delta 2), reused 7 (delta 2), pack-reused 0 Unpacking objects: 100% (11/11), 5.23 KiB | 3.00 KiB/s, done. CMD\ws_branches\(2)>cd repo_for_testing CMD\ws_branches\(2)\repo_for_testing>git branch * test_branch CMD\ws_branches\(2)\repo_for_testing>dir /b .gitignore 20210528_test_branch.txt LICENSE newFile.txt README.md

9. NOW WE ADD A NEW FILE TO THE 'main' BRANCH

~\ws_branches\(3) main branch> git clone https://github.com/aj/repo_for_testing.git Cloning into 'repo_for_testing'... remote: Enumerating objects: 11, done. remote: Counting objects: 100% (11/11), done. remote: Compressing objects: 100% (6/6), done. remote: Total 11 (delta 2), reused 7 (delta 2), pack-reused 0 Unpacking objects: 100% (11/11), 5.23 KiB | 3.00 KiB/s, done. ~\ws_branches\(3) main branch>cd repo_for_testing ~\ws_branches\(3) main branch\repo_for_testing>dir /b .gitignore LICENSE newFile.txt README.md ~\ws_branches\(3) main branch\repo_for_testing>echo "test_file_20210528" > "test_file_20210528.txt" ~\ws_branches\(3) main branch\repo_for_testing>dir /b .gitignore LICENSE newFile.txt README.md test_file_20210528.txt ~\ws_branches\(3) main branch\repo_for_testing>git add -A ~\ws_branches\(3) main branch\repo_for_testing>git commit -m "0357" [main daa4600] 0357 1 file changed, 1 insertion(+) create mode 100644 test_file_20210528.txt ~\ws_branches\(3) main branch\repo_for_testing>git push Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 4 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 298 bytes | 298.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 remote: Resolving deltas: 100% (1/1), completed with 1 local object. To https://github.com/aj/repo_for_testing.git 5b934da..daa4600 main -> main ~\ws_branches\(3) main branch\repo_for_testing>

10. NEXT, WE GET THAT NEW FILE INTO OUR 'test_branch'

~\ws_branches\(2) test_branch\repo_for_testing>git branch * test_branch ~\ws_branches\(2) test_branch\repo_for_testing>git fetch remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (1/1), done. remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0 Unpacking objects: 100% (3/3), 278 bytes | 2.00 KiB/s, done. From https://github.com/aj/repo_for_testing 5b934da..daa4600 main -> origin/main ~\ws_branches\(2) test_branch\repo_for_testing>dir /b .gitignore 20210528_test_branch.txt LICENSE newFile.txt README.md ~\ws_branches\(2) test_branch\repo_for_testing>git branch -r origin/HEAD -> origin/main origin/main origin/test_branch ~\ws_branches\(2) test_branch\repo_for_testing> git checkout origin/main Note: switching to 'origin/main'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example: git switch -c <new-branch-name> Or undo this operation with: git switch - Turn off this advice by setting config variable advice.detachedHead to false HEAD is now at daa4600 0357 ~\ws_branches\(2) test_branch\repo_for_testing>git switch -c origin/main Switched to a new branch 'origin/main' ~\ws_branches\(2) test_branch\repo_for_testing>git branch * origin/main test_branch ~\ws_branches\(2) test_branch\repo_for_testing>dir /b .gitignore LICENSE newFile.txt README.md test_file_20210528.txt ~\ws_branches\(2) test_branch\repo_for_testing> git pull There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details. git pull <remote> <branch> If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=origin/<branch> origin/main ~\ws_branches\(2) test_branch\repo_for_testing>git pull https://github.com/aj/repo_for_testing.git origin/main fatal: couldn't find remote ref origin/main ~\ws_branches\(2) test_branch\repo_for_testing>git branch --set-upstream-to=origin/test_branch origin/main Branch 'origin/main' set up to track remote branch 'test_branch' from 'origin'. ~\ws_branches\(2) test_branch\repo_for_testing>git pull Merge made by the 'recursive' strategy. 20210528_test_branch.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 20210528_test_branch.txt ~\ws_branches\(2) test_branch\repo_for_testing>dir /b .gitignore 20210528_test_branch.txt LICENSE newFile.txt README.md test_file_20210528.txt ~\ws_branches\(2) test_branch\repo_for_testing>git checkout origin/test_branch Note: switching to 'origin/test_branch'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example: git switch -c <new-branch-name> Or undo this operation with: git switch - Turn off this advice by setting config variable advice.detachedHead to false HEAD is now at 03e190f 0303 ~\ws_branches\(2) test_branch\repo_for_testing>git switch -c origin/test_branch Switched to a new branch 'origin/test_branch' ~\ws_branches\(2) test_branch\repo_for_testing>dir /b .gitignore 20210528_test_branch.txt LICENSE newFile.txt README.md ~\ws_branches\(2) test_branch\repo_for_testing>git switch -c origin/main fatal: A branch named 'origin/main' already exists. ~\ws_branches\(2) test_branch\repo_for_testing>git merge origin/main warning: refname 'origin/main' is ambiguous. warning: refname 'origin/main' is ambiguous. Updating 03e190f..fca45d6 Fast-forward test_file_20210528.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 test_file_20210528.txt ~\ws_branches\(2) test_branch\repo_for_testing>git push fatal: The current branch origin/test_branch has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin origin/test_branch CMD\ws_branches\(2) test_branch\repo_for_testing>git push --set-upstream origin origin/test_branch This step might have been wrong w.r.t. the input argument above. The command should have been: git push --set-upstream origin test_branch Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 4 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 354 bytes | 354.00 KiB/s, done. Total 2 (delta 1), reused 0 (delta 0), pack-reused 0 remote: Resolving deltas: 100% (1/1), completed with 1 local object. remote: remote: Create a pull request for 'origin/test_branch' on GitHub by visiting: remote: https://github.com/aj/repo_for_testing/pull/new/origin/test_branch remote: To https://github.com/aj/repo_for_testing.git * [new branch] origin/test_branch -> origin/test_branch Branch 'origin/test_branch' set up to track remote branch 'origin/test_branch' from 'origin'. ~\ws_branches\(2) test_branch\repo_for_testing>dir /b .gitignore 20210528_test_branch.txt LICENSE newFile.txt README.md test_file_20210528.txt ~\ws_branches\(2) test_branch\repo_for_testing>git branch origin/main * origin/test_branch test_branch ~\ws_branches\(2) test_branch\repo_for_testing>git branch -r origin/HEAD -> remotes/origin/main origin/main origin/origin/test_branch origin/test_branch ~\ws_branches\(2) test_branch\repo_for_testing>git log commit fca45d643326fb5d88d70c0cf699f7032118df4b (HEAD -> origin/test_branch, origin/origin/test_branch, origin/main) Merge: daa4600 03e190f Author: unknown <aj@gmail.com> Date: Fri May 28 04:02:07 2021 +0530 Merge branch 'test_branch' of https://github.com/aj/repo_for_testing into origin/main commit daa46003a895e4b33577e3c248977334620e3558 (origin/main, origin/HEAD) Author: unknown <aj@gmail.com> Date: Fri May 28 03:57:24 2021 +0530 0357 commit 03e190f8810c9335613cc2c534914e666c04eeeb (origin/test_branch, test_branch) Author: unknown <aj@gmail.com> Date: Fri May 28 03:03:23 2021 +0530 0303 commit 5b934dac6c399f06275feab5b4a91fa8063e092b Author: Admin on Ubuntu <administrator@master.com> Date: Wed Mar 17 19:09:57 2021 +0530 cmt 1909 commit 974a71b3df68140ebbc3b4775201b39c500aa589 Author: aj <aj@gmail.com> Date: Wed Mar 17 01:32:16 2021 +0530 Initial commit ~\ws_branches\(2) test_branch\repo_for_testing> Tags: Technology,GitHub,Cloud,

No comments:

Post a Comment