DEV Community

Joonhyeok Ahn (Joon)
Joonhyeok Ahn (Joon)

Posted on • Edited on

Git tutorials - be fluent with commands

If you read the first chapter of Git tutorial, you are starting the journey! We will look into more about Git operations, which will be useful to your daily engineering flow once you get familiar with them. Let's start without further ado.

Configs

To config email and name, run

git config user.email <email>
git config user.name <name>
Enter fullscreen mode Exit fullscreen mode

Branches

To switch branches, run

git checkout <existing branch>
Enter fullscreen mode Exit fullscreen mode

To create a new branch, run

git branch <new branch>
Enter fullscreen mode Exit fullscreen mode

Be careful. This will create a new branch based on the current head.

To create a branch based on a different branch, run

git branch <new branch> <base branch>
Enter fullscreen mode Exit fullscreen mode

To create a new branch and switch to that branch, run

git checkout -b <new branch> <base branch>
Enter fullscreen mode Exit fullscreen mode

Operations in a branch

Stage changes

To add all of the changes you make, run

git add .
Enter fullscreen mode Exit fullscreen mode

To add a specific change, run

git add /path/to/file
Enter fullscreen mode Exit fullscreen mode

Unstage changes

To unstage all of the changes you make, run

git reset .
Enter fullscreen mode Exit fullscreen mode

To unstage a specific change, run

git reset /path/to/file
Enter fullscreen mode Exit fullscreen mode

Stash changes

Stash is used to temporarily save changes so you can work on something else and come back later.
To stash your changes, run

git stash
Enter fullscreen mode Exit fullscreen mode

To stash with message, run

git stash push -m <message>
Enter fullscreen mode Exit fullscreen mode

To retrieve the most stashed changes, run

git stash pop
Enter fullscreen mode Exit fullscreen mode

To retreive a specific stash, run

git stash apply stash@{number}
Enter fullscreen mode Exit fullscreen mode

Clean untracked changes

Clean is used to remove untracked changes. To remove all of untracked changes, run

git clean -f . 
Enter fullscreen mode Exit fullscreen mode

Diff changes

Diff is useful to quickly to see the difference between two sources.
To see the difference between working directory and local repo, run

git diff 
Enter fullscreen mode Exit fullscreen mode

To see the difference between staging and local repo, run

git diff --cached
Enter fullscreen mode Exit fullscreen mode

Commits

Commit captures the staged changes and persist into your repository.

git commit -m <message>
Enter fullscreen mode Exit fullscreen mode

Once committed, you can run following commands.

Log commits

To see the history of commits, run

git log
Enter fullscreen mode Exit fullscreen mode

To see the compacted history, run

git log --oneline
Enter fullscreen mode Exit fullscreen mode

To see the visualized graph, run

git log --graph
Enter fullscreen mode Exit fullscreen mode

Reset commits

Revert winds back the commits we pushed.
Start by finding which commit you want to come back with git log. Then run,

git reset --soft/--hard <git-sha>
Enter fullscreen mode Exit fullscreen mode

Option soft will move back to the previous commit you chose and will not remove those changes. On the other hand, hard will remove completely so be careful.

Revert commits

Revert creates a new commit undoing changes you made in a specific commit.
Start by finding which commit you want to come back with git log. Then run,

git revert <git-sha>
Enter fullscreen mode Exit fullscreen mode

Merge commits

Merge incorporates one branch into another one. Typically, it's used to integrate what you developed on a branch into the main branch.

First, switch the target branch. Run

git checkout <target branch>
Enter fullscreen mode Exit fullscreen mode

Then, run

git merge <merging branch>
Enter fullscreen mode Exit fullscreen mode

There are more Git commands, these have covered the daily development flow I've had for years.

For more useful content, follow me @bitethecode or on Github!

Top comments (0)