8 Must Know Git Techniques for Junior Developer

Introduction

Everyone knows about manage Git is extremely important. However, editing, rollback, deleting the history with Git is way much stressful sometimes.

In this post, I’ll talk about the basic Git techniques for all the developers must know.

1. Git Clone

When you’re working on an existing codebase, the first step is to clone the codebase into your local system.
To clone a repository, copy the existing codebase address. Here is where you can get the address from remote.

remote


clone_git

CMD

// cloning from a source on the Internet, add the URL:
git clone https://github.com/VannsKang/my_git_project.git

// general:
git clone /path/to/repo

// If your source doesn’t reside in the same system,
// you can SSH to a remote system and clone too:
git clone username@remote_system_ip:/path/to/repo/on/remote

2. Remotes in Git

Once git linked to your local system, you can check or change, even remove the link.

CMD

// check the list of remotes
git remote -v

// add new remote
git remote add remote_name remote_address
// e.g) git remote add origin http://github.com/VannsKang/my_git_project.git

// remove remote link
git remote remove remote_name

// change remote addresss for exsiting remote name
git remote set-url remote_name new_remote_address

3. Branching in Git

Branching is necessary for Git to bifurcate lines of work in a project.

CMD

// to check a list of local branches
git branch
git branch -a

// to check a list of remote branches
git branch -r

// create a new branch
git branch new_branch

// change to new branch from current branch
git checkout new_branch

// git branch + git checkout
git checkout -b new_branch

// to rename current branch
git branch -m new_name_branch

// to remove local branch
git branch -D branch_name

// to remove remote branch
git push origin --delete branch_name

4. Commit in Git

This is a simple protocol to commit.

// marking All file to commit
git add .

// marking certain file(file_name) to commit
git add file_name

// commit and move to message editor
git commit

// commit with commit message (not to move commit message editor)
git commit -m "commit_message"

// git add . + git commit -m "commit_message"
//  However, it doesn't mark the newly added files. For those cases, use git add .
git commit -am "commit_message"

// generate and push remote to branch
git push remote_name branch_name

5. Git Stash

What if while doing some work, another request comes in? Then, needed to stop what you are doing and change branches for a while.
In this situation, Git stash can bail out the developers.

CMD

// save temporarily to the stash list
git stash
git stash save

// checkout stash list
git stash list

// load temp save to current branch
git stash apply

// remove stash list
git stash drop
git stash drop [stash_name]

// stash apply + stash drop
git stash pop

6. Git Rebase

Git rebase is the most effective way to control Git workflow.
Someone says it is dangerous to rebase Git, but for clear managing the Git workflow. You should be familiar with it.

CMD

// if you are the branch out from master and
// some commits already done ahead from current commit
// but you want to squash them to one.
git rebase -i master

// rebase for current branch N means number of listing the commit head in editor
git rebase -i HEAD~N
// e.g) git rebase -i head~3

// if you get the conflict after squash, fix then continue
git rebase --continue

// if you want to get out from the rebase process
git rebase --abort

Git Rebase has some options but at least you have to know these three below.

  • squash : squash multiple commit to one
  • reword : edit commit message
  • drop : delete commit history. caution! it is different from rollback, rollback has rollback history from commit history, however this is remove the commit history you point out for drop.

After all fixup, you have to rewrite or overriding old commit history. for that, use …

// Danger! force to overwrite remote commit history
git push remote_name branch_name -f

// merge without auto commit
git merge branch-name --no-commit --no-ff

Now you get all this new clear commit history that you want.

7. Git Cherry-pick

Sometimes you forget which branch you working on. Let’s assume that you work on the Y branch and committed, but after all, you would realize that it should be committed on the X branch.


cherry-pick-git

In this situation, Git rebase is to edit history for the same branch, not others, so it is not a good answer for this case.
Here is the answer, Git cherry-pick.

CMD

// if you are on the Y branch, checkout to the X branch first
// on the X branch, run the cmd below
// commit_hash can be noticed on `Git log`.
git cherry-pick commit_hash

// cherry-pick multiple commits each by each
git cherry-pick commit_hash commit_hash commit_hash

// cherry-pick multiple commits range
git cherry-pick commit_hash..commit_hash

// Sometimes you face the conflict when doing cherry-pick
// exit from cherry-pick
git cherry-pick --abort

8. ETC …

These are all additional tips for some cases.

// set the global git user name
// set the global git user email
git config --global user.name 'user_name'
git config --global user.email 'user_email'

// global git information
git config --global --list
git config --global -l

// edit git config
git config --global --edit

// cancel all codes from the previoues commit
git reset --hard HEAD^

// cancel only commi lefting the code change
git reset --soft HEAD^

// cancel merge
git reset --merge

// log
git log

// log with code change
git log -p

// list only 20 logs
git log -20

Conclusion

It is very essential to keep 8 things in mind about Git.
It makes it easier and safe to develop so much.

The Git Rebase part is a bit lacks in the details in this post.
Soon, gonna talk about Git rebase with more details in the next post.