DEV Community

Cover image for Regular GIT actions of developers in 2021
kennethnnah
kennethnnah

Posted on • Updated on

Regular GIT actions of developers in 2021

It's often confusing the different commands needed to interact and efficiently work with GIT. We need a handleful of the useful ones. In this short article, I am going to show you the regular unavoidable task performed by modern developers.

Prerequisite:

  • Code editor (vscode preffered) .

Github is owened by Microsoft helping millions of developers version control and host their code online -(tracking code history). Most recruiters require a candidate to be proficient with the use of versioning softwares like GitHub or GitLab. These tools help teams work productively. We'll use Github for this demostration.

BASIC COMMANDS

  • Create a repository on github (Web)
  • Set up global username git config --global user.name <"ur username">
  • Set up global email git config --global user.email <"ur password">
  • Add remote url to local git remote add origin <url>
  • Innitialise a git repository locally git init
  • Git clone git clone -b <branch> <remote_repo>
  • Push code to remote git push origin <branch name>
  • Check remote connection
  • Make or create a new branch git checkout -b "master"
  • Updated local branch with remote changes git pull
  • Create a branch git checkout -b myNewBranch
  • Pull request git pull <remote> <branch>
  • Merge between branches
  • Check for changes in the tree state git status
  • Check the repo remote URL git remote -v
  • Check remote url git config --get remote.origin.url
  • Undo a merge action to its last commit git reset --hard
  • Undo git init rm -rf .git
  • Change git remote origin git remote set-url origin your-git-origin-url-path
  • Undo git add . git reset
  • Delete branch from local git branch -D my-local-branch
  • Delete branch from remote git push -d origin <branch-name>

The list above does not capture all the command actions on GitHub, but that which is needed to perform routine git tasks. Think of Github as a tree with different branches(folders). These branches are the exact clone of the master branch folder whenever you type git branch branch-name in the command line.

git-folder

Create a repository on github
There are two methods to this;

  1. Create a repo on GitHub website and
  2. Create a repo locally

How to create a git repository on GitHub website; To do this, navigate to your newly created GitHub account, follow the prompts, make sure you have confirmed your email address. I created a new account for this demo. The image below is only for new sign-ups with fresh GitHub account.

create

If you have an already existing account, your screen will look like the image below:
old

After clicking the repo button, you will be prompted to give a name to the repo and optionally add a readme to the new repo folder, ones this done, go ahead and click the create repo green button as the final step.

Innitialize a git repo locally
Fire up your vsCode, navigate to the command line and type this on the command line git init. This command innitialises a git repo locally on your pc.

Save your code

Write code on your vsCode, Before saving your code, be sure your remote repo(online) is connected with your local repo. Let us assume we have typed a line of code for the first time and we want to check connection with the remote, type: git remote if you get a response like fatal: not a git repository (or any of the parent directories): .git, then it means you are not connected to remote. *connect with the remote: * git remote add origin url.

Git clone git clone url

Push code to github from local

You can only push code to GitHub from local when you are rightly connected to the remote as illustrated. Follow the steps to push your first code to remote.
git add .: This command saves your work in the staging area
git commit -m "commit message" This command saves your code to GitHub.
**push code to remote:**
git push origin master`: the origin refers to the remote repo while "master" is the branch we are committing code to.

*issues from pushing to remote can be solved with this 2 steps

  1. git clone url
  2. git push

Check remote connection: git status

Create a branch git branch branch-name

Check tree/branch status

If you recall that GitHub repository chain is like a tree with branches, you may want to track which brach you are currently working on with this command: git branch, it shows you your current working branch.

Pull request(PR): git pull

If you recall, GitHub is a great place for coding teams to collaborate easily. You may have had code commits from other team members on the GitHub repo that has not reflected on your local machine, to check this, you need to type git status, this will let you know of any changes to the current file, to enable a git pull.

Merge between branches
To merge a branch with another, you need to be sure which branch you are currently on. You can use git merge branch or git rebase branch

Discussion (0)