DevOPs Git Commands by Editorial Staff February 26, 2023 written by Editorial Staff February 26, 2023 How to create a new branch in Git: By default, you have one branch, the main branch. With this command, you can create a new branch. Git won’t switch to it automatically – you will need to do it manually with the next comman git branch branch_name root@vmlinux:/var/lib/git/.git/devops1# git branch br_work1 root@vmlinux:/var/lib/git/.git/devops1# git branch br_work2 root@vmlinux:/var/lib/git/.git/devops1# git branch br_work3 How to list branches in Git: You can view all created branches using the git branch command. It will show a list of all branches and mark the current branch with an asterisk and highlight it in green. git branch root@vmlinux:/var/lib/git/.git/devops1# git branch br_work1 br_work2 br_work3 * main How to switch to a newly created branch in Git: When you want to use a different or a newly created branch you can use this command git checkout branch_name root@vmlinux:/var/lib/git/.git/devops1# git checkout br_work1 Switched to branch 'br_work1' How to create a branch in Git and switch to it immediately: In a single command, you can create and switch to a new branch right away. git checkout -b branch_name root@vmlinux:/var/lib/git/.git/devops1# git checkout -b new_branch5 Switched to a new branch 'new_branch5' root@vmlinux:/var/lib/git/.git/devops1# git branch br_work1 br_work2 br_work3 main * new_branch5 How to delete a branch in Git: When you are done working with a branch and have merged it, you can delete it using the command below git branch -d branch_name root@vmlinux:/var/lib/git/.git/devops1# git branch -d br_work3 Deleted branch br_work3 (was 6f76b3f). How to merge two branches in Git: To merge the history of the branch you are currently in with the branch_name, you will need to use the command below git merge branch_name root@vmlinux:/var/lib/git/.git/devops1# git branch br_work1 br_work2 main * new_branch5 root@vmlinux:/var/lib/git/.git/devops1# git merge br_work1 Already up to date. root@vmlinux:/var/lib/git/.git/devops1# git merge br_work2 Already up to date. How to show the commit log as a graph in Git: We can use –graph to get the commit log to show as a graph. Also, –oneline will limit commit messages to a single line. git log –graph –oneline root@vmlinux:/var/lib/git/.git/devops1# git log --graph --oneline 6f76b3f (HEAD -> new_branch5, main, br_work2, br_work1) commiting some text files *f21baa3 (origin/main, origin/HEAD) added line in demo.txt *8c723ac info1 renamed to demo *98e404b created Python_1 folder and moved py files in that folder *5e55c5a Python Practise files uploaded *3e7a588 first 2 files created How to show the commit log as a graph of all branches in Git: Does the same as the command above, but for all branches git log –graph –oneline –all How to abort a conflicting merge in Git: If you want to throw a merge away and start over, you can run the following command: git merge –abort How to see remote URLs in Git: You can see all remote repositories for your local repository with this command git remote -v root@vmlinux:/var/lib/git/.git/devops1# git remote -v origin https://github.com/yvsc369/devops1 (fetch) origin https://github.com/yvsc369/devops1 (push) How to get more info about a remote repo in Git: Just replace origin with the name of the remote obtained by running the git remote -v command git remote show origin root@vmlinux:/var/lib/git/.git/devops1# git remote show origin remote origin Fetch URL: https://github.com/yvsc369/devops1 Push URL: https://github.com/yvsc369/devops1 HEAD branch: main Remote branch: main tracked Local branch configured for 'git pull': main merges with remote main Local ref configured for 'git push': main pushes to main (fast-forwardable) How to push changes to a remote repo in Git: When all your work is ready to be saved on a remote repository, you can push all changes using the command below git push while pushing you will get some type of errors like below root@vmlinux:/var/lib/git/.git/devops1# git push Username for 'https://github.com': yvsc369 Password for 'https://yvsc369@github.com': remote: Support for password authentication was removed on August 13, 2021. remote: Please see https://docs.github.com/en/get-started/getting-started- with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication. fatal: Authentication failed for 'https://github.com/yvsc369/devops1/ Method 1:- Create Public keys and Create SSH Keys in https://github.com/settings/ssh/new root@vmlinux:/var/lib/git/.git/devops1# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:0bo3C2Q4WlrLrFeQnsR3nRYwO3x2dZHBrwfi3/g2zcU root@vmlinux The key's randomart image is: +---[RSA 2048] + | o. .o*| | o o. +.| | . .. =.oo. .| | =..o.+=. | | o=+S. o . + | | Bo=. E| | o +.o o . =o| | .. o o o.=| | .. . oo| +----[SHA256] + root@vmlinux:/var/lib/git/.git/devops1# ls -l ~/.ssh/ total 8 -rw------- 1 root root 0 Sep 30 05:45 authorized_keys -rw------- 1 root root 1675 Sep 30 09:53 id_rsa -rw-r--r-- 1 root root 394 Sep 30 09:53 id_rsa.pub root@vmlinux:/var/lib/git/.git/devops1# cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDZaPMR0RofHfmlREo7ymKczXGZ7Hsw+wQ85XGUxd3FgZ4ZG 5ojXIHcfFk1Uk2eOSJ0W5tCtLLp/2xPCitr4Nx+Iex/zzeDdCZ2f68zfIkur0B0ZqgSs0Tn2JJTMv WmtS/HymOeJ5dPyOPiL/zEj8Nd28QQcD7c9BS410yCLV2pPdKGxo6VqQHPJn9OJvyvybLcm3wQoNP Ya1bEKNRURNYNv96cQDl+unOf1ymw+vHJvISMJhQx63gvV6Fr4j4sk3gP4y6sY62gHC1fUvgvgjF9 WfhlI1mmQCUHK19jHqvNv1Hd0KM8gPkN5GuP6ThO1deyorNbJQ76KCb45zCXqLF7 Copy the Key Code in Key Section box and add SSH Key mentioned below, and then try to push to github. Method 2:- Using Access Tokens Link to create token https://github.com/settings/tokens Kept for 90 days, you select all options as of now, because we are the beginners and we are at training stage root@vmlinux:/var/lib/git/.git/devops1# git push Username for 'https://github.com': yvsc369 Password for 'https://yvsc369@github.com': ghp_gco8ld6uLMx…DV (token) Counting objects: 3, done. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 5.00 MiB | 5.25 MiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/yvsc369/devops1 f21baa3..6f76b3f main -> main How to add a remote repository in Git This command adds a remote repository to your local repository (just replace https://repo_here with your remote repo URL) git remote add origin git@github.com:yvsc369/devops1.git root@vmlinux:/var/lib/git/.git/devops1# git remote add origin git@github.com:yvsc369/devops1.git How to pull changes from a remote repo in Git: If other team members are working on your repository, you can retrieve the latest changes made to the remote repository with the command below git pull root@vmlinux:/var/lib/git/.git# git pull remote: Enumerating objects: 35, done. remote: Counting objects: 100% (35/35), done. remote: Compressing objects: 100% (31/31), done. remote: Total 35 (delta 9), reused 23 (delta 3), pack-reused 0 Unpacking objects: 100% (35/35), done. From github.com:yvsc369/devops1 * [new branch] main -> origin/main 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> master How to check remote branches that Git is tracking: This command shows the name of all remote branches that Git is tracking for the current repository git branch -r root@vmlinux:/var/lib/git/.git# git branch -r origin/main How to fetch remote repo changes in Git: This command will download the changes from a remote repo but will not perform a merge on your local branch (as git pull does that instead). git fetch root@vmlinux:/var/lib/git/.git# git fetch root@vmlinux:/var/lib/git/.git# How to check the current commits log of a remote repo in Git Commit after commit, Git builds up a log. You can find out the remote repository log by using this command git log origin/main root@vmlinux:/var/lib/git/.git# git log origin/main commit 6f76b3faf315ba6b1cfa301fc4ff3f3549af6f93 (HEAD -> master, origin/main) Author: Venkat Chandra <yvsc369@gmail.com> Date: Fri Sep 30 07:51:02 2022 +0000 commiting some text files commit f21baa39ad597977928f8e678abd9b13b3e481f7 Author: Venkat Chandra <yvsc.369@hotmail.com> Date: Tue Sep 27 12:46:20 2022 +0530 added line in demo.txt commit 8c723ac023528cd3d91404d80a22275628d32433 Author: Venkat Chandra <yvsc.369@hotmail.com> Date: Tue Sep 27 12:30:21 2022 +0530 info1 renamed to demo How to merge a remote repo with your local repo in Git: If the remote repository has changes you want to merge with your local, then this command will do that for you git merge origin/main How to get the contents of remote branches in Git without automatically merging This lets you update the remote without merging any content into the local branches. You can call git merge or git checkout to do the merge. git remote update How to push a new branch to a remote repo in Git: If you want to push a branch to a remote repository you can use the command below. Just remember to add -u to create the branch upstream git push -u origin branch_name root@vmlinux:/var/lib/git/.git/devops1# git push -u origin new_branch5 Username for 'https://github.com': yvsc369 Password for 'https://yvsc369@github.com': Counting objects: 4, done. Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 1002 bytes | 1002.00 KiB/s, done. Total 4 (delta 0), reused 0 (delta 0) remote: remote: Create a pull request for 'new_branch5' on GitHub by visiting: remote: https://github.com/yvsc369/devops1/pull/new/new_branch5 remote: To https://github.com/yvsc369/devops1 * [new branch] new_branch5 -> new_branch5 Branch 'new_branch5' set up to track remote branch 'new_branch5' from 'origin'. How to remove a remote branch in Git: If you no longer need a remote branch you can remove it using the command below git push –delete origin branch_name root@vmlinux:/var/lib/git/.git/devops1# git push --delete origin new_branch5 To https://github.com/yvsc369/devops1 - [deleted] new_branch5 How to use Git rebase: You can transfer completed work from one branch to another using git rebase. git rebase branch_name_here Git Rebase can get really messy if you don’t do it properly. Before using this command I suggest that you re-read the official documentation here How to run rebase interactively in Git: You can run git rebase interactively using the -i flag. It will open the editor and present a set of commands you can use. git rebase -i master # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like “squash”, but discard this commit’s log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit How to force a push request in Git This command will force a push request. This is usually fine for pull request branches because nobody else should have cloned them. But this isn’t something that you want to do with public repos. git push -f root@vmlinux:/var/lib/git/.git/dev1/devops1# git checkout main Switched to branch 'main' Your branch is up to date with 'origin/main'. root@vmlinux:/var/lib/git/.git/dev1/devops1# git push -f Username for 'https://github.com': yvsc369 Password for 'https://yvsc369@github.com': TokenPassword Everything up-to-date 0 comment 0 FacebookTwitterPinterestEmail Editorial Staff previous post Git Commands next post Kubernetes Architecture You may also like Git Commands February 26, 2023 Git Commands February 26, 2023 Git Commands February 25, 2023 DevOps December 19, 2022 Leave a Comment Cancel Reply Save my name, email, and website in this browser for the next time I comment.