-
Notifications
You must be signed in to change notification settings - Fork 0
tutorial advanced git
Not all files should be committed. For example, compiled files (.o
, .out
, etc) and temporary files generated by your text editor (#example.c#
, etc) should probably stay out of your repo. In order to keep these files out, add a .gitignore
file. Using the wildcard *
, you can specify files to keep out.
For example:
*.o # keep out all files ending in .o
*.sublime-* # keeps out .sublime-project and .sublime-workspace files
passwords.txt # if there are secrets kept in your project directory, they probably shouldn't be committed.
__pycache__/* # sometimes there will be entire folders that you want to keep out.
You can find a collection of template files here. If your project involves multiple languages and tools (i.e. compiled C files and text editor temporary files), you can simply paste the contents of both gitignore templates into your .gitignore
file.
- I forgot to add a gitignore; I just added one, but all of the temporary files are still there!
Simply delete the temporary files, and restage. For example, if your undesired files are *.o
files:
rm -r *.o # recursively remove files ending in .o
git add --all # restage files. Equivalent to "git add -A"
- I accidentally staged something bad, but I've also made changes that I don't want to lose.
Unstage the files, then re-add the desired files:
git reset
git add super_important_changes.c
If this happens a lot, you probably want to add your file to .gitignore
.
- I committed some code, but I changed my mind.
You can "non-destructively" revert changes with git revert
. This will revert the changes with a new commit undoing the change so that you can revert your revert if you change your mind again.
$ git log --online
86bb32e bad commit
3602d88 good commit
[...]
$ git revert HEAD
$ git log --online
1061e79 Revert "bad commit"
86bb32e bad commit
3602d88 good commit
[...]
Sometimes, you'll make a change that you really regret, and need to revert. With one developer, and developing a single feature at a time, that's usually enough.
What happens when there's more than one person, working on more than one feature? What happens when one developer needs to revert their changes, but that would result in losing changes made by another?
Branches act as a parallel, separate code repository; changes made on one branch are not reflected in other branches. Branches can be created, destroyed, or reverted without affecting any other work. When using branches, a new branch is generally created for some new subsystem that may or may not work with the code base. After satisfactory development on that branch, the branch is merged into the main branch.
To create a branch:
git branch foo
To switch to another branch:
git checkout foo
To merge:
git merge foo # Merges changes made in branch foo into the current branch
Ever want to know who indented with tabs or didn't include a function header?
git blame example.c
Just make sure no one blames someone else.