Create new branch
New branch can be created using “git branch”command. Here new branch name is “branch1”. This will create the branch in local git by default from Master branch and then it needs to be pushed to remote repository.
command : git branch <new branch name>
New branch can also be created by using “git checkout” command along with “-b” flag. This allows to create the branch and switch to the created branch.
command : git checkout -b <new branch name>
DD:DevOpsDiggers DevOpsDiggers$ git checkout -b branch1
Switched to a new branch 'branch1'
DD:DevOpsDiggers DevOpsDiggers$
If new branch needs to be created from any other existing branch.
command : git checkout -b <new branch name> <existing branch>
Push created branch to Remote Repository
Locally created branches can be pushed to Remote Repository using git push command along with “-u” flag and here origin is used as the push to new branch is occurring for the first time to remote repository.
command : git push -u origin <branch name>
DD:DevOpsDiggers DevOpsDiggers$ git push -u origin branch1
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'branch1' on GitHub by visiting:
remote: https://github.com/Blog-DD/DevOpsDiggers/pull/new/branch1
remote:
To https://github.com/Blog-DD/DevOpsDiggers.git
* [new branch] branch1 -> branch1
Branch 'branch1' set up to track remote branch 'branch1' from 'origin'.
After pushing, branch can be viewed in Remote Repository.
If all the local branches need to be pushed to Remote Repository “–all” flag would help along with git push command.
command : git push –all origin
DD:DevOpsDiggers DevOpsDiggers$ git push --all origin
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/Blog-DD/DevOpsDiggers.git
* [new branch] branch3 -> branch3
* [new branch] branchnew -> branchnew
DD:DevOpsDiggers DevOpsDiggers$ git branch -a
branch3
* branchnew
master
remotes/origin/HEAD -> origin/master
remotes/origin/branch1
remotes/origin/branch3
remotes/origin/branchnew
remotes/origin/master
Switch to another branch
Switching to created branches can be done by using “git checkout” command.
command : git checkout <branch name>
DD:DevOpsDiggers DevOpsDiggers$ git checkout branch1
Switched to branch 'branch1'
DD:DevOpsDiggers DevOpsDiggers$
List all created branches
All created branches in local can be listed with “git branch” command.
command : git branch
DD:DevOpsDiggers DevOpsDiggers$ git branch
* branch1
branch2
branch3
master
Along with “-a” flag both local and remote branches can be listed which helps to recognize the branches not pushed to Remote repository.
command : git branch -a
DD:DevOpsDiggers DevOpsDiggers$ git branch -a
* branch1
branch2
branch3
master
remotes/origin/HEAD -> origin/master
remotes/origin/branch1
remotes/origin/master
Rename branch name
Branch names can be renamed with git branch command along with “-m” flag.
command : git branch -m <current branch name> <new branch name>
DD:DevOpsDiggers DevOpsDiggers$ git branch -m branch1 branchnew
DD:DevOpsDiggers DevOpsDiggers$ git branch
branch2
branch3
* branchnew
master
Delete branch
Branches can be deleted with git branch command along with “-d” flag.
Note: User can not be on the same branch that is going to be deleted. So please checkout to any other branch and perform the delete command.
command : git branch -d <branch name to be deleted>
DD:DevOpsDiggers DevOpsDiggers$ git branch -d branch2
Deleted branch branch2 (was 3cdd9cf).
DD:DevOpsDiggers DevOpsDiggers$ git branch
branch3
* branchnew
master
Clone remote repository
Git clone can be used to get all the contents of remote repository to local workstation.
command : git clone <URL of remote repository to be cloned>
DD:Test DevOpsDiggers$ git clone https://github.com/Blog-DD/DevOpsDiggers.git Cloning into 'DevOpsDiggers'... remote: Enumerating objects: 15, done. remote: Counting objects: 100% (15/15), done. remote: Compressing objects: 100% (11/11), done. remote: Total 15 (delta 3), reused 7 (delta 0), pack-reused 0 Unpacking objects: 100% (15/15), done.
Add file
Files can be added using git add command.
command : git add <file name>
DD:DevOpsDiggers DevOpsDiggers$ git add Newfile DD:DevOpsDiggers DevOpsDiggers$ git status On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: Newfile
Commit file
Added files can be committed using git commit command.
command : git commit -m “commit message”
DD:DevOpsDiggers DevOpsDiggers$ git commit -m "Committing test file"
[master 2bf603b] Committing test file
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 Newfile
Push file
Committed changes can be pushed to remote repository using git push command.
command : git push (or) git push origin <branch name>
DD:DevOpsDiggers DevOpsDiggers$ git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 282 bytes | 282.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/Blog-DD/DevOpsDiggers.git
3cdd9cf..2bf603b master -> master
Status
Git status command would help to check the changes made to any file and if there are any untracked files and also it is best practice to use git status command often to avoid any conflicts.
Command : git status
DD:DevOpsDiggers DevOpsDiggers$ git status On branch master Your branch is up to date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: Test File no changes added to commit (use "git add" and/or "git commit -a")
Pull changes
Git pull helps to get changes from branches in remote repository to local workstation. Switching to particular branch would be required to do this.
Command : git pull
If user wants to pull the changes from a different branch.
Command : git pull origin <name of branch to get the changes from>
If there are no changes, status would show as “Already up to date”.
DD:DevOpsDiggers DevOpsDiggers$ git pull Already up to date.
If there are any changes in remote repository and are not in local, after pull those changes would appear in local branch.
DD:DevOpsDiggers DevOpsDiggers$ git pull remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/Blog-DD/DevOpsDiggers 6e6f77f..6f076d8 master -> origin/master Updating 6e6f77f..6f076d8 Fast-forward Test File | 3 +++ 1 file changed, 3 insertions(+)
Revert commit
Commits can be reverted with git revert command.Git log command would help to get all the commits’ information along with commit id.
command : git log
DD:DevOpsDiggers DevOpsDiggers$ git log
commit 2bf603bf9f26922db46f006b845f26a37d6bac57 (HEAD -> master, origin/master, origin/HEAD)
Author: DevOpsDiggers <devopsdiggers@gmail.com>
Date: Fri May 1 16:24:37 2020 -0500
Committing test file
select the commit id that needs to be reverted and use the revert command.
command : git revert <commit id>
DD:DevOpsDiggers DevOpsDiggers$ git revert 2bf603bf9f26922db46f006b845f26a37d6bac57
[master eb8d379] Revert "Committing test file"
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 Newfile
After that, push needs to be done in order to revert the commits from Central/Remote repository too.
Delete files
Deleting files from remote repository can be done in 3 simple steps. Use git rm command to delete the file from local repository.
command : git rm <name of the file to be deleted>
DD:DevOpsDiggers DevOpsDiggers$ git rm Newfile rm 'Newfile'
Deleted files need to be committed with git commit command.
command : git commit -m “commit message”
DD:DevOpsDiggers DevOpsDiggers$ git commit -m "deleting Newfile" [master 081ba4c] deleting Newfile 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Newfile
Then changes need to be pushed to delete the files from remote repository.
DD:DevOpsDiggers DevOpsDiggers$ git push Counting objects: 2, done. Delta compression using up to 4 threads. Compressing objects: 100% (1/1), done. Writing objects: 100% (2/2), 241 bytes | 241.00 KiB/s, done. Total 2 (delta 0), reused 0 (delta 0) To https://github.com/Blog-DD/DevOpsDiggers.git 472d158..081ba4c master -> master