This will tell you files that are untracked (new file) and tracked but not staged (modified file). git add should be used for both cases. After that git status will show “changes to be committed” and we can use git commit -m "message" to safely commit the change.
1
2
3
4
5
6
$ git status -s
M README # modified but not stagedMM Rakefile # both staged and unstagedA lib/git.rb # new files in staging areaM lib/simplegit.rb # modified and staged?? LICENSE.txt # new files that aren't tracked
# compared with current saved versiongit diff
git diff HEAD
# compare with previous saved versiongit diff HEAD~1
# compare stage with current saved versiongit diff --staged
# see the difference for a particular filegit diff <file>
# compare between two commitsgit diff HEAD~3..HEAD
# compare between two branchesgit diff <branch1>..<branch2>
We can stash our current work and move to another job, say debugging. After finishing the job we can continue on our previous work.
1
2
3
4
5
6
7
8
9
10
11
# stash current workgit stash
# list the stashed workgit stash list
# restore ith stashed workgit stash apply stash@{i}# still keep the stashed contentgit stash drop stash@{i}# drop the stashed contentgit stash pop stash@{i}# = apply + drop
Remove the file from both the staging area and the working directory.
1
2
3
4
5
# remove the file from both the staging area and the working directorygit rm
# remove the file from staging area but keep in the working directorygit rm --cached
# list tagsgit tag # list allgit tag -l "v1.0"# match pattern# create tagsgit tag <name> <commit>
git tag -a <name> -m "message"# with message# show information of a taggit show <tag name>
# push tags to remotegit push origin <tag name>
git push origin --tags # push all tags# delete tagsgit tag -d <tag name>
When there are tags, you can use git describe for a specific commit and it will output <tag>_<numCommits>_g<hash>, where tag is the closest ancestor tag in history, numCommits is how many commits away that tag is, and hash is the hash of the commit being described
HEAD is a pointer that points to the current version.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# version rollbackgit reset --hard
# rollback but keep the modification in staging areagit reset --soft
# rollback but keep the modificationgit reset --mixed # default behavior for reset# find the logs of historygit log
# find the logs for all git commandsgit reflog
Connecting over HTTPS doesn’t require SSH Keys but you have to type your password every time.
Connecting over SSH requires generating SSH Keys on each computer.
1
2
# create SSH Key for authenticationssh-keygen -t ed25519 -C "your_email@example.com"
Then copy the content in id_ed25519.pub to GitHub -> Account settings -> Add SSH Key.
1
2
3
4
5
6
7
8
# connect a local repo to github repogit remote add origin git@github.com:example.git
# first time push local repo, need to connect local branch with server branchgit push --set-upstream origin master
# clone a repo from githubgit clone git@github.com:example.git
# give a name other that 'origin'git remote add <shortname> <url>
# update remote branchesgit fetch <remote>
# fetch & mergegit pull
# push your branch to your remote servergit push <remote> <branch>
There might be some files that you don’t want Git to track, for example, the log file and the file produced by building system. Then you can add a .gitignore file in your folder:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# ignore all .a files*.a
# but do track lib.a, even though you're ignoring .a files above!lib.a
# only ignore the TODO file in the current directory, not subdir/TODO/TODO
# ignore all files in any directory named buildbuild/
# ignore doc/notes.txt, but not doc/server/arch.txtdoc/*.txt
# ignore all .pdf files in the doc/ directory and any of its subdirectoriesdoc/**/*.pdf