Simple Notes for Git

These notes are for easy reference since I am new to Git.

You can clone the particular branch in the repo.

1
git clone -b branch-name

After you have done the git clone your repo to the local device, go inside the repo folder via terminal. You can check git status or check which branch you are in.

1
2
git status
git branch -a

If you want to double check if origin is correct.

1
git remote -v

You can fetch the branches along with their respective commits, but not the actual files.

1
git fetch

If you need to synchronize your branch list, for example, a branch is delete on github but not on your local machine.

1
git fetch -p

You can pull actual files with info such as the branches with the respective commits.

1
git pull

You can create and checkout the new branch at the same time.

1
git checkout -b new-branch

After working on your file you can add the particular file to this branch.

1
git add filename

Or add all file in the current folder to this branch.

1
git add .

Then you can make a commit locally, remember always to add some comments for your commit.

1
git commit -m "Fixed some typos"

If you are using GPG to sign your commit, add -S in the end.

1
git commit -m "Fixed some typos" -S

Normally, you can now push your new-branch together with the new commit to Github. Later you can go to pull request on Github.

1
git push origin new-branch

Or perhaps you want to merge the new-branch locally with your master branch. First, go back to master branch then merge.

1
2
git checkout master
git merge new-branch

If you have sucessfully merged the branches, you can choose to delete your previous new-branch locally. Be careful here.

The -d option stands for –delete, which would delete the local branch, only if you have already pushed and merged it with your remote branches.

The -D option stands for –delete –force, so by the name, you can guess it will delete the local branch no matter if you have pushed/merged or not.

So, please be extra careful. I suggest you use -d unless you are totally sure and have double checked the git status.

1
2
git branch -d new-branch
git branch -D new-branch

If you previously have pushed the new-branch to Github and now you want to delete it remotely, yes, you can.

1
git push origin --delete new-branch

When in doubt, please read documentations.
https://www.git-scm.com/doc