Git commands

As I am learning git in my 5th week here at CDOT, all the different pieces of knowledge are coming together and get organized in order, namely in the alphabetical order.

This is the list of git commands in the alphabetical order. I am going to make it complete which will take me more than one weak, so this version is still unfinished.

I hope this will be helpful for those who are new to git.

git ABC

git add .

to add all files from the “working directory” to the “staging area”

git branch

to see the list of available/current branches in your repo (just their names)

git branch –no-merged

to see the branches you have not merged into master yet

git branch –merged

to see the list of branches you have merged into master, and which can be safely deleted.

git branch -v

to see the verbose list of available/current branches in your repo (their names and additional info)

git branch bough

to create pointer bough pointing to the same commit as pointer master

git branch -d bough

to delete bough when you don’t need it any more, e.g. after the work in bough has been merged into master.

git checkout bough

to move pointer HEAD from master to bough

git checkout master

to switch from branch x to the master branch (e.g. before updating the code in your local repo from the code in your upstream repo)

git checkout -b bough

to create branch bough and move HEAD to it

git checkout -b bough origin/bough

if your origin has bough, this command will download bough to your local copy

git checkout -b bough bgh origin/bough

to download a copy of origin’s bough, rename it into bgh, and switch to it

git clone https://github

to download a copy of the project from the main repo

git commit -a -m ‘Made a change’

to make a new commit and move HEAD and the pointer it’s attached to onto the new commit. -a stands for “add” and -m for “message”

git commit -m ‘Made a change’

to commit a change that has been already added to the “staging area”. -m stands for “message”

git diff

git diff HEAD~1

to see the difference between

git fetch origin

to make your local copy up-to-date with the origin. The origin’s master pointer corrects the position of your local copy’s master pointer.

git init

to run in the directory where your project is which you want to upload to gitHUB

git log

to see the commits made in your local repository in reverse chronological order: newest on top.

git log –pretty=oneline

to see the list of commits’ hash# and commit messages

git merge bough

to merge bough into master. You should be on master to do this.

git pull

to update your local copy with the info from your origin. Why will it work without arguments? Because your local copy’s master is tracking your origin’s master.

git pull https://github.com/

git pull origin bough

merge origin’s bough into your local copy’s bough

git pull upstream master

in order to update the code in your local repo with the code from your upstream repo (upstream repo is the one you originally cloned the project from)

git pull –rebase origin master

git pull –rebase alpha master 

.. to alpha from master

git push

to upload the commit to which your bough is set, and set origin’s bough to this commit.

git push -f  origin bough

upload and force merge of your bough into your fork’s bough

git push origin bough

to upload to my fork the new branch “bough” which I have created

git push origin :bough

to delete origin’s bough

git push origin master

upload my master to origin’s master

git rebase master bough

to pull in the work in your bough into your master

git rebase -i HEAD~2

to see 2 commits

git rebase -i master

git rebase –abort

to abort the rebasing process

git rebase –onto master bough

git remote

to see a brief (simplified) list of links to other people’s “forks”

git remote add https://github.com/

to create a link to a remote repo

git remote add alpha https://github.com/

to create a link named alpha to the remote repo whose URL you use

git remote -rm alpha

to delete the link to the remote repo aliased “alpha”

git remote -v

to see a verbose list of links to other people’s “forks”

git remote add origin https://github.com/

git reset — hard c3dc0. . .

force set HEAD to the commit you want

git status

to determine which file is in which state

Q&A

Some of the questions that I had this week and found answers to.

Q: What is a remote repo?

A: A version of your project on the cloud.

* * *

Q: What is a fork?

A: When you want to contribute to a project, you create an intermediate copy of it by “forking” it on GitHUB. This intermediate copy is called your “fork”. Then you use the URL of your “fork” to download a copy to work on your computer. Thus, there are 3 copies of the project: the main copy, the intermediate copy (your “fork”) and your local copy.

* * *

Q: After I have finished working on a bug where do I push my changes?

A: To your fork. Then you make a pull request and eventually the changes in your fork get merged into the main repo.

* * *

Q: How do I choose which URL to use for forking: HTTP, SSH, or Git Read-only?

A: If you are forking from your own repo, HTTP or SSH will do. Otherwise, use the Read-only URL.

* * *

Q: Do I edit the file and then create a branch or vice versa?

A: Create a branch first.

* * *

Q: Do I use the command “git push origin bough” only at first when I have no bough on my fork yet, and subsequently I just say “git push” if I am on bough? Or do I always use “git push origin bough” every time I want to push?

A: It is better to be specific, so use “git push origin bough”, otherwise everything will be pushed.

* * *

Q: What is package.json?

A: A file the author of a node.js app writes to define metadata about the app. This includes which node module it is dependent on.

* * *

Q: How do I see my express version number?

A: npm info express version

* * *

Q: Why use npm install?

A: To install depencies based on package.json

* * *

Q: How do I see the list of dependencies?

A: npm ls

* * *

Q: Where should I be to delete a branch, on its ancestor or it doesn’t matter where?

A: Anywhere as long as you are not on the branch itself.

Leave a comment