Skip to content

Instantly share code, notes, and snippets.

@quachngocxuan
Last active July 22, 2023 17:38
Show Gist options
  • Save quachngocxuan/5d0df63be594a6aefdb38ca3fed89d98 to your computer and use it in GitHub Desktop.
Save quachngocxuan/5d0df63be594a6aefdb38ca3fed89d98 to your computer and use it in GitHub Desktop.
Learn enough to be dangerous - Git

Learn enough to be dangerous - Git

This is based primarily on this guide.

CORE CONCEPTS

Git status sequence

The status of the file has been promoted from untracked to staged, which means the file is ready to be added to the repository. Untracked/unstaged and staged are two of the four states commonly used by Git. Technically, untracked and unstaged are different states, but the distinction is rarely important because git add tracks and stages files at the same time.

The main Git status sequence for a changing file. git status sequence

Commitment issues

One common issue when learning Git involves figuring out when to make a commit. Unfortunately, there’s no simple answer, and real-life usage varies considerably. My best advice is to make a commit whenever you’ve reached a natural stopping point, or when you’ve made enough changes that you’re starting to worry about losing them. In practice, this can lead to inconsistent results, and it’s common to work for a while and make a large commit and then make a minor unrelated change with a small commit. This mismatch between commit sizes can seem a little weird, but it’s a difficult situation to avoid.

Many teams (including most open-source projects) have their own conventions for commits, including the practice of squashing commits to combine them all into one commit for convenience.

Ignore files to be committed

Git lets us ignore files to be committed using a special hidden configuration file called .gitignore Example: content in the file .gitignore

.DS_Store

Wildcards can be used to ignore many files

*.tmp

Or a directory can be ignored also

tmp/
cached/*

Or all except a file

!/log/.keep

Branching

One of the most powerful features of Git is its ability to make branches, which are effectively complete self-contained copies of the project source, together with the ability to merge one branch into another, thereby incorporating the changes into the original branch. The best thing about a branch is that you can make your changes to the project in isolation from the master copy of the code, and then merge your changes in only when they’re done. This is especially helpful when collaborating with other users; having a separate branch lets you make changes independently from other developers, reducing the risk of accidental conflicts.

The main repository evolution is a series of commits, and the branch effectively represents a copy of the repo at the time the branch was made.

CORE COMMANDS

Man page of git

$ git help

Or for a specific command

$ git help <command>

Scrolling your mouse to see all command explanation on the command line

Initial setup git environment for an user

$ git config --global user.name "Your Name"
$ git config --global user.email your.email@example.com

Configuration information would be stored in ~/.gitconfig

Init a repo

$ git init

View the current status of repo (ex: which filed are't tracked)

$ git status

Add all untracked files

$ git add -A

Or

$ git add .

Or add only a file

$ git add <filename>

Unstage tracked files (to commit)

$ git rm --cached <file>

Commit (to local changes)

$ git commit -m 'Message here'

Let write commit messages in the present tense using the imperative mood, as in “Initialize repository” rather than “Initializes repository” or “Initialized repository”.

And when to include changes to stage, add more parameter -a

$ git commit -a -m 'Message here'

or

$ git commit -am 'Message here'

Note that the -a option includes changes only to files already added to the repository, so when there are new files it’s important to run git add -A to make sure they’re added properly.

If you did some typo in commit message, change it by this

$ git commit --amend

View the record of commit

$ git log

or use parameter -p to show the full diffs represented by each commit

$ git log -p

Connect to git server

$ git remote add origin https://github.com/<name>/reponame.git
$ git push origin master

Push to git server

$ git push

or

$ git push origin master

master can be changed into another branch.

or

$ git push -u origin master

The -u option to git push sets GitHub as the upstream repository, which means we’ll be able to download any changes automatically when we run git pull.

View difference between local commit and untracked changes

$ git diff

or checking diff with other branch from current branch

$ git diff <branch>

Create and checkout a new branch

$ git checkout -b <new branch>

-b option is to create new branch.

Delete a branch

$ git checkout master
$ git branch -D test-branch

Note here that we need to use -D instead of -d to delete the branch because test-branch is unmerged.

Check list of branches

$ git branch
  • indicating the currently checked-out branch or
$ git branch -a

-a option means all.

Merge a branch to master

$ git checkout master
$ git merge <branch>
$ git push

Recover after a mistake on content

$ git checkout -f

-f option means 'force' It’s worth noting that git checkout -f itself is potentially dangerous, as it wipes out all the changes you’ve made, so use this trick only when you’re 100% sure you want to revert to HEAD

Recover to a specific state (or commit)

We should run git log first to get SHA code of the commit identifier we want to recover

$ git log

Then run command with SHA code

$ git checkout <SHA code>

Get changes from remote repo

$ git pull

Because of the potential for conflict, it’s a good idea to do a git pull before making any changes on a project with multiple collaborators.

Add alias for command

$ git config --global alias.co checkout

So new you can checkout by the command:

$ git co <branch>

RECOMMENDED READINGS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment