Setup
git config --global user.name “[firstname lastname]”- Set your name for credit in version history. Without the global flag set it for a local repo. And without the [firstname lastname] get the current usergit config --global user.email “[valid-email]”- Set your email address for associating with history markers.git config --global color.ui auto- Enable automatic command line coloring for Git for easy reviewing.git config --help- Display help information about Git configuration.git init- Initialize a new Git repository in the current directory.touch .gitignore- Create a .gitignore file to specify untracked files to ignore.- Include in .gitignore: .DS_Store, .project, *.pyc - Common files to ignore in Git.
Basic Commands
git status- Check the status of changes as untracked, modified, or staged.git add -A- Add all changes (including untracked files) to staging.git reset calc.py- Remove 'calc.py' from staging area.git reset- Remove all changes from the staging area.git commit -m "Initial Commit"- Commit the staged changes with a message.git log- View the commit history.
Common Workflow
git clone [url]- Clone a repository into a new directory.git remote -v- List the remote connections you have to other repositories.git branch -a- List all branches, local and remote.git diff- Show changes between commits, commit and working tree, etc.git pull origin master- Fetch changes from the master branch and merge them.git push origin master- Push local changes in the master branch to remote.
Connecting to a Remote Repository
git clone [url]- Clone the remote repository to your local machine.cd [repository-name]- Change directory to your cloned repository.git remote add origin [url]- Add a remote repository URL.git remote -v- Verify the new remote URL.
Branching and Merging
git branch [branch-name]- Create a new branch.git checkout [branch-name]- Switch to the newly created branch.git status- Check the status in the new branch.git add -A- Add all new changes to staging in the new branch.git commit -m "New feature"- Commit your changes with a message.git push -u origin [branch-name]- Push the new branch to the remote repository.git checkout master- Switch back to the master branch.git pull origin master- Update local master branch.git merge [branch-name]- Merge the new branch into master.git push origin master- Push the updated master to the remote repository.git branch -d [branch-name]- Delete the local branch after merging.git push origin --delete [branch-name]- Delete the remote branch.