Git CLI operations

I usually use Git with CLI, so I take a note how to operate.

Download from remote repo

git clone downloads source code form remote Git repository.
git clone <git repository>
When cloning by specifying a branch.
git clone -b <branch name> <git repository>

Set user information

git clone can set user name and email address on local repository.
cd <local repo>
git config <user name>
git config <email address>

First commit to remote repository

I can create local repo with git init. After setting remote repo URL, I can make copy repo in remote git server.
git init
git add --all
git commit -m "First Commit" git remote add origin <remote repo address> git push -u origin master

Update remote repo

After editing the source locally, I can reflect the update to the remote repo.
git add <updated file>
git add <added file>
git rm <removed file>
git commit -m "Commit Message"
git push -u origin <branch name>

Switch local branch

The local branch can be switched with checkout comman.
git checkout <branch name>

Show commit logs

I can check the commit logs up to the current commit. The upper is newer.
git log
In the case of only commit id and titile.
git log --oneline

Merge commits

I can merge the N commits that I've picked up into one commit。 The lower one is newer commit.
git rebase -i HEAD~N
If I change tha TAG from pick to fixup, the commit is merged into previous one. After closimg editer, tha canges is refrected to git history.

Create orphan branch

Orphan branch is a branch which does not have parent history.
git checkout --orphan <new branch name>
git reset --hard
After creating orphan branch, adding new files for the orphan branch.
git add --all
git commit -m "First Commit"
git push -u origin <new branch name>

Go back to commit id

I can restore the source codes at specific commit ID.
git reset --hard <commit id>

Back to head

I can restore the source codes to the state before editing.
git reset --hard HEAD

Recover file

I can restore a specified source file to the state before editing.
git checkout HEAD <file name>
All files.
git checkout HEAD -f

Remove local branch

The D option of git branch removes local branch.
git branch -D <local branch name>

Remove all removed file

I can remove all deleted files from local git repo history.
git rm `git ls-files --deleted`

I have technical job experience in enbedded software development and server side infrastructure/application engineering. I'm interested in programming and computer security.
To write down my technical knowledge in the place where I can access from anywhere. To share my program source code. To train my writing skill.
  • LinkedIn (preparing)

  • Twitter

  • Facebook (preparing)

  • GitHub

  • StackOverFlow (preparing)