Python for Network Engineers

Ten Git Commands Every Developer Should Know

by: George El., February 2019, Reading time: 2 minutes

In this post I will explain 10 commands for your workflow as developer.

Inititalize the repo

git init

Add all files of your working directory in the staging area

git add .

Commit your changes with an informational message

git commit -m "first commit"

Upload your changes to github

of course you need to have an account on github first, then you create a repo on github called myrepo

git remote add origin https://github.com/user/myrepo.git
git push origin master

Pull the latest changes from remote repo

git pull origin master

Show a log of the commits.

git log 
or
git log --graph --decorate --oneline --all

Show the status of your repo

git status

Create a new branch feature

git checkout -b feature
or
git branch feature
git checkout feature

Merge feature to master

git merge feature

you must be in the master branch. If you master branch does not have any commits since you branched, git will do a fast forward merge, meaning it will take the commits from feature and put them in the master branch. If you want to avoid this you can use the argument “–no-ff”

Undo changes of a certain commit

git revert {hash id}

Go back to a commit

git reset --soft hash
deletes the commit but leaves the files in stage area - you can commit them again

git reset --mix hash
deletes the commit and unstages the files (leaves them untracked) - you must add and commit

git reset hash
(same as --mix) is the default

git reset --hard hash
deletes the commit, unstages and deletes files (be careful with this one)

Contribute to a project

If you want to contribute to a project in github, you will have to fork the repo, make changes and create a pull request. If the repo creator approves your changes, he will do a merge pull request.

comments powered by Disqus